WooCommerce: Auto complete paid orders
WooCommerce: Auto complete paid orders
Are you struggling with WooCommerce orders not auto-completing for virtual products? 😩 Don't worry, you're not alone. This issue can be quite frustrating, and it seems like a bug rather than a feature, right? But fear not, we have some helpful solutions that will make your life easier! 💪
Solution 1: The Snippet Code
One common solution is to use a snippet code provided by WooCommerce. 🙌 It's a simple piece of code that you can add to your website's functions.php file. Here's the code:
/**
* 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' );
}
This snippet should auto-complete orders for virtual products, but there's a catch. It doesn't work for payment methods like BACS, Pay on Delivery, and Cheque. 😕 But hey, it works perfectly fine for Paypal and other Credit Card gateways payment methods. So if that's what you're using, this snippet might be the perfect solution for you!
Solution 2: The Plugin
If the snippet code doesn't cover all your payment methods, don't worry; there's another solution! 🎉 You can use a plugin called WooCommerce Autocomplete Orders. This plugin automates the process of completing orders for all payment methods, including BACS, Pay on Delivery, Cheque, and even the Credit Card gateways payment methods.
So if you want a hassle-free solution that works with any payment method, this plugin is definitely worth considering! 💡
Implementing Conditional Code for Specific Payment Methods
Now, let's answer the question you had about implementing conditional code based on WooCommerce payment methods. Let's say you only want to apply the snippet code for virtual products when the payment method is not BACS, Pay on Delivery, or Cheque. 🤔 Here's how you can achieve that:
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 );
$payment_method = $order->get_payment_method();
$excluded_payment_methods = array( 'bacs', 'cod', 'cheque' );
if ( ! in_array( $payment_method, $excluded_payment_methods ) ) {
$order->update_status( 'completed' );
}
}
In this modified code, we added a few extra lines to check the payment method and exclude the ones we don't want to apply the snippet code to. Just replace the $excluded_payment_methods
array with the payment method slugs you want to exclude.
Conclusion
Auto-completing paid orders for virtual products in WooCommerce doesn't have to be a headache anymore! With the snippet code or the WooCommerce Autocomplete Orders plugin, you have the power to automate this process and focus on more important aspects of your business.
So go ahead, try out these solutions, and let us know which one works best for you! And if you have any questions or need further assistance, don't hesitate to reach out. Happy selling! 🛍️