Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing functions from Woocommerce hooks

This will probably open a door for me as there's something I'm missing so a WooCommerce 101 please;

part of the WooCommerce template archive-product.php contains the code;

    <?php
            /**
             * woocommerce_before_shop_loop hook.
             *
             * @hooked woocommerce_result_count - 20
             * @hooked woocommerce_catalog_ordering - 30
             */
            do_action( 'woocommerce_before_shop_loop' );
        ?>

From this, and reading the documentation, it implies that this;

remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );

should remove the result count from the product categories returned. Only it doesn't.

What's wrong?

like image 592
Chris Pink Avatar asked Aug 10 '16 16:08

Chris Pink


People also ask

How do you remove action hooks?

If you are a WordPress developer, then you must have definitely used action and filter hooks. You might have also known that add_action() hook can be removed using remove_action() & add_filter() hook can be removed using remove_filter() functions.

What is hooks in WooCommerce?

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.

How do I add a hook to WooCommerce?

To use WooCommerce hooks (or WordPress hooks in general), you'll need to add code to your site. But again, you do not need to edit the template files themselves – you can add this code all in the same spot. There are two places you can add this code: Your child theme's functions.


1 Answers

I was going to say you should read the documentation but it is leaving an important part out.

remove_action() cannot be called directly and must, itself, be added to an action hook. The action hook needs to come before the action being removed. In this case I would just use the same hook, but an earlier priority (default is 10, I've used 1)

function so_38878702_remove_hook(){
   remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
}
add_action( 'woocommerce_before_shop_loop', 'so_38878702_remove_hook', 1 );
like image 165
helgatheviking Avatar answered Oct 21 '22 03:10

helgatheviking