2017-12-29 21 views
0

私はバックエンドで行われた注文数量の手動変更の在庫を調整しようとしています。私は3つの状況を処理するためにしました:バックエンド経由で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 
    } 
} 
関連する問題