woocommerce - 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 次......
所以我的问题是为什么会这样?以及如何防止在一次更新中多次执行并强制它只运行一次?
谢谢
解决方案
推荐阅读
- vuejs2 - 如何在 Nuxt 本地托管谷歌字体
- ruby-on-rails - Docker + Rails + NGINX - “错误” IP 地址
- angular - 单击单元格按钮时如何获取网格的行数据
- ios - SwiftUI Firebase - 读取数组数据以查看字符串是否等于数组字符串
- reactjs - 将三元表达式放入字符串插值时出现语法错误
- function - 如何添加事件监听器以关闭菜单切换
- python - 映射整数在 ElasticSearch 中不起作用
- android - binding.root 表示什么?
- java - 如何在不使用 for 循环的情况下通过字符串列表过滤 java 流
- snowflake-cloud-data-platform - 如何使用单个 sqs 为多个表创建外部管道?