DEV Community

Hisman
Hisman

Posted on

Useful WooCommerce Hooks

Hooks in WordPress are a way to add/modify some functionalities without directly editing the core files. There are two types of hooks: actions and filters. Actions let you add some code to run at specific points, whereas filters let you modify an existing variable.

Hooks can be used by calling the add_action or add_filter function in your plugin or the functions.php file in your theme. Those functions accept a callback function that contains your custom code.

Because WooCommerce is one of the most popular eCommerce plugins for WordPress. WooCommerce also implements some hooks to let other developers extend its functionalities. Below are some useful WooCommerce hooks that you can use on your website :

Change the Number of Products per Page

function change_products_per_page() {
    return 30;
}
add_filter( 'loop_shop_per_page', 'change_products_per_page' );
Enter fullscreen mode Exit fullscreen mode

Change Breadcrumb Delimiter

function change_breadcrumb_delimiter( $defaults ) {
    $defaults['delimiter'] = ' > ';
    return $defaults;
}
add_filter( 'woocommerce_breadcrumb_defaults', 'change_breadcrumb_delimiter' );
Enter fullscreen mode Exit fullscreen mode

Hide Page Title

add_filter( 'woocommerce_show_page_title', '__return_false' );
Enter fullscreen mode Exit fullscreen mode

Show Free Shipping Cost

function show_free_shipping_cost( $label, $method ) {
    if ( $method->cost > 0 ) {
        return $label;
    }

    return $label .= ': ' . wc_price(0);
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'show_free_shipping_cost', 10, 2 );
Enter fullscreen mode Exit fullscreen mode

Disable Out of Stock Variation

function disable_out_of_stock_variation( $active, $variation ) {
    if( ! $variation->is_in_stock() ) {
        return false;
    }

    return $active;
}
add_filter( 'woocommerce_variation_is_active', 'disable_out_of_stock_variation', 10, 2 );
Enter fullscreen mode Exit fullscreen mode

Top comments (0)