WooCommerce:在订单中保存对产品的更改时挂接 [英] WooCommerce: hook when saving changes to product in an order

查看:119
本文介绍了WooCommerce:在订单中保存对产品的更改时挂接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找时间...

I have been searching for hours...

我不知道如何在按现有顺序编辑产品数量后单击保存"时如何执行功能.

I can't figure out how get a function to be executed when clicking "save" after editing the quantity of a product in an existing order.

我尝试过:

add_action('woocommerce_order_edit_product', 'your_function_name');
function your_function_name(){
//my php function code would be here
}

但单击保存"时未调用your_function_name函数.

but the your_function_name function is not being called when clicking save.

我测试了该函数,当直接调用该函数时,它应该可以正常工作,所以我认为我得到了错误的钩子...

I tested the function and when calling it directly it works as it should, so I think I got the wrong hook...

推荐答案

在解决此问题2天后,我发现它:有两个钩子,保存之前和之后一个:

after wrestling with this issue for 2 days now, i found it: there is two hooks, one before and one after saving:

  1. woocommerce_before_save_order_items
  2. woocommerce_saved_order_items

在后端保存订单时都会触发

两者.保存之前一个,之后保存一个.

both are fired when saving an order in the backend. one before the save and one afterwards.

两个钩子都具有相同的变量:$ order_id(int)& $ items(数组)

both two hooks carry the same variables: $order_id (int) & $items (array)

我猜第一个钩子,您可以得到旧订单并将其内容与items数组进行比较,以查看发生了什么变化.至少这是我现在想要完成的.

i guess with the first hook, you could get the old order and compare its contents with the items array to see what has changed. at least this is what i try to accomplish now.

因此,这就是触发该事件的方式:

so this is how you would trigger this:

add_action( 'woocommerce_before_save_order_items', 'so42270384_woocommerce_before_save_order_items', 10, 2 );
function so42270384_woocommerce_before_save_order_items( $order_id, $items ) {
    echo $order_id;
    var_dump( $items );
}

注意..

将产品添加到现有订单中确实实现了另一个在此之前被调用的钩子(因此,在单击SAVE时,将触发上述函数,但是在保存之前,该订单及其项已被设置(添加产品时,该订单(将立即保存).这意味着$order = new WC_Order( $order_id );之前和之后都已经有新项目,因此无法找到已更改的内容.)但是woocommerce_ajax_add_order_item_meta挂钩是在添加产品"上触发的,并为此提供了帮助.祝大家编码愉快.

be aware..

adding a product to an exitsting order does implement another hook that gets called before this (so when hitting SAVE, the above function will fire, but the order and its items are already set BEFORE saving (when adding a product, the order will save immediately). that means $order = new WC_Order( $order_id ); will have the new items already in it, before and after, so there is no way to find, what has changed.). but the woocommerce_ajax_add_order_item_meta hook is triggered on 'add product' and helped me on that end. happy coding everyone..

这篇关于WooCommerce:在订单中保存对产品的更改时挂接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