WooCommerce: Auto complete paid orders

Cover Image for WooCommerce: Auto complete paid orders
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

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! 🛍️


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello