How to Auto Approve Orders in WooCommerce

Auto Approve Orders WooCommerceIf you are running an eCommerce store, then you are aware that the site requires tracking and managing orders. However, this can be a strenuous job for the administrator, especially if your online store gathers high-traffic, which results in a large number of orders in a day.

Additionally, you know that an order is created as soon as the Checkout process is completed. An order is assigned a status according to where the order is in the payment-to-delivery chain.

Moreover, statuses are set or changed by WooCommerce, the Payment Gateway, and the store administrator depending on where the order is initiated.

After payment for an order is completed if the user has not selected the Cash-on-Delivery option. When an order is completed, its status is set to “Processing” until the store owner manually changes it to “Completed”. This will be a daunting task for the store owner or the administrator.

Auto Approve Orders WooCommerce

In this brief tutorial, I will share with you a custom PHP code that will help you to automatically set the status of the order as Completed whenever a payment has been successfully made. This implies that even if the user selects the Cash-on-Delivery option, the order status will automatically change to Completed.

Moreover, I will also share with you a solution that will automatically set the WooCommerce order status as Completed for virtual products. These products are not tangible or downloadable.

This is because membership subscription may not have a tangible form but instead an expiry date. If you deal with downloadable products, the Cash-on-Delivery option should not be included, but instead, only a condition should be added to check if all the products in the order are virtual.

Now that you know how the order statuses are set, then here is a detailed guide on how to:

  • Automatically set the WooCommerce order status as Completed based on the payment method.
  • Automatically set the WooCommerce order status as Completed for virtual products.

a)    Steps to Automatically Set the WooCommerce Order Status as Completed Based on Payment Method

Here are the simple steps that you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will automatically set the woocommerce order status as completed based on the payment method.
  3. Add the following code to the php file:
/**

Snippet to automatically set the woocommerce order status as completed based on the payment method

*/

add_action('woocommerce_order_status_changed', 'njengah_auto_complete_by_payment_method');

function njengah_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!="bacs")

{

$order->update_status( 'completed' );

}

}

}
  1. To see the outcome, head over to the WordPress Dashboard and click on WooCommerce > Orders. You will see that the order has been set to Completed as shown below:approved order

How the Code Works

In the code above, I have used the woocommerce_order_status_changed hook to call a function when the status of an order is changed. It checks whether the order has the Processing status and changes it to Completed.

By default, WooCommerce has four default payment methods, which are Direct Bank Transfer (bacs), Check payments (cheque), Cash on Delivery (cod), and PayPal (PayPal). However, you need to specify the payment method you are setting the condition for.

b)    Steps to Automatically Set the WooCommerce Order Status as Completed for Virtual Products

downloadable product : Auto Approve Orders WooCommerceHere are the steps that you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will automatically set the woocommerce order status as completed for virtual products.
  3. Add the following code to the php file:
/**

Snippet to automatically set the woocommerce order status as completed for virtual products

*/

add_action('woocommerce_order_status_changed', 'njengah_auto_complete_virtual');

function njengah_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' );

}

}

}

 

  1. The outcome will be the same as for the first example, as the order will be automatically set as Completed.

How the Code Works

In the code above, I have used the same hook to call the function njengah_auto_complete_virtual. After it has checked if the is in a Processing status, then the code goes through each item in the order to see if there is a virtual product.

The line_item refers to the product, and only the products need to be fetched one by one to check if they are virtual. If there is no virtual product, the status of the order will not be marked as Completed.

Conclusion

In this brief tutorial, I have shared how you can automatically set the WooCommerce order status as Completed. I have done it based on the payment method and for virtual products.

However, depending on the requirements you need for your store, using the first code snippet, you may add more conditions to mark the order status as Completed. For instance, if payment has been made, you can mark Order Status as Completed even for non-virtual products by adding a check on the payment method after it checks for whether it is a virtual product.

However, through this code snippet, the status of the order will be set as Completed only if all products in the order are virtual products. I hope that this post provided a solution to automatically approve orders in WooCommerce.

Similar Articles

Comments are closed.