How to Implement Conditional Autocompletion for Paid WooCommerce Orders
In WooCommerce, virtual products often encounter issues with automatic order completion. This comprehensive guide provides multiple solutions to address this problem, including a custom code snippet and plugin options. For a more granular approach, consider implementing conditional code based on WooCommerce payment methods.
Conditional Code for Autocompleting Paid Orders
To selectively apply the autocompletion feature based on payment methods, utilize the woocommerce_payment_complete_order_status filter hook, which is triggered when a payment is required in checkout. Here's an improved version compatible with WooCommerce 3 and above:
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'; }
This code changes the allowed paid order status to "completed" for all payment gateways except "Bank wire" (bacs), "Cash on delivery" (cod), and "Cheque" (cheque).
Additional Considerations
Alternatives
Improved version (2018)
add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 ); function wc_auto_complete_paid_order( $order_id ) { // No updates for Bank wire, Cash on delivery, and Cheque if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) { return; } // Autocomplete all others else { $order->update_status( 'completed' ); } }
Original answer (all WooCommerce versions)
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 ); function custom_woocommerce_auto_complete_paid_order( $order_id ) { // No updates for Bank wire, Cash on delivery, and Cheque 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; } // Autocomplete all others else { $order->update_status( 'completed' ); } }
Note: Be sure to place the code snippets in the functions.php file of your child theme.
The above is the detailed content of How to Automatically Complete Paid WooCommerce Orders Based on Payment Method?. For more information, please follow other related articles on the PHP Chinese website!