我正在开发一个 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);