I'm developing a plugin that creates an order directly (no cart) and applies a coupon. In version 3.0 of the woo API the function add_coupon()
has been deprecated in favour of a WC_Order_Item_Coupon
object you add to the order.
Create the coupon
$coupon = new WC_Order_Item_Coupon();
$coupon->set_props(array('code' => $coupon, 'discount' => $discount_total,
'discount_tax' => 0));
$coupon->save();
This is successful. I can validate by calling $coupon->get_discount()
.
I then add the coupon to the order and recalculate totals:
$order->add_item($item);
$order->calculate_totals($discount_total);
$order->save();
Logging into wp-admin I can see the order with coupon code visible. However, the coupon has had no effect on line items or total.
Have a misunderstood how api v3.0 intends us to handle coupons?