This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Join the live AMA session with Adam Silverstein on open source and WordPress core! Register Now →

WooCommerce Hooks: A Detailed Guide With the Examples of Actions and Filters

Updated on December 26, 2022

6 Min Read
woocommerce hooks

WooCommerce hooks allow developers to customize and extend their store’s functionality. They can be attached to specific points in the code, such as before or after a product is added to the cart or an order is placed.

Hooks take the form of small code snippets that intercept calls between software components and offer appropriate actions for particular events. In the case of WooCommerce, hooks take on special significance because of their ease in altering standard behaviors.

In this article, I’ll tell you about types of WooCommerce hooks, their benefits, and a list of common WooCommerce hooks.

What Are Hooks in WooCommerce?

Hooks are an essential part of WooCommerce and are used to customize and extend the plugin’s functionality. They allow developers to easily modify and customize the store without modifying the core code of WooCommerce, which makes it easier to maintain and update the store.

Types of WooCommerce Hooks

There are two types of hooks in WooCommerce: actions and filters.

  1. Actions allow developers to add custom functions to specific points in the code, such as when a product is added to the cart, or an order is placed.
  2. Filters allow developers to modify data as it is passed from one part of the code to another. For example, a filter might be used to modify the price of a product before it is displayed on the product page.

From launching to customizing your WooCommerce stores, Cloudways is at your service.

Whether you’re a beginner or an expert, Cloudways Platform is based on UI, where you can create and customize your online store in a few seconds.

Benefits of Using WooCommerce Hooks

Here are a few benefits of using WooCommerce hooks.

  • Customization: Hooks allow developers to customize and extend the functionality of the WooCommerce plugin flexibly and powerfully. This can be useful for store owners who want to add custom features or modify the behavior of their store.
  • Maintainability: It is easier to maintain and update the store because hooks allow developers to modify the behavior of the store without modifying the core code. This can save time and effort when making changes or updating the store.
  • Compatibility: By using hooks, developers can ensure that their customizations are compatible with future versions of WooCommerce. This can help avoid issues when updating the plugin and ensure that the store continues functioning as intended.

How to Use Hooks in WooCommerce?

To use hooks in WooCommerce, you will need to:

  1. Add the function to the hook: You will need to add the custom function to the hook using the add_action() or add_filter() function in WooCommerce.
  2. Identify the hook you want to use: First, you will need to determine which hook you want to use based on the desired behavior you want to modify or the function you want to add.
  3. Create a custom function: You will need to create a custom function that will be executed when the hook is fired. This function can be as simple or complex as you need it to be.

List of Common WooCommerce Hooks

Here is a list of some common WooCommerce hooks:

  • get_product_search_form: It displays the product search form. I will first attempt to locate the product-searchform.php file in either the child or the parent, then load it. The default search form will be displayed if it doesn’t exist.

Type: filter

if ( ! function_exists( 'get_product_search_form' ) ) {

    function get_product_search_form( $echo = true  ) {
        ob_start();

        do_action( 'pre_get_product_search_form'  );

        wc_get_template( 'product-searchform.php' );

        $form = apply_filters( 'get_product_search_form', ob_get_clean() );

        if ( $echo ) {
            echo $form;
        } else {
            return $form;
        }
    }
}
  • pre_get_product_search_form: Get the classes for the product cat div. string|array $class One or more classes to add to the class list. object $category object Optional.

   Type: filter

function wc_get_product_cat_class( $class = '', $category = null ) {
    $classes   = is_array( $class ) ? $class : array_map( 'trim', explode( ' ', $class ) );
    $classes[] = 'product-category';
    $classes[] = 'product';
    $classes[] = wc_get_loop_class();
    $classes   = apply_filters( 'product_cat_class', $classes, $class, $category );

    return array_unique( array_filter( $classes ) );
}
  • woocommerce_after_account_downloads: This action is used to download WooCommerce products after account authentication.

   Type: Action

do_action( 'woocommerce_after_account_downloads', $has_downloads );
  • woocommerce_ajax_variation_threshold : This is the filter that is used for ajax variation.

    Type: Filter

function woocommerce_variable_add_to_cart() {
        global $product;

        // Enqueue variation scripts
        wp_enqueue_script( 'wc-add-to-cart-variation' );

        // Get Available variations?
        $get_variations = sizeof( $product->get_children() ) <= apply_filters( 'woocommerce_ajax_variation_threshold', 30, $product );

        // Load the template
        wc_get_template( 'single-product/add-to-cart/variable.php', array(
            'available_variations' => $get_variations ? $product->get_available_variations() : false,
            'attributes'           => $product->get_variation_attributes(),
            'selected_attributes'  => $product->get_variation_default_attributes()
        ) );
    }
  • woocommerce_available_download_count : WooCommerce filter for the count of available downloads.

Type: filter

if ( is_numeric($download['downloads_remaining']) )
echo apply_filters( 'woocommerce_available_download_count', '<span class="woocommerce-Count count">' . sprintf( _n( '%s download remaining', '%s downloads remaining', $download['downloads_remaining'], 'woocommerce' ), $download['downloads_remaining'] ) . '</span> ', $download );
  • woocommerce_available_download_link : WooCommerce filter used to show the available product download links.

 Type: filter

echo apply_filters( 'woocommerce_available_download_link', '<a href="' . esc_url( $download['download_url'] ) . '">' . $download['download_name'] . '</a>', $download );
  • woocommerce_available_download_start : WooCommerce action used to start the download

Type: action

do_action( 'woocommerce_available_download_start', $download );
  • woocommerce_before_account_downloads : WooCommerce action to download product before account using boolean.

