【发布时间】:2021-06-02 20:51:50
【问题描述】:
您好,我正在尝试在 mu-plugins 中创建一个函数,以防止某些用户将订单状态从特定订单状态更改为特定订单状态。
我到处寻找,尝试了许多不同的方法,但似乎没有任何效果。
实际上该函数是使用woocommerce_order_status_changed 动作钩子运行的。问题是这个钩子在订单状态已经改变之后运行,是什么导致了无限循环。
我发现的最有用的钩子似乎是woocommerce_before_order_object_save。
我在 WooCommerce Github 上找到了 "Add an additional argument to prevent 'woocommerce_order_status_changed' from being called" 有用的相关主题。
我尝试使用@kloon code snippet solution:
add_filter( 'woocommerce_before_order_object_save', 'prevent_order_status_change', 10, 2 );
function prevent_order_status_change( $order, $data_store ) {
$changes = $order->get_changes();
if ( isset( $changes['status'] ) ) {
$data = $order->get_data();
$from_status = $data['status'];
$to_status = $changes['status'];
// Do your logic here and update statuses with CRUD eg $order->set_status( 'completed' );
// Be sure to return the order object
}
return $order;
}
但$changes 变量始终是一个空数组。
我尝试使用wp_insert_post_data Wordpress hook,但是当我设置时:
$data['post_status'] = "some status";
它只是阻止保存整个更新(整个新数据)。
这是我要运行的代码:
function($data){
if($data['order_status'] == 'comlpeted' && $data['new_order_status'] == 'proccessing'){
// prevent the order status from being changed
$data['new_order_status'] = $data['order_status'];
}
few more if conditions...
return $data;
}
感谢任何帮助或建议。
【问题讨论】:
-
问题“如何让某些用户将订单状态从特定订单状态更改为特定订单状态”?为什么“在mu-plugins中” | (这是什么意思)?
标签: php wordpress woocommerce hook-woocommerce orders