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

问题描述 投票:0回答:1

我正在调整库存,以便通过后端完成订购数量的手动更改。我要处理三种情况:

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

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

获取项目/数量变化列表的适当钩子/方法是什么?

php wordpress woocommerce hook-woocommerce
1个回答
0
投票

无论如何,找到了达到预期结果的方法。 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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.