Я нашел этот фрагмент, который программно добавляет купон к заказу:
// Create the coupon
global $woocommerce;
$coupon = new WC_Coupon($coupon_code);
// Get the coupon discount amount (My coupon is a fixed value off)
$discount_total = $coupon->get_amount();
// Loop through products and apply the coupon discount
foreach($order->get_items() as $order_item){
$product_id = $order_item->get_product_id();
if($this->coupon_applies_to_product($coupon, $product_id)){
$total = $order_item->get_total();
$order_item->set_subtotal($total);
$order_item->set_total($total - $discount_total);
$order_item->save();
}
}
$order->save();
Он работает хорошо.Однако я хотел бы применить купон к существующей подписке, чтобы при его продлении купон применялся к заказу, который будет создан автоматически.
Есть ли способ сделать это?
Спасибо!