【问题标题】:How to change order status based on coupon used in woocommerce orders如何根据 woocommerce 订单中使用的优惠券更改订单状态
【发布时间】:2020-08-25 06:51:15
【问题描述】:

我想在下订单后使用特定优惠券时更改订单状态。我找到了一些可以更改订单状态的代码,但我不知道如何从顺序。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    试试这个代码

    add_action('woocommerce_thankyou', 'change_order_status_based_on_coupon', 10, 1);
    
    function change_order_status_based_on_coupon( $order_id ) {
    
        if ( ! $order_id )
            return;
    
        // Getting an instance of the order object
        $order = wc_get_order( $order_id );
    
        // Coupons used in the order LOOP 
        foreach( $order->get_used_coupons() as $coupon_code ){
    
            // Retrieving the coupon ID
            $coupon_obj = get_page_by_title( $coupon_code, OBJECT, 'shop_coupon' );
            $coupon_id  = $coupon_obj->ID;
    
            // Get an instance of WC_Coupon object
            $coupon = new WC_Coupon($coupon_id);
    
            // Now you can get code in your condition
            if ( $coupon->get_code() == 'Your coupon code' ){
                $order->update_status('Your order status', 'order_note'); // order note is optional, if you want to  add a note to order
                break;
            }
            
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-24
      • 1970-01-01
      • 2017-12-12
      • 2022-10-05
      • 2021-04-08
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 2017-08-03
      相关资源
      最近更新 更多