Overcoming the Limitations of WooCommerce's Order Autocompletion
Normally, WooCommerce automates the completion of orders for virtual products. However, for payment methods such as "Bank wire," "Cash on delivery," and "Cheque," this functionality may not be available. To address this issue and conditionally apply order autocompletion based on payment methods, the following solutions can be implemented:
Using the woocommerce_payment_complete_order_status Filter Hook
This filter hook is triggered when a payment is required during checkout and is used by all payment methods. By modifying the allowed paid order statuses, you can achieve conditional autocompletion:
add_filter( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 ); function wc_auto_complete_paid_order( $status, $order_id, $order ) { return 'completed'; }
Utilizing the woocommerce_thankyou Action Hook
For WooCommerce versions 3 and above, the following code can be used:
add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 ); function wc_auto_complete_paid_order( $order_id ) { if ( ! $order_id ) return; $order = wc_get_order( $order_id ); if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) { return; } else { $order->update_status( 'completed' ); } }
Original Solution
For all WooCommerce versions, the following code can be used:
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 ); function custom_woocommerce_auto_complete_paid_order( $order_id ) { if ( ! $order_id ) return; $order = wc_get_order( $order_id ); if ( 'bacs' == get_post_meta($order_id, '_payment_method', true) ) || ( 'cod' == get_post_meta($order_id, '_payment_method', true) ) || ( 'cheque' == get_post_meta($order_id, '_payment_method', true) ) ) { return; } elseif( $order->get_status() === 'processing' ) { $order->update_status( 'completed' ); } }
These solutions provide conditional order autocompletion for paid orders based on the specified payment methods, ensuring efficient and accurate order processing in WooCommerce stores that accept a variety of payment options.
The above is the detailed content of How Can I Automate WooCommerce Order Completion for Specific Payment Methods?. For more information, please follow other related articles on the PHP Chinese website!