2

I'm trying to use new WC_Abstract_Order::get_coupon_codes() methods to get coupon codes in an order. This was the previous working snippet:

$coupons = $order->get_used_coupons();

$nCoupons = count($coupons);
            if($nCoupons == 1)
                $descrizioneCoupon = 'Coupon ' . $coupons[0];
            else if($nCoupons > 1)
            {
                $descrizioneCoupon = 'Coupons ';
                for($i=0;$i<$nCoupons;$i++)
                {
                    $descrizioneCoupon .= $coupons[$i];
                    if($i<$nCoupons-1)
                    {
                        $descrizioneCoupon .= ', ';
                    }
                }
            }

So I'm trying to replace the first line with this one:

$coupons = \WC_Abstract_Order::get_coupon_codes(); 

I'm doing this since get_used_coupons() is deprecated.

As a result I get this error message:

Fatal error: Uncaught Error: Using $this when not in object context ...

Any suggestions? Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Davide Iandoli
  • 99
  • 3
  • 13
  • 1
    @LoicTheAztec I tested my code replacing the old method with the new ($order->get_coupon_codes) and it works, thanks. I didn't know what was the new method to get coupon codes. My fault was that I wasn't enough clear in my question. – Davide Iandoli Sep 09 '19 at 07:56

1 Answers1

2

The solution here is simply to replace:

$coupons = $order->get_used_coupons();
// OR: 
// $coupons = \WC_Abstract_Order::get_coupon_codes();

by:

$order->get_coupon_codes();

Don't use \WC_Abstract_Order, always use the WC_Order instance object $order

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399