WooCommerce Hooks: Actions and Filters

Introduction: What are WooCommerce Hooks? Hooks in WordPress essentially allow you to change or add code without editing the core files. Extensively used throughout WordPress and WooCommerce and are very useful for developers.

There are two types of hook: actions and filters.

Action Hooks allow you to insert custom code at various points (wherever the hook is run).
Filter Hooks allow you to manipulate and return a variable which it passes (for instance a product price).
There is an excellent article on hooks and filters here.

Using hooks
If you use a hook to add or manipulate code, you can add your custom code to your theme’s functions.php file.

Using action hooks to execute your own code, you hook in by using the action hook

do_action('action_name');

Here is where you need to place your code:

add_action( 'action_name', 'your_function_name' );
function your_function_name() {
// Your code
}

 

Using filter hooks:
Filter hooks are called throughout are code using

apply_filter( 'filter_name', $variable );

 

To manipulate the passed variable, you can do something like the following:

add_filter( 'filter_name', 'your_function_name' );

function your_function_name( $variable ) {
// Your code
return $variable;
}

With filters, you must return a value. To learn more about options for using hooks and filters see our Snippet doc section.

API Documentation

For a comprehensive view of the WooCommerce API, see the API Documentation.

Available WooCommerce action hooks and filters

For details on what the action hooks and filters do, reference the WooCommerce Hooks

Comments are held in moderation before posting. Please be patient with me as I am very busy. I don't allow any code to be added. I'll update the comment section soon to allow it though. All spam comments will be filtered out by WordPress automation. So don't even bother you little bot you.

Leave a Reply