Chat with us, powered by LiveChat

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.

How to Use WooCommerce Hooks

December 6, 2016

5 Min Read
Reading Time: 5 minutes

Hooks are an interesting concept in code development. The idea of implementing hooks is prevalent in almost all major languages.

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

Woocommerce Hooks

WooCommerce hooks have two distinct types, i.e.

  • Action Hooks are used to add custom code at selected locations in the code.
  • Filter Hooks are used to manipulate a variable before it is returned.

 General Layout

  • projects_before_main_content
  • projects_after_main_content
  • projects_sidebar

Archives

  • projects_archive_description

Loop

  • projects_before_loop
  • projects_before_loop_item
  • projects_loop_item
  • projects_after_loop_item
  • projects_after_loop

Single Project

  • projects_before_single_project
  • projects_before_single_project_summary
  • projects_single_project_summary
  • projects_after_single_project_summary
  • projects_after_single_project

Filters & Action

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

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 is the action which is used to download WooCommerce product after account authentication.

   Type: Action

do_action( 'woocommerce_after_account_downloads', $has_downloads );
  • woocommerce_ajax_variation_threshold : This is the filter which is used to 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 : This is WooCommerce filter for the count of available download.

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 : This is WooCommerce filter use to show available product download link.

 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 : This is WooCommerce action use to start 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 define using ‘ABSPATH’

Type:  action

<?php
 if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
do_action( 'woocommerce_before_account_orders', $has_orders ); ?>
  • woocommerce_breadcrumb_defaults: WooCommerce filter use 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 use 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 use for 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 use for name of 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 use for pricing of cart item.

 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 use for product item of 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 use for product id of 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 use for a subtotal of cart item.

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 use for thumbnail of cart item.

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 use for visibility of cart item.

   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 use 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 ) );
    }

Conclusion

In this article, I discussed the concept of WooCommerce hooks and how hooks can be used in loop, single project, filters, and actions. These hooks are essential in all aspects of WooCommerce developments including theme and plugin development. If you would like to add to the conversation, please leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Your WooCommerce Store Needs A Reliable Host

With no compromise on Performance, Security & Support.

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 owais.alam@cloudways.com

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

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!