doudiao2335 2018-01-10 13:29
浏览 71
已采纳

以编程方式将优惠券应用于WooCommerce3中的订单

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?

  • 写回答

2条回答 默认 最新

  • doujia5863 2018-01-10 15:29
    关注

    OK, so I played about a little longer and it looks like in V3 things are a little more manual.

    Adding a WC_Order_Item_Coupon item to a woo order does simply that. It adds the coupon object to the order object. No calculations are made and the product line items remain unchanged. You have to iterate over the product items manually and apply the coupon yourself by calculating the line item totals and subtotals. calculate_totals() then does as expected.

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

    I wrote a helper function to make sure the coupon applies to the product in question coupon_applies_to_product(). Strictly not needed given I'm creating the order entirely in code.. but I use it it other places so added it.

    // Add the coupon to the order
    $item = new WC_Order_Item_Coupon();
    $item->set_props(array('code' => $coupon_code, 'discount' => $discount_total, 'discount_tax' => 0));
    $order->add_item($item);
    $order->save();
    

    You now get the nicely formatted order in wp-admin with line items showing the specific discount + the coupon code etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形