Call PHP function after WooCommerce order completed using woocommerce_order_status_completed hook

By Faruque Ahamed Mollick

WooCommerce is an extendable WordPress plugin to start an eCommerce store. In this post, I am going to tell you how to call PHP function after WooCommerce order completed.

WooCommerce already contains lots of its own action hooks and filter hooks so that developers can extend the features and functionality of this plugin programmatically. Using these hooks, you can develop add-ons for WooCommerce.

There is an action hook available for WooCommercethat can call a function after order status completed. The action hook is woocommerce_order_status_completed which is one of the most useful action hooks to develop add-ons for WooCommerce.

Using woocommerce_order_status_completed action hook we can easily call PHP function after WooCommerce order completed.

Let’s see the code snippet.

First of all, create our custom PHP function:

// define woocommerce_order_status_completed callback function
function call_order_status_completed( $array ) { 
    // Write your code here
};

And then call it with the action hook woocommerce_order_status_completed:



add_action( 'woocommerce_order_status_completed', 'call_order_status_completed', 10, 1);

Complete code to call PHP function after WooCommerce order completed

So below is our complete example code:

// define woocommerce_order_status_completed callback function
function call_order_status_completed( $array ) { 
    // Write your code here
}; 

// Call our custom function with the action hook
add_action( 'woocommerce_order_status_completed', 'call_order_status_completed', 10, 1);

You can see in the above code that we have first created our function and then take the function name as the parameter inside our action hook. This is the rules of using the action hook on WordPress.

 

Also, read:

 

Inside the custom function, you can put your required code that you need. Here you can use several WooCommerce data like order ID, each and every field of checkout that is provided by the customers etc.

You can use the code in this post in your sitewide plugin or in your theme’s functions.php. If you are going to develop an add-on that works with WooCommerce, in that case, you should put the code in the sitewide plugin.

So we have learned how to call our custom PHP function after the order status on WooCommerce store become completed.

I hope this post be useful to you and help you in developing WordPress.

Leave a Reply

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