订单更新后WooCommerce挂钩吗? [英] WooCommerce hook after order is updated?

查看:151
本文介绍了订单更新后WooCommerce挂钩吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过管理员更改某人的订单(例如他们的地址或自定义元字段)时,是否可以使用一个钩子?我阅读此问题,但不幸的是,保存订单之前已触发woocommerce_process_shop_order_meta,这意味着我无权访问新更新的数据.我需要的是能够使用保存到订单中的新数据.

Is there a hook I can use when I make a change to someone's order via the admin (such as their address, or a custom meta field)? I read this question but unfortunately woocommerce_process_shop_order_meta is fired before the order is saved, meaning I have no access to the newly updated data. What I need is to be able to use the new data that is saved to the order.

更新:使用save_post_shop_order的问题是在命中元数据之前就已对其进行了更新,因此我无法比较以前保存的元值,例如:

UPDATE: An issue with using save_post_shop_order is that the meta is updated before this is hit, so I can't compare the previously saved meta value, for example:

$metaArray = $_POST['meta'];

foreach($metaArray as $meta => $key) {
    $metaArr[$key["key"]] = $key["value"];
}

$meta = get_post_meta($order->ID);

if($meta['coverstart'][0] != $metaArr['coverstart']) {
    die("COVER START DATE HAS CHANGED");
}

die()永远不会被选中,因为脚本始终会获取新保存的值.

The die() is never hit, because the script always gets the newly saved value.

推荐答案

对不起,但保存订单后触发了woocommerce_checkout_update_order_meta …请参阅位于

Sorry but woocommerce_checkout_update_order_meta is fired after the order is saved… See this extract source code located in WC_Checkout create_order() method:

// Save the order.
$order_id = $order->save(); // <== Order is saved here before

do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data ); <== // The hook

return $order_id;

因此,在woocommerce_checkout_update_order_meta中,您可以获取已保存的订单数据:

    通过从$order_id参数中检索WC_Order对象并对其使用所有方法,来实现
  • .
  • 或将get_post_meta()$order_id参数一起使用以获取保存在wp_postmeta数据库表中的数据.
  • by retrieving the WC_Order object from the $order_id argument and using all methods on it.
  • or using get_post_meta() on with the $order_id argument to get the data saved in wp_postmeta database table.

然后您可以使用update_post_meta()功能更新数据…

Then you can update the data with update_post_meta() function…

您甚至可以在保存数据之前使用woocommerce_checkout_create_order ...

您将能够使用WC_Order类的所有可用方法(CRUD getters方法)从$order参数获取数据.

You will be able to get the data from the $order argument using all available methods for the WC_Order class (CRUD getters methods).

您将能够使用CRUD设置器方法更改此数据并将其保存...

You will be able to alter this data and saving it using the CRUD setters methods…

stackOverFlow中的一些示例

如果您需要在订单处理后执行此操作,则可以使用以下挂钩:

If you need to do that after the order process the hooks to be used can be:

  • woocommerce_new_order(在新创建的订单事件中)
  • woocommerce_thankyou(在收到订单的页面上)
  • woocommerce_order_status_changed(发生订单状态更改事件)
  • woocommerce_new_order (on newly created order event)
  • woocommerce_thankyou (on order received page)
  • woocommerce_order_status_changed (on order status changing event)

可能还有其他人...

And may be some others…

要在后端保存订单时更改数据,您将使用具有3个参数的 save_post_shop_order :$post_id$post$update

To alter the data when order is saved in backend, you will use save_post_shop_order that has 3 arguments: $post_id, $post and $update

这篇关于订单更新后WooCommerce挂钩吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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