首页 > 解决方案 > WooCommerce:更新订单

问题描述

每次使用“woocommerce_update_order”操作挂钩更新订单后,我都会尝试运行一些代码,如下所示:

add_action( 'woocommerce_update_order', 'update_order' );

在开发“update_order”函数花了一些时间后,我意识到它并没有像我想象的那样只被调用一次,而是被多次调用。因此,为了测试,我重写了“更新订单”功能,如下所示:

function update_order( $order_id ) {
    $order = wc_get_order( $order_id );
    $current_count = get_post_meta( $order_id, '_edit_sale_count', true );
    $new_count = 1;
    if( $current_count ) $new_count = $current_count + 1;
    $order->update_meta_data( '_edit_sale_count', $new_count );
    $order->save();
}

更新单个订单并检查数据库后,我意识到函数“update_order”被调用了 1500 次......

所以我的问题是为什么会这样?以及如何防止在一次更新中多次执行并强制它只运行一次?

谢谢

标签: woocommercehook-woocommerce

解决方案


推荐阅读