Explore how WooCommerce hooks and filters allow you to tailor your online store. Dive into actionable examples and tips for creating scalable customizations without editing core files.
You want to change how WooCommerce behaves, but you know better than to edit the plugin’s core files. Good instinct. The next update would wipe those edits, and you’d be back where you started. Hooks and filters are the way out. They let you plug your own code into WooCommerce at set points, so your customizations ride along through every update.
This guide walks through both, with working examples you can drop into a site plugin or your theme’s functions file. We’ll cover where each one fits, where they bite, and how to keep the code maintainable.
Table of Contents
- Introduction to WooCommerce Hooks and Filters
- Using WooCommerce Hooks
- Using WooCommerce Filters
- Creating Custom Hooks
- Real-World Examples
- Best Practices
- Conclusion
Introduction to WooCommerce Hooks and Filters
WooCommerce inherits the hook system from WordPress, so if you’ve used WordPress hooks, this will feel familiar. There are two kinds, and the difference is simple: actions let you run code at a certain point, filters let you change a value on its way through. Both fire without you touching a single core file.
Hooks
Action hooks run your function at a specific moment in a WooCommerce process. A couple you’ll reach for often:
woocommerce_before_cart: Fires before the cart is displayed.woocommerce_thankyou: Runs on the order-received page after an order is placed.
Filters
Filters hand you a value, let you modify it, and expect you to return it. Miss the return and you’ll blank out whatever you were filtering, so watch for that. Two examples:
woocommerce_product_get_price: Modify a product’s price as it’s read.woocommerce_email_subject_new_order: Adjust the subject line of the new-order notification email.
Using WooCommerce Hooks
Adding functionality with an action hook comes down to two steps.
1. Identify the Hook
Find the right spot. The WooCommerce Code Reference lists every hook, and Query Monitor will show you which hooks fire on a given page in real time. That second option saves a lot of guessing.
2. Attach a Callback Function
Hang your function on the hook with add_action(). WooCommerce calls it when the moment arrives.
Example of the code
<?php
/**
* Add a custom thank-you message after order completion.
*/
add_action('woocommerce_thankyou', 'custom_thank_you_message');
function custom_thank_you_message() {
echo '<p>Thank you for shopping with us! We hope to see you again soon.</p>';
}Using WooCommerce Filters
To change data on the fly, use add_filter(). Filters are the tool for prices, button text, email fields, and other values WooCommerce hands you before it uses them.
One honest caveat on the example below: woocommerce_product_get_price runs every single time a product’s price is read, which on a busy shop is a lot. Keep the callback cheap, guard it tightly (as the ID check does here), and don’t do database work or API calls inside it. Same goes for anything you attach to cart and checkout hooks, since heavy logic there is felt on every page load.
Example of the code
<?php
/**
* Adjust product price dynamically.
*
* @param float $price The original price.
* @return float Modified price.
*/
add_filter('woocommerce_product_get_price', 'dynamic_price_adjustment', 10, 2);
function dynamic_price_adjustment($price, $product) {
// Example: Apply a 10% discount on a specific product ID.
if ($product->get_id() === 123) {
$price *= 0.9;
}
return $price;
}Creating Custom Hooks
Sometimes WooCommerce doesn’t expose a hook where you need one. You can add your own with do_action(), which is handy when you’re building something others (or future you) will extend.
1. Define the Hook
Call do_action() at the point you want to open up, inside your theme or plugin:
Example of the code
<?php
/**
* Custom hook to run after a product is added to the cart.
*/
function custom_hook_add_to_cart() {
do_action('custom_after_add_to_cart');
}
add_action('woocommerce_add_to_cart', 'custom_hook_add_to_cart');2. Attach Functions to the Hook
Now anything can listen for custom_after_add_to_cart, and you can attach as many callbacks as you like without them stepping on each other:
Example of the code
<?php
/**
* Display a custom message after adding a product to the cart.
*/
add_action('custom_after_add_to_cart', function() {
echo '<p>Special Offer: Get 10% off your next purchase!</p>';
});Real-World Examples
Here are three things people actually ask for, each a few lines of code.
1. Displaying Custom Messages
<?php
/**
* Add a promotion banner to the cart page.
*/
add_action('woocommerce_before_cart', function() {
echo '<div class="promo-banner">Free shipping on orders over $50!</div>';
});2. Modifying Add-to-Cart Button Text
<?php
/**
* Change the text of the add-to-cart button.
*/
add_filter('woocommerce_product_single_add_to_cart_text', function() {
return 'Add to Basket';
});3. Customizing Email Subjects
<?php
/**
* Change the subject of new order notification emails.
*
* @param string $subject The default subject.
* @param WC_Order $order The order object.
* @return string Modified subject.
*/
add_filter('woocommerce_email_subject_new_order', function($subject, $order) {
return 'New Order: ' . $order->get_order_number();
}, 10, 2);Best Practices
- Document Custom Code: Leave a comment on every hook and filter saying what it does and why. Future you will thank you.
- Use Unique Names: Prefix custom hook and function names to avoid clashing with another plugin’s code.
- Test on Staging: Try every customization on a staging site before it touches production, especially anything on cart or checkout.
- Use Priority When Order Matters: The priority argument in
add_action()andadd_filter()controls what runs first when several callbacks share a hook.
Conclusion
Hooks and filters are how you make WooCommerce your own without fighting the next update. Actions run your code, filters change a value, and custom hooks let you open up your own extension points. That’s most of what you need.
Start small: one hook, tested on staging, documented in a line of comment. Build from there, keep the callbacks light on cart and checkout, and your customizations will hold up as the shop and WooCommerce both grow.


