How to change the default order status on WooCommerce

How to change the default order status on WooCommerce

Sometimes, while developing an e-commerce website, you’ll need to automatically check the payment status and mark the order status as “Completed”, “Pending” or as per your requirement. A default order status is set on the basis of payment done. The Front-end users can check their order’s status in their Account History page. In this blog we will learn how to change the default order status on Woo Commerce as “Invoice to be printed” step by step.

Step 1: Create a custom plugin

How to change the default order status on WooCommerce

To create a custom plugin on your website follow the following steps:

1. Navigate to the Word Press plugins folder, in our case it is app/plugins. Create a folder as “customstatus”.

Important Note:  Give your folder a unique name (Word press default plugins name should not conflict with your plugin name) using lowercase letters and dashes.

2. Now create the main PHP file of your plugin. To do so, create a PHP file within your plugin folder and give it the same name as the folder name. In our case, the folder name is “customstatus” so the file name will be customstatus.php.

3. Set up your plugin’s information. Just copy paste the below code in customstatus.php file.

/**
 * Plugin Name: My First Plugin
 * Plugin URI: https://www.mywebsite.com/my-first-plugin
 * Description: The very first plugin that I have ever created.
 * Version: 1.0
 * Author: Your Name
 * Author URI: https://www.mywebsite.com
 */

Note: Edit the details such as Author Name, Plugin Name, Description, etc. as per your requirement.

4. That’s it! You’ve just completed the minimum number of steps that are required to create a Word Press plugin. You can now activate it from the Plugins tab in Word Press admin.

Step 2: Add Code in your custom plugin

How to change the default order status on WooCommerce

1. Now we have to create a function and hook it to a specific Action. Simply copy the below function in your customstatus.php.

2. Since we want to update the order status on complete payment, add an action to the ‘woocommerce_payment_complete’ hook as given below in the highlighted code.

//COPY THE WHOLE CODE IN customstatus.php FILE.

#<editor-fold defaultstate="collapsed" desc="Default Status as Invoice To Be Printed">
function action_woocommerce_payment_complete( $order_id ) {  
      if( ! $order_id ) return;
        $order = wc_get_order( $order_id );  
        $order->update_status( 'wc-print-invoice' ); //Order status will be updated to “Invoice To Be Printed”.
};  
          
// add the action  
add_action( 'woocommerce_payment_complete', 'action_woocommerce_payment_complete', 10, 3 );  
#</editor-fold>

3. Just Save your customstatus.php file and it’s done. From now on, the order status will be updated to “Invoice to Be Printed” when the payment is complete.

USEFUL NOTE: If you want to update the order status when “Thank You” or “Checkout Success” page is loaded you can add the action on “woocommerce_thankyou” hook. In customstatus.php replace
add_action( ‘woocommerce_payment_complete’, ‘action_woocommerce_payment_complete’, 10, 3 ); with add_action( ‘woocommerce_thankyou’, ‘ggb_custom_change_order_status’ ,10,3);

You can reach us at support@knowband.com for any queries regarding Woocommerce. We provide customized changes on Woocommerce and also specific modules to help you sell better.

One of the most popular module for Woocomerce is Spin And Win. This module has a proven record of generating sales and boosting conversion rate. See what client’s are saying about this module:

spin and win review

 

Check the following video for more information:

Aparajita Singh

Aparajita Singh

Aparajita Singh is an experienced Software engineer in PHP. She also has knowledge of C, JAVA and database design. She has been working in the IT Industry from the last 2yrs and still looking forward to achieving more in the IT industry. She lives in New Delhi and her hobby is to write the technical writeups.

Leave a Reply

Your email address will not be published. Required fields are marked *