Woocoomerce hooks for order cheatsheet

Certainly, Woocommerce is a powerful tool that provides a wide range of e-commerce robust functions and these core functions use a proper life cycle.
We can extend the e-commerce app using woo-commerce hooks.
The Hooks are actions and filters that help to change the data using the filter hook and add your custom function using the action hook.
Here’s a WooCommerce order hooks cheat sheet that provides some common hooks and their descriptions. WooCommerce is a popular e-commerce plugin for WordPress, and hooks allow you to customize and extend its functionality by executing custom code at specific points in the order processing lifecycle.

 

woocommerce_new_order:
Description: Triggered when a new order is created.
Example Use Case: Sending notifications or performing custom actions when a new order is placed.
 add_action( 'woocommerce_new_order', '_call_after_new_order',  99, 2  );
function _call_after_new_order( $order_id, $order ) {
// add logic with $order_id and $order object
}
woocommerce_order_status_changed:
Description: Triggered when the order status changes.
Example Use Case: Sending notifications or performing actions based on order status changes (e.g., processing to completed).
add_action( 'woocommerce_order_status_changed', '_call_after_order_status_change', 10, 3 );

/**
* Function for `woocommerce_order_status_changed` action-hook.
*
* @param $id
* @param $status_transition_from
* @param $status_transition_to
*
* @return void
*/
function _call_after_order_status_change( $id, $status_transition_from, $status_transition_to){

// action...
}
woocommerce_order_status_{status}:
Description: Triggered when an order transitions to a specific status.
Example Use Case: Performing actions specific to a particular order status (e.g., on-hold, processing, completed).
woocommerce_order_status_completed:
Description: Triggered when an order is marked as completed.
Example Use Case: Fulfillment, inventory management, or sending follow-up emails.
woocommerce_order_status_processing:
Description: Triggered when an order is marked as processing.
Example Use Case: Inventory updates, order processing, or custom notifications.
woocommerce_order_status_pending:
Description: Triggered when an order is pending payment.
Example Use Case: Handling payment gateways, sending payment reminders.
woocommerce_order_status_cancelled:
Description: Triggered when an order is canceled.
Example Use Case: Restoring inventory, updating records, sending cancellation notifications.
woocommerce_order_status_on-hold:
Description: Triggered when an order is on-hold.
Example Use Case: Sending on-hold notifications.
function _call_after_status_changed( $order_id ){
$order = new WC_Order( $order_id );
//Now add your logic
}

add_action( 'woocommerce_order_status_completed', '_call_after_status_changed');
add_action( 'woocommerce_order_status_pending', '_call_after_status_changed');
add_action( 'woocommerce_order_status_cancelled', '_call_after_status_changed');
add_action( 'woocommerce_order_status_processing', '_call_after_status_changed');
add_action( 'woocommerce_order_status_on-hold', '_call_after_status_changed');

Please note: Here I added all actions related to the status change you can copy only one that is related to your requirements.

 

woocommerce_pre_payment_complete:

Description: Executed at the very beginning of payment.

Example Use Case: Applies at the very beginning and doesn’t depend on order status

woocommerce_payment_complete:
Description: Executed when a payment for an order is completed.
Example Use Case: Unlocking access to digital products, sending order completion emails.
add_action( 'woocommerce_pre_payment_complete', '_call_payment_fnc' );
add_action( 'woocommerce_payment_complete', '_call_payment_fnc' );

function _call_payment_fnc( $order_id ) {

$order = wc_get_order( $order_id );
// get the order data and add your logic

}
woocommerce_order_item_meta_end:
Description: Executed at the end of displaying order item meta data.
Example Use Case: Adding custom information to display alongside order items in emails or on the order page.
add_action('woocommerce_order_item_meta_end', 'add_custom_info_in_order_items', 10, 4);

function add_custom_info_in_order_items($item_id, $item, $order, $plain_text) {

// This will display in order page and email template

echo '<div> Custom information: '. wc_get_order_item_meta( $item_id, 'your custom field id') .'</div>';

}
These are just a few examples of WooCommerce order hooks. To use them, you’ll need to add custom code to your WordPress theme’s functions.php file or use a custom plugin. Be cautious when working with hooks, as they involve direct manipulation of the WooCommerce order data and can affect your store’s functionality. Always test your custom code thoroughly before deploying it to a live website.
Thanks

 

Related Blog

Sign up for our newsletter to stay up to
date with tech news!