save_post_ 操作时 WooCommerce 订单状态不会更改
我正在开发一个 WooCommerce 插件,我在 save_post_shop_order
操作挂钩上保存额外的元数据。 现在我想添加一个逻辑,根据某些条件将订单状态更改为“待处理”。 我发现无论哪种状态,save_post_shop_order
操作挂钩中的订单状态都不会改变。
function save_order_data(int $post_id)
{
$nonce_name = isset($_POST['save_invoice_nonce']) ? $_POST['save_invoice_nonce'] : '';
$nonce_action = 'save_invoice';
if (!wp_verify_nonce($nonce_name, $nonce_action)) {
return;
}
if (!current_user_can('edit_shop_orders', $post_id)) {
return;
}
if (wp_is_post_autosave($post_id)) {
return;
}
if (wp_is_post_revision($post_id)) {
return;
}
$order = wc_get_order($post_id);
$order->update_status('pending'); // This command works but it seems order status is being overwritten maybe by WooCommerce to previous status
}
add_action('save_post_shop_order', 'save_order_data', PHP_INT_MAX);
I'm developing a WooCommerce plugin which I save extra metadata on save_post_shop_order
action hook.
Now I want to add a logic which changes order status to 'pending' based on some conditions.
I figured out that no matter which status, the order status doesn't change in save_post_shop_order
action hook.
function save_order_data(int $post_id)
{
$nonce_name = isset($_POST['save_invoice_nonce']) ? $_POST['save_invoice_nonce'] : '';
$nonce_action = 'save_invoice';
if (!wp_verify_nonce($nonce_name, $nonce_action)) {
return;
}
if (!current_user_can('edit_shop_orders', $post_id)) {
return;
}
if (wp_is_post_autosave($post_id)) {
return;
}
if (wp_is_post_revision($post_id)) {
return;
}
$order = wc_get_order($post_id);
$order->update_status('pending'); // This command works but it seems order status is being overwritten maybe by WooCommerce to previous status
}
add_action('save_post_shop_order', 'save_order_data', PHP_INT_MAX);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终不得不使用 Transients 来记住在下一页加载时进行状态更改,然后更改
woocommerce_after_register_post_type
挂钩上的订单状态。I finally had to use Transients to remember doing status change in next page load and then change order status on
woocommerce_after_register_post_type
hook.