Autocompletion of Paid Orders in WooCommerce for Specific Payment Methods
WooCommerce generally autocompletes orders for virtual products. However, certain payment methods, such as "Bank wire," "Cash on delivery," and "Cheque," require a conditional approach.
woocommerce_payment_complete_order_status Filter Hook
The optimal solution for this issue is to leverage the woocommerce_payment_complete_order_status filter hook, which is used by all payment methods when a payment is required at checkout.
Conditional Code Using the Filter Hook
The following code utilizes the filter hook and conditionally applies the status change based on payment method:
add_filter('woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3); function wc_auto_complete_paid_order($status, $order_id, $order) { // Check if the payment method is not "Bank wire," "Cash on delivery," or "Cheque." if (!in_array($order->get_payment_method(), array('bacs', 'cod', 'cheque'))) { // If not, change the order status to "completed." return 'completed'; } // Otherwise, do not modify the order status. return $status; }
By using this filter hook, you can effectively and accurately update the status of paid orders based on payment methods without triggering multiple notifications to customers.
Alternatives
Code Snippet:
If the payment methods you want to target are limited to those that do not trigger auto-completion by default ("Bank wire," "Cash on delivery," and "Cheque"), you can use the following code:
add_action('woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1); function custom_woocommerce_auto_complete_paid_order($order_id) { $order = wc_get_order($order_id); // Check if the payment method is not "Bank wire," "Cash on delivery," or "Cheque." if (!in_array($order->get_payment_method(), array('bacs', 'cod', 'cheque'))) { // If not, update the order status to "completed." $order->update_status('completed'); } }
Plugin:
The "WooCommerce Autocomplete Orders" plugin can also provide a solution for specific payment methods. However, it may not work with certain Credit Card gateways.
The above is the detailed content of How to Automate WooCommerce Order Completion for Specific Payment Methods?. For more information, please follow other related articles on the PHP Chinese website!