WooCommerce – Automatically set order status to completed after payment is received

Sometimes you’ll need to automatically check the payment status and mark an order as completed.

This usually happens for orders that have “virtual products” in them and these are not marked completed by default in WooCommerce.

To make all orders automatically completed I’ve used the code below.

Using this hook will allow you to set this behavior to all payment methods at once, but if you wish you could also check the payment method from the order, and filter the change accordingly.

add_filter( 'woocommerce_payment_complete_order_status', 'update_order_status_to_completed', 10, 2 );
 
function update_order_status_to_completed( $order_status, $order_id ) {
 
$order = new WC_Order( $order_id );
 
 if ( 'processing' == $order_status && ( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) {

 return 'completed';
 
 }
 return $order_status;
}
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 3.33 out of 5)
Loading...Loading...
balter-verdell swiney.bernie