2017-11-14 5 views
1

배송 방법으로 "빠른 배송"을 주문하면 주문 상태가 On-Hold로 업데이트됩니다.배송 방법에 따라 Woocommerce 주문 상태 변경

내가 다른 약간의 "속달 배달"배송 방법 요금을 가지고 있다고 생각했는데 '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; 
    } 
} 

편집 :

여기

는 내가 가지고있는 코드입니다 -----------------------------

Woocommerce 테이블 전송을 사용하는 사용자는 get_method_id가 테이블 속도 ID를 반환하므로 의견을하시기 바랍니다 더 좋은 방법이 있다면 나는 빠르고 적은 메모리 집약적 인 재미를 사용하는 것을 선호 ... 아래로 대신

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

을 get_method_title 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는 대소 문자를 구분하지 않는다는 것을 읽었습니다. –

관련 문제