🚀 Is building a website too difficult? Let me guide you step by step—Learn about the 「Naibabiji WordPress Website Building Coaching Service」 →

Method to Automatically Complete WooCommerce Orders for Automatic Fulfillment

Yesterday, a friend consulted Naiba. He usedWooCommerce Automatic Serial Key Delivery FunctionFailed. After payment, the status displayed is pending. The administrator needs to manually click 'Send' in the backend for the system to send an email to the customer with the serial number. Actually, the reason for this issue is simple: WooCommerce's workflow is designed so that after a user pays, the default status is 'Processing'. The order is only completed after the administrator processes it (because it involves situations like shipping). However, don't worry. Naiba here sharestwo effective methods to achieve automatic order completion.

Method 1: Using a Plugin

We use the 'Autocomplete WooCommerce Orders' plugin to achieve automatic order completion. 1. Search for 'Autocomplete WooCommerce Orders' in the backend or click the link below to download and install the plugin.Download Link2. Activate the plugin and enter the WooCommerce settings interface. 3. Set and save at the 'Autocomplete Orders' section.https://wordpress.org/plugins/autocomplete-woocommerce-orders/'Autocomplete WooCommerce Orders' supports separate settings for whether to automatically complete physical orders and virtual orders. It is also compatible with most payment gateways, so you can use it with confidence. If you are not satisfied with this free plugin, you can also trythe official paid plugin.

Method Two: Using Code

1. Add the following code to your website's theme functions file. If you don't know how to add it, you can use:Safe Method to Add Code to the functions.php File: Code Snippets
add_action('woocommerce_order_status_changed', 'ts_auto_complete_by_payment_method');

function ts_auto_complete_by_payment_method($order_id)
{

	if ( ! $order_id ) {
	return;
	}

	global $product;
	$order = wc_get_order( $order_id );

	if ($order->data['status'] == 'processing') {
	$payment_method=$order->get_payment_method();
		if ($payment_method!="cod")
		{
			$order->update_status( 'completed' );
		}

	}

}
This code calls thewoocommerce_order_status_changedhook to determine if the order is completed. As long as it's not a Cash on Delivery order, it will be automatically marked as completed after payment is received. WooCommerce default payment method IDs (you can modify the code above yourself): Direct bank transfer (bacs), Check payments (cheque), Cash on delivery (cod), and PayPal (paypal). 2. The following code checks if it's a virtual product. If it is a virtual product, it automatically completes the order.
add_action('woocommerce_order_status_changed', 'ts_auto_complete_virtual');

function ts_auto_complete_virtual($order_id)
{

	if ( ! $order_id ) {
		return;
	}

	global $product;
	$order = wc_get_order( $order_id );

	if ($order->data['status'] == 'processing') {

		$virtual_order = null;

		if ( count( $order->get_items() ) > 0 ) {

			foreach( $order->get_items() as $item ) {

				if ( 'line_item' == $item['type'] ) {

					$_product = $order->get_product_from_item( $item );

					if ( ! $_product->is_virtual() ) {
						// once we find one non-virtual product, break out of the loop
						$virtual_order = false;
						break;
					}
					else {
					$virtual_order = true;
					}
				}
			}
		}

		// if all are virtual products, mark as completed
		if ( $virtual_order ) {
		$order->update_status( 'completed' );
		}
	}
}
Alright, the above are the methods shared by Naiba for WooCommerce automatic order completion. Hope it helps you.

🚀 Still feeling confused after reading the tutorial? Let me guide you step-by-step.

「Naibabiji WordPress Website Building Coaching Service」—From choosing a domain and buying hosting, to installing a Theme and publishing content, I「ll coach you through every step, helping you avoid detours and reach your goal directly.

👉 Learn about the Website Building Coaching Service
🔒

Comments are closed

The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.

×
二维码

Scan QR Code to Follow

AI Website Building Assistant

🤖
Hello! I am the Naibabiji AI Assistant. How can I help you?
Quick Consultation: