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);
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.