To automatically set the complete order status in WooCommerce, you can use hooks and filters provided by the WooCommerce plugin. This solution also reduces admin overhead because your staff wont have to login and update each order with the Completed order status – it’s done automatically when the thank you page is triggered. Here’s an example of how you can achieve this:
Open your theme’s functions.php
file or create a custom plugin for this purpose.
/**
* Automatically set complete order status.
*
* @param int $order_id The order ID.
*/
function set_complete_order_status( $order_id ) {
// Get the order object
$order = wc_get_order( $order_id );
// Check if the order is already completed
if ( $order->has_status( 'completed' ) ) {
return;
}
// Update the order status to completed
$order->update_status( 'completed' );
}
add_action( 'woocommerce_order_status_processing', 'set_complete_order_status', 10, 1 );
add_action( 'woocommerce_order_status_completed', 'set_complete_order_status', 10, 1 );
This code defines a function set_complete_order_status
that will be triggered when an order status changes to either “processing” or “completed.” Inside the function
Save the changes to the functions.php
file or activate the custom plugin.
You can modify the code according to your specific requirements by adjusting the hook or adding conditions to check for other statuses if needed.
Leave a Review