2017-12-29 31 views
0

백엔드를 통해 주문한 수량을 수동으로 변경하려면 재고 조정을 원합니다. 나는 세 가지 상황을 처리했습니다 :백엔드를 통해 WooCommerce 주문에서 수행 된 변경 목록을 어떻게 얻을 수 있습니까?

  1. 새 항목이 기존 항목이 기존 항목의 수량을 변경 순서
  2. 에서 제거 순서
  3. 에 추가 될 때

이 목적으로 woocommerce_process_shop_order_meta 후크를 사용하고 싶습니다. 그러나 게시 된 정보 목록에서 변경 사항을 추적하지 않습니다.

항목/수량 변경 목록을 얻는 데 적합한 후크/방법은 무엇입니까?

+0

: 여기

사례 누군가의 코드가 비슷한 솔루션을 찾고입니다. 비록 이것을 처리하는 더 좋은 방법이 있다면, 감사 하겠지만. – Gaurav

+0

먼저 답변에 맞춤 설정 코드를 추가해야합니다 ... – LoicTheAztec

답변

0

어쨌든 원하는 결과를 얻는 방법을 찾았습니다. woocommerce_process_shop_order_meta은 이러한 목적에 적합한 것이 아닙니다. 그러나, 여기에서는 약간의 모호하고 크게 문서화되지 않은 후크가 유용합니다. `woocommerce_ajax_add_order_item_meta`,`woocommerce_delete_order_items`, 그리고`woocommerce_before_save_order_items` : 나는 구성하는 방법을 알아 냈어요

//When a new order item is added 
add_action('woocommerce_new_order_item', 'su_oqa_add_item', 10, 3); 
function su_oqa_add_item($item_id, $item, $order_id) { 
    $order  = wc_get_order($order_id); 
    $product = $item->get_product(); 
    // Update product stock 
} 

//When an order item is deleted 
// use before hook to get access to current item status in the order 
add_action('woocommerce_before_delete_order_item', 'su_oqa_remove_item'); 
function su_oqa_remove_item($item_id) { 
    $order_id = wc_get_order_id_by_order_item_id($item_id); 
    $order = wc_get_order($order_id); 
    $item  = $order->get_items()[$item_id]; 
    $product = $item->get_product(); 
    // Update product stock 
} 

//When an order/item quantity is updated 
add_action('woocommerce_before_save_order_items', 'su_oqa_save_items', 10, 2); 
function su_oqa_save_items($order_id, $posted) { 
    $order = wc_get_order($order_id); 
    $items = $order->get_items(); 
    $qtys = $posted['order_item_qty']; 
    foreach ($qtys as $item_id => $qty) { 
     $item = $items[$item_id]; 
     $product = $item->get_product(); 
     // Update product stock 
    } 
}