我们如何通过后端获取对 WooCommerce 订单执行的更改列表?

How can we obtain the list of changes performed on WooCommerce order via backend?

我正在寻找调整库存以通过后端完成的订单数量手动更改。我必须处理三种情况:

  1. 当新商品添加到订单时
  2. 从订单中删除现有商品时
  3. 当现有项目的数量发生变化时

我希望为此目的使用 woocommerce_process_shop_order_meta 挂钩。但是,它不会跟踪已发布信息列表中的任何更改。

获取 item/quantity 更改列表的适当 hook/method 是什么?

总之,想出了一个达到预期效果的方法。 woocommerce_process_shop_order_meta 不是用于此目的的正确钩子。但是,一些晦涩且大部分未记录的钩子在这里很有用。

以下是代码片段,以防有人正在寻找类似的解决方案:

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