首页 > 解决方案 > WooCommerce 钩子 – woocommerce_update_order 问题

问题描述

我已经注册了以下 woocommerce 钩子:

add_action('woocommerce_update_order', 'some_func', 300, 2);
function some_func($order_id, $order){
  // ...
}

但是,我有几个问题:

这会触发多次,而不是仅在更新订单时触发。它使用旧命令触发两次,一次触发一次。

我还尝试了以下方法:

add_action('woocommerce_update_order', 'some_func', 300, 2);
function some_func($order_id, $order){
    remove_action('woocommerce_update_order', 'some_func');
    // ...
}

这也不会改变它。

另外,我尝试修改 remove_action 以包含优先级和参数计数,例如:

add_action('woocommerce_update_order', 'some_func', 300, 2);
function some_func($order_id, $order){
    remove_action('woocommerce_update_order', 'some_func', 300, 2);
    // ...
}

现在,它只触发一次,但它给了我旧的命令而不是新更新的命令

我正在使用 WooCommerce 3.7.0。

关于如何在更新后仅触发一次钩子而获得最新版本的订单的任何建议?

谢谢!

标签: wordpresswoocommercehook-woocommerce

解决方案


add_action( 'save_post', 'my_save_post_function', 10, 3 );

function my_save_post_function( $post_ID, $post, $update ) {

  if("shop_order" == $post->post_type){
  $msg = 'An order updte fireda';

  wp_die( $msg );
  }
}

在后置条件内执行您的操作


推荐阅读