WooCommerce Hooks: Actions and filters

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

Introduction: What are hooks?

↑ Back to top

Hooks in WordPress essentially allow you to change or add code without editing core files. They are used extensively 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).

Note: This is a Developer level documentation. We are unable to provide support for customizations under our Support Policy. If you are unfamiliar with code/templates and resolving potential conflicts, select a WooExpert or Developer for assistance.

Using hooks

↑ Back to top

If you use a hook to add or manipulate code, you can add your custom code in a variety of ways:

Using action hooks

↑ Back to top

To execute your own code, you hook in by using the action hook do_action('action_name');. Here is where to place your code:

add_action( 'action_name', 'your_function_name' );

function your_function_name() {
// Your code
}

Using filter hooks

↑ Back to top

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.

Example

↑ Back to top

In the example below, both action hooks and filter hooks are used. This tutorial teaches you how to change and add fields to the checkout.

  • Actions are used to:
    • Add a new field to the checkout
    • Add that new field to the order
  • Filters are used to:
    • Override the labels and placeholders of existing fields
    • Make an existing field optional while it used to be required
    • Remove existing fields

API Documentation

↑ Back to top

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

Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.