Removing functions from Woocommerce hooks

11,996

Solution 1

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

Solution 2

To remove result count action from Woocommerce -

add_action('woocommerce_before_shop_loop', 'remove_result_count' );
    function remove_result_count()
    {
         remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20);
      
    }

use this code in your function.php file.

Important: To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.

http://codex.wordpress.org/Function_Reference/remove_action

Here you can get WooCommerce Action and Filter Hook -

https://docs.woothemes.com/wc-apidocs/hook-docs.html

Share:
11,996

Related videos on Youtube

Chris Pink
Author by

Chris Pink

Web developer and front-end designer. I used Wordpress as my main CMS for a number of reasons. 1. It has a wonderful community and there's a plug in or a solution to almost anything 2. despite the team's best efforts to keep it exclusively for blogs, it's a brilliant website CMS as well as one for blogs 3. the admin screen is incredibly simple for the less technically able and 4. I'm too rooted (read scared) to migrate all that knowledge to a different framework. I am developing my own personal boilerplate based on underscores _s.

Updated on June 04, 2022

Comments

  • Chris Pink
    Chris Pink almost 2 years

    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?

  • Chris Pink
    Chris Pink over 7 years
    Thank you, I'm beginning to understand this important technique now. And it's true that it is not mentioned in the Wordpress documentation.
  • helgatheviking
    helgatheviking over 7 years
    I just added it to the Codex. Though I think they are moving over to developer.wordpress.org.