You can use the order complete hook, by adding it and some custom code to your functions.php, to execute some custom code whenever an order is complete (for eg when its paid) as well as use a similar hook for any WooCommerce Order Status. In this post I’ll teach you how.
// Table of Contents
- What is a Hook in WooCommerce (WordPress)?
- How many order related Hooks are there?
- How to use the ‘order complete’ hook specifically?
- How to use all the other order related status’s?
- Learn to customize WooCommerce Fully with an online Udemy course
// What is a Hook in WooCommerce (WordPress)?
A hook in WooCommerce is a way for you to ‘inject’ your custom code into an existing WooCommerce process (for eg an order process of payment, stock reduction, shipping etc) so you can do cool stuff like update an external stock system, send a follow up email in addition to the specific order email, interact with a shipping API. Anything your particular business model and website needs to do.
Hooks and Filters are how WordPress and WooCommerce allow you to customize what would otherwise be a static process which could not be changed without overwrite problems when you install an update to the WooCommerce Plugin.
You can view all of the WooCommerce Hooks and Filters here or view the Official WooCommerce Documentation here. These will, in combination with the below instructions, allow you to use any of the hooks available in WooCommerce not just the order related ones.
// How many order related Hooks are there?
In addition to the ‘order complete’ hook there is actually a hook for each of the possible WooCommerce Order Status’s.
You can view all of the possible Order Status’s here of view the image below.
So, for each of these status’s there is a PHP hook that you could use in order to do some custom code at various stages of the order process. Here is a list of all the Hooks related to Orders.
- woocommerce_order_status_pending
- woocommerce_order_status_failed
- woocommerce_order_status_on-hold
- woocommerce_order_status_processing
- woocommerce_order_status_completed
- woocommerce_order_status_refunded
- woocommerce_order_status_cancelled
// How to use the ‘order complete’ hook specifically?
So, let’s use one of these hooks, the ‘woocommerce_order_status_completed‘ by way of an example.
Step 1: Create a function full of custom code:
<?php
function doSomethingOnOrderComplete( $order_id ){
$order = wc_get_order( $order_id );
$user = $order->get_user();
if( $user ){
// do something with the user
}
}
Step 2: Add it to our desired Hook (ie Order Complete Hook):
<?php
// previous code block goes here
add_action( 'woocommerce_order_status_completed', 'doSomethingOnOrderComplete' );
Step 3: Add to functions.php and test an order:
Find your functions.php in your WordPress site, at something like, https://yoursite.com/wp-content/themes/yourtheme/functions.php, and add the complete code to it.
<?php
function doSomethingOnOrderComplete( $order_id ){
$order = wc_get_order( $order_id );
$user = $order->get_user();
if( $user ){
// do something with the user
}
}
add_action( 'woocommerce_order_status_completed', 'doSomethingOnOrderComplete' );
If you then process an order and update it to complete you will see that your custom code has fired.
The way I tested this was to,
- Add ‘cash as payment’ in the WooCommerce Settings at, https://yoursite.co.nz/wp-admin/admin.php?page=wc-settings&tab=checkout
- Then go and place an order in your site and process it as ‘cash on delivery’. This will mean that the order will be ‘processing’ in the system.
- Then go to your URL similar to this, https://yoursite.com/wp-admin/edit.php?post_type=shop_order, and update the order you just made to complete.
- At this point you can check that your custom code, whatever it is, has been processed.
// How to use all the other order related status’s?
You can do the exact same thing as above for each and every of the WooCommerce Order Status Hooks. So, for example you could do something like this for a ‘failed’ order.
<?php
// changed the function name
function doSomethingOnOrderFailed( $order_id ){
$order = wc_get_order( $order_id );
// send an order to the admin to alert them to a failed order, for eg
}
// changed the name of the hook to use
add_action( 'woocommerce_order_status_failed', 'doSomethingOnOrderFailed' );
// Learn to customize WooCommerce Fully with an online Udemy course
If you’d like to know more about how to customize WooCommerce completely check out these courses from Udemy which are low cost, online and have been completed by thousands of people.