Type:  action

$has_downloads = (bool) $downloads;
do_action( 'woocommerce_before_account_downloads', $has_downloads );
  • woocommerce_before_account_orders : WooCommerce action defines using ‘ABSPATH’.

Type:  action

<?php
 if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
do_action( 'woocommerce_before_account_orders', $has_orders ); ?>
  • woocommerce_breadcrumb_defaults: WooCommerce filter used to navigate breadcrumb.

  Type:  filter

function woocommerce_breadcrumb( $args = array() ) {
        $args = wp_parse_args( $args, apply_filters( 'woocommerce_breadcrumb_defaults', array(
            'delimiter'   => '&nbsp;&#47;&nbsp;',
            'wrap_before' => '<nav class="woocommerce-breadcrumb" ' . ( is_single() ? 'itemprop="breadcrumb"' : '' ) . '>',
            'wrap_after'  => '</nav>',
            'before'      => '',
            'after'       => '',
            'home'        => _x( 'Home', 'breadcrumb', 'woocommerce' )
        ) ) );

        $breadcrumbs = new WC_Breadcrumb();

        if ( $args['home'] ) {
            $breadcrumbs->add_crumb( $args['home'], apply_filters( 'woocommerce_breadcrumb_home_url', home_url() ) );
        }

        $args['breadcrumb'] = $breadcrumbs->generate();

        wc_get_template( 'global/breadcrumb.php', $args );
    }
  • woocommerce_breadcrumb_home_url : WooCommerce filter used to navigate breadcrumb using home.

 Type: filter

function woocommerce_breadcrumb( $args = array() ) {
        $args = wp_parse_args( $args, apply_filters( 'woocommerce_breadcrumb_defaults', array(
            'delimiter'   => '&nbsp;&#47;&nbsp;',
            'wrap_before' => '<nav class="woocommerce-breadcrumb" ' . ( is_single() ? 'itemprop="breadcrumb"' : '' ) . '>',
            'wrap_after'  => '</nav>',
            'before'      => '',
            'after'       => '',
            'home'        => _x( 'Home', 'breadcrumb', 'woocommerce' )
        ) ) );

        $breadcrumbs = new WC_Breadcrumb();

        if ( $args['home'] ) {
            $breadcrumbs->add_crumb( $args['home'], apply_filters( 'woocommerce_breadcrumb_home_url', home_url() ) );
        }

        $args['breadcrumb'] = $breadcrumbs->generate();

        wc_get_template( 'global/breadcrumb.php', $args );
    }
  • woocommerce_cart_item_class: WooCommerce filter used for the class of cart item.

Type:  filter

<?php
    if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); }?>">
  • woocommerce_cart_item_name : WooCommerce filter used for the name of the cart item.

 Type: filter

<?php
    if ( ! $product_permalink ) {
        echo apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . '&nbsp;';
        echo apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key );
    }
?>
<?php echo apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . '&nbsp;'; ?>
  • woocommerce_cart_item_price : WooCommerce filter used for pricing of cart items.

 Type: filter

<?php
$product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
?>

<td class="product-price" data-title="<?php _e( 'Price', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
?>
  • woocommerce_cart_item_product : WooCommerce filter used for product items in the cart.

Type: filter

<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );}
?>

<?php
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); }
?>

<?php
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); }
?>
  • woocommerce_cart_item_product_id: WooCommerce filter used for the product id of the cart item.

 Type: filter

<?php
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id   = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
?>
<?php
$_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
?>
  • woocommerce_cart_item_subtotal : WooCommerce filter used for a subtotal of cart items.

Type: filter

<td class="product-total">
<?php echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); ?>

<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
?>
  • woocommerce_cart_item_thumbnail : WooCommerce filter used for thumbnails of cart items.

Type: filter

<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
$product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
?>
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
?>
  • woocommerce_cart_item_visible : WooCommerce filter used for visibility of cart items.

   Type: filter

<?php
    if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {       
    }
?>
  • woocommerce_catalog_orderby: WooCommerce filter used for the ordering of product catalog.

Type: filter

function woocommerce_catalog_ordering() {
        global $wp_query;

        if ( 1 === $wp_query->found_posts || ! woocommerce_products_will_display() ) {
            return;
        }

        $orderby                 = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
        $show_default_orderby    = 'menu_order' === apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
        $catalog_orderby_options = apply_filters( 'woocommerce_catalog_orderby', array(
            'menu_order' => __( 'Default sorting', 'woocommerce' ),
            'popularity' => __( 'Sort by popularity', 'woocommerce' ),
            'rating'     => __( 'Sort by average rating', 'woocommerce' ),
            'date'       => __( 'Sort by newness', 'woocommerce' ),
            'price'      => __( 'Sort by price: low to high', 'woocommerce' ),
            'price-desc' => __( 'Sort by price: high to low', 'woocommerce' )
        ) );

        if ( ! $show_default_orderby ) {
            unset( $catalog_orderby_options['menu_order'] );
        }

        if ( 'no' === get_option( 'woocommerce_enable_review_rating' ) ) {
            unset( $catalog_orderby_options['rating'] );
        }

        wc_get_template( 'loop/orderby.php', array( 'catalog_orderby_options' => $catalog_orderby_options, 'orderby' => $orderby, 'show_default_orderby' => $show_default_orderby ) );
    }

Summary

In this article, I discussed the concept of WooCommerce hooks and how hooks can be used in loops, single projects, filters, and actions. These hooks are essential in all aspects of WooCommerce development, including theme and plugin development. If you’ve got any questions, please write them in the comments.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now