2017-11-14 9 views
1

ここでの考え方は、出荷方法として「速達」が指定された注文がオン・ホールドに更新されるという考え方です。そこに私は言葉'express'はどこにでもフォーマットされた配送方法タイトルに表示されるかどうかを確認するためにstristr()を使用していると考え、いくつかの異なる「速達」配送方法・レートを持つ配送方法に基づいて、ウーココマスの注文ステータスを変更します。

。しかし、私は何も手に入らないので何かが欠けているようです。

注文状況を更新できるように、注文発送方法が「速達」であるかどうかを確認するにはどうすればよいですか?

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; 
    } 
} 

EDIT ------------------------------:ここ

は、私が持っているコードです。 -----------------------------

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; 
     } 
    } 
} 
+0

$ SHIPPING_METHODの値が何であるかをデバッグする書き込み。 –

答えて

1

私は、 ction strpos()代わりに、配送方法IDは常に小文字で表示されます(スラッグのようなものです)。

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_id(), $search) !== false){ 
      $order->update_status('on-hold'); 
      break; 
     } 
    } 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルやも任意のプラグインファイルになります。

テストおよび動作します...

+0

こんにちは@LoicTheAztec、あなたの答えをありがとう、私はエクスプレス注文のために動作するようにこのコードを取得することができないのですか?しかし、もし私が変更すれば!== falseをtrueにすると、すべてが保留に変更されます。これは、問題がstrposを使用することを意味します。私はstrposのドキュメントを見てきましたが、これはintergerを返すだけですが、これが問題になるかもしれませんか?もう一度ありがとう –

+0

お詫び申し上げます、私はstrposが大文字小文字を区別していないことを読んだ、それはない!!! –

関連する問題