这里的想法是,当订单以“快递”作为送货方式时,订单状态会更新为待定。
由于我有一些不同的“快递”运输方式费率,我认为通过使用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_method_id 返回表速率 ID,所以我使用 get_method_title 代替,如下所示,如果有更好的方法请评论...
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;
}
}
}
我更喜欢使用速度更快、内存占用更少的函数,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;
}
}
}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。
经过测试并有效……
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句