Last updated on October 18, 2023

WooCommerce order status not updating

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Fix WooCommerce order status update problems.

The code snippet provided below can be used to troubleshoot and fix the issue of WooCommerce order status not updating. This issue can occur due to various reasons, such as conflicts with other plugins or themes, incorrect settings, or custom code interfering with the order status update process.

To address this issue, you can use the following code snippet as a starting point:

/**
 * Fix WooCommerce order status not updating.
 */
function wpsnippets_fix_order_status_update_issue( $order_id ) {
    $order = wc_get_order( $order_id );

    // Update the order status manually
    $order->update_status( 'processing' );
}
add_action( 'woocommerce_order_status_pending', 'wpsnippets_fix_order_status_update_issue', 10, 1 );

This code snippet creates a custom function wpsnippets_fix_order_status_update_issue that hooks into the woocommerce_order_status_pending action. When an order’s status is set to “pending”, this function will be triggered.

Inside the function, we retrieve the order object using the provided $order_id parameter. Then, we manually update the order status to “processing” using the update_status() method of the order object.

By using this code snippet, you can ensure that the order status is updated to “processing” whenever an order is set to “pending”. You can modify the status value as per your requirements.

Please note that this is a basic example, and you may need to customize it further based on your specific needs and the root cause of the order status update issue.

Examples

Example 1: Manually updating WooCommerce order status

This use case demonstrates how to manually update the order status in WooCommerce using the wp_update_post() function.

$order_id = 123;
$new_status = 'completed';

$order_data = array(
    'ID' => $order_id,
    'post_status' => $new_status,
);

wp_update_post( $order_data );

In this code example, we use the wp_update_post() function to update the post status of a WooCommerce order. We pass an array containing the order ID and the new status to the function. This allows us to programmatically change the order status to any desired value.

Example 2: Updating WooCommerce order status based on custom conditions

This use case demonstrates how to update the order status in WooCommerce based on custom conditions using the woocommerce_order_status_changed hook.

function wpsnippets_update_order_status( $order_id, $old_status, $new_status ) {
    if ( $new_status === 'processing' ) {
        // Custom condition to update order status
        $new_status = 'completed';
    }

    $order_data = array(
        'ID' => $order_id,
        'post_status' => $new_status,
    );

    wp_update_post( $order_data );
}
add_action( 'woocommerce_order_status_changed', 'wpsnippets_update_order_status', 10, 3 );

In this code example, we use the woocommerce_order_status_changed hook to trigger a custom function when the order status changes. Inside the function, we can add custom conditions to determine the new order status. In this case, if the new status is ‘processing’, we update it to ‘completed’ using the wp_update_post() function.

Example 3: Updating WooCommerce order status programmatically after a specific event

This use case demonstrates how to update the order status in WooCommerce programmatically after a specific event using a custom function and a scheduled event.

function wpsnippets_update_order_status_after_event() {
    $order_id = 123;
    $new_status = 'completed';

    $order_data = array(
        'ID' => $order_id,
        'post_status' => $new_status,
    );

    wp_update_post( $order_data );
}

// Schedule the event to run after a specific time
$event_time = strtotime( '+1 hour' );
wp_schedule_single_event( $event_time, 'wpsnippets_update_order_status_event' );

// Hook the function to the scheduled event
add_action( 'wpsnippets_update_order_status_event', 'wpsnippets_update_order_status_after_event' );

In this code example, we define a custom function wpsnippets_update_order_status_after_event() that updates the order status to ‘completed’. We then schedule a single event using wp_schedule_single_event() to run the function after a specific time (in this case, 1 hour from the current time). Finally, we hook the function to the scheduled event using add_action(). This allows us to update the order status programmatically after a specific event occurs.

Last updated on October 18, 2023. Originally posted on November 7, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *