代码之家  ›  专栏  ›  技术社区  ›  blackhill24

根据发货方式更改商业订单状态

  •  1
  • blackhill24  · 技术社区  · 8 年前

    这里的想法是,当订单以“快递”作为发货方式时,订单状态将更新为保留。

    由于我有一些不同的“快递”运输方式,我认为通过使用 stristr() 'express' 显示在格式化的发货方式标题中的任何位置。但我似乎错过了什么,因为我什么都没有得到。

    以下是我的代码:

    add_action( 'woocommerce_thankyou', 'express_orders_4865', 10, 1 );
    function express_orders_4865( $order_id ) {
        global $woocommerce;
    
        $order = new WC_Order( $order_id );
    
        $shipping_method = $order->get_shipping_method();
    
        if (stristr($shipping_method, 'express') === TRUE) {
            $order->update_status('on-hold');
        } else {
            return;
        }
    }
    

    编辑-----------------------------------------------------------

    对于任何使用Woocommerce Table Rate Shipping的人,get\u method\u id返回表Rate id,因此我使用get\u method\u标题,如下所示,如果有更好的方法,请发表评论。。。

    add_action( 'woocommerce_thankyou', 'express_shipping_update_order_status', 10, 1 );
    function express_shipping_update_order_status( $order_id ) {
        if ( ! $order_id ) return;
    
        $search = 'Express'; // The needle to search in the shipping method ID
    
        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );
    
        // Get the WC_Order_Item_Shipping object data
        foreach($order->get_shipping_methods() as $shipping_item ){
            // When "express delivery" method is used, we change the order to "on-hold" status
    
    
            if( strpos( $shipping_item->get_method_title(), $search ) !== false ){
                $order->update_status('on-hold');
                break;
            }
        }
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   LoicTheAztec    5 年前

    我更喜欢使用更快、内存占用更少的功能 strpos() 相反,因为运输方法ID总是小写的(就像一种slug)。

    所以最好是得到 WC_Order_Item_Shipping 对象数据,使用可用方法。

    所以代码应该是:

    add_action( 'woocommerce_thankyou', 'express_shipping_update_order_status', 10, 1 );
    function express_shipping_update_order_status( $order_id ) {
        if ( ! $order_id ) return;
        
        $search = 'express'; // The needle to search in the shipping method ID
    
        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );
    
        // Get the WC_Order_Item_Shipping object data
        foreach($order->get_shipping_methods() as $shipping_item ){
            // When "express delivery" method is used, we change the order to "on-hold" status
            if( strpos( $shipping_item->get_method_title(), $search ) !== false && ! $order->has_status('on-hold')){
                $order->update_status('on-hold');
                break;
            }
        }
    }
    

    代码进入函数。活动子主题(或主题)或任何插件文件中的php文件。

    测试和工作

        2
  •  0
  •   brasofilo Gary    4 年前

    add_action( 'woocommerce_thankyou', 'express_shipping_update_order_status', 10, 1 );
    function express_shipping_update_order_status( $order_id ) {
        if ( ! $order_id ) return;
    
        $search = 'express'; // The needle to search in the shipping method ID
    
        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );
    
        // Get the WC_Order_Item_Shipping object data
        foreach($order->get_shipping_methods() as $shipping_item ){
            // When "express delivery" method is used, we change the order to "on-hold" status
            if( strpos( $shipping_item->get_method_title(). $search ) !== false ){
                $order->update_status('on-hold');
                $order->save();
                break;
            }
        }
    }