订阅优惠券PHP

时间:2019-02-11 11:34:14

标签: php woocommerce woocommerce-subscriptions

我发现此片段以编程方式向订单添加了优惠券:

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

效果很好。但是,我想将优惠券应用于现有订阅,以便在续订时将优惠券应用于将自动创建的订单。

有没有办法做到这一点?

谢谢!

1 个答案:

答案 0 :(得分:0)

像您这样的声音可能想要设置一个脚本,使其以给定的时间间隔运行,检查订阅,创建订单并应用优惠券。根据运行的系统,您可能希望查看Linux上的cron或Windows上的Windows Task Scheduler,以便定期执行脚本。

相关问题