My Courses > CustomizeWoo > Module 2 > Lesson 06: Hooks: Filters

Hooks: Filters

MARK LESSON AS COMPLETE

Filters are vital for customizing WooCommerce – if there were no filters, we would have had to edit the plugin core files. Instead, we can “hook” into each template and edit WooCommerce functions.

Video

Please note: English captions are enabled by default (click on “CC” in the video player to disable). Also, you can click on the “gear” icon to manage quality and playback speed. Enjoy!

Sorry, this video is visible to logged in and fully registered students only.
If you already enrolled in this course, please LOG IN
Otherwise, please enroll in the FREE / PRO COURSE
For any other queries, feel free to get in touch

Useful Links

WordPress Action Hooks: https://developer.wordpress.org/plugins/hooks/actions/

WooCommerce Hooks: https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/

9 thoughts on “Hooks: Filters

  1. Hi Rodolfo,
    I wonder why you mentioned that it is “not easy” to find filter hooks vs do_action() ?

    Filter hooks use apply_filter() which should be also easy to find , right ? or maybe there methods to apply filter other than using the apply_filter() ?

    1. Thanks for your comment Besir!

      It’s not easy to “find” filter hooks (they use the apply_filters() function) because there are millions of them + there are no “visual hook guides” – like mine – for action hooks.

      Also, filters operate in the backend but very rarely in the frontend, so it’s more complex. At the same time, nothing stops you from doing a file search for “apply_filters”!

  2. In the project specs you mention edit add to cart button label. I can’t find a filter that hooks into the label. Can you show me where it is located?

    I’m enjoying the course and I want to learn as much as I can.

    1. Hello! I did a quick search through the files and found the “woocommerce_product_add_to_cart_text” filter:

      apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this );
      

      You can try to use that one to “filter” i.e. edit the add to cart labels. Let me know!

  3. Hey Rodolfo, I felt inspired to try write my very first own PHP snippet. And it’s working!!! 🙂 I am so excited. But I’ll be even more excited if you could tell me whether I did it right.

    I wanted to change the sale badge text. Here’s what I wrote:

    // Change sale badge text
    add_filter( 'woocommerce_sale_flash', 'edit_sale_badge_text' );
    function edit_sale_badge_text( $val ) { 
    	$val = '<span class="onsale">' . esc_html__( 'Buy me!', 'woocommerce' ) . '</span>'; 
    	return $val; 
    }

    What do you think? And please do give it to me straight – I want to learn to do this right from the get go.

    1. Noelle, that’s indeed perfect. Well done 🙂

      If your site is only in English you don’t really need the esc_html__() bit, so you could simplify it if you wanted. Also, you’re giving back just one variable, and you’re not manipulating the original one (i.e. you’re overwriting it completely), so you could even skip the $val declaration:

      add_filter( 'woocommerce_sale_flash', 'edit_sale_badge_text' );
      
      function edit_sale_badge_text() { 
         return '<span class="onsale">Buy me!</span>'; 
      }
      
      1. Wonderful! Thank you for the feedback.

  4. Did it worked, it looked like in your video the code did not work, I am going to try it and post, but I just wanted to say it might be a better display if we see the code working, the video seems to show the same snapshot of when the checkbox was there, just asking,

    1. Ah! You’re totally right Shessvy, in the second screenshot the checkbox is still there – that’s just a video editing mistake. I can guarantee the snippet works 🙂

      I copy the snippet here so that you can test it. Actually now that you’re there, I’m going to teach you a quicker way of working with filters when you need to return just true or false:

      add_filter( 'woocommerce_checkout_show_terms', '__return_false' );
      

      Let me know 🙂

Questions? Feedback? Support? Leave your comment now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]

Your email address will not be published. Required fields are marked *