0

I found this snippet that adds a coupon to an order programatically:

// 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();

It works well. However, I would like to apply a coupon to an existing subscription so that when it renews, the coupon will be applied to the order that will be created automatically.

Is there a way to do this?

Thanks!

Lhen
  • 181
  • 3
  • 15

1 Answers1

0

Sounds like you perhaps want to setup a script to be run at given intervals, check subscriptions, create orders and applies coupons. Depending on what system you are running you probably want to look into cron on Linux or Windows Task Scheduler on Windows in order to execute your script at regular intervals.

inquam
  • 12,664
  • 15
  • 61
  • 101
  • all those are taken cared of by WooCommerce. It's how to apply the coupon when WooCommerce renews the subscription (creating an order) is where I'm stuck at. – Lhen Feb 11 '19 at 13:42