How to set woocommerce order status based on payment gateway?

There is a woocommerce documentation to automatically complete orders. You could also change the “Completed” status to be another order status, like “Processing”. The function is following

/**
 * Auto Complete all WooCommerce orders.
 */
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
}

Add code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Avoid adding custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme.

One of my clients asked yesterday that they want to automatically complete order for paypal and bitcoin but not for bank transfer since bank transfer take 3-5 days to complete the transactions. so i modified the above code and sharing here if it help anybody. The modified code is following:

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }
         $payment_title = get_post_meta( $order_id, '_payment_method_title', true );

if($payment_title == 'PayPal'){

    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );

} else if($payment_title == 'Bitcoin'){
    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
}
}

Change the payment title to your real title. If you need any help, feel free to comment or contact us.

Leave a Reply

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