我正在调整库存,以便通过后端完成订购数量的手动更改。我要处理三种情况:
我希望为此目的使用woocommerce_process_shop_order_meta
钩。但是,它不会对发布的信息列表中的任何更改进行跟踪。
获取项目/数量变化列表的适当钩子/方法是什么?
无论如何,找到了达到预期结果的方法。 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
}
}