Questions? Feedback? powered by Olark live chat software
How To Use Action Hook And Filter Hook
1 (20%) 1 vote[s]

When we need to customizable something in Woocommerce , in addition to editing the template that we have said earlier, we also have another option that is utilizing action hook and filter hook Woocommerce available so that we can do everything.

Use Action hook of Woocommerce

If you came across two articles above suggestions, you can understand the action will be created by the function do_action()  in the template. And we try to open the file  archive-product.php in the folder template will see some sections of this.

01
02
03
04
05
06
07
08
09
<?php
/**
* woocommerce_archive_description hook
*
* @hooked woocommerce_taxonomy_archive_description - 10
* @hooked woocommerce_product_archive_description - 10
*/
do_action( 'woocommerce_archive_description' );
?>

That means it has attached a hook action woocommerce_archive_description name at that location, and often the this action hook we will use to locate that makes an action right at the position of the hook. For example, you want to insert a sentence welcoming someone into position so that the hook will be placed in the following code in the theme functions.php:

01
02
03
04
05
/* Testing Hook */
function tp_before_main_content() {
 echo '<p>This text will appear above the list of products.</p>';
}
add_action('woocommerce_archive_description', 'tp_before_main_content');

Very easy to understand and easy to  use
In addition, we can delete some content available on  hook was because you did not see the comment is @hooked ? That’s exactly it tells us that there  function is the hook at this position.
Now I take the example is to delete the section options displayed in the store.

1
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );

The paragraph above is mean i delete the function woocommerce_catalog_ordering() out hookwoocommerce_before_shop_loop  and it will be executed with the priority is 30 . It is simply based on this information is known:

01
02
03
04
05
06
07
08
09
<?php
/**
* woocommerce_before_shop_loop hook
*
* @hooked woocommerce_result_count - 20
* @hooked woocommerce_catalog_ordering - 30
*/
do_action( 'woocommerce_before_shop_loop' );
?>

Use Filter Hook

In WordPress, Filter Hook will be declared by the function ()apply_filters and allows us to edit the content code that hook is set. For example, open the file /templates/single-product/sale-flash.php you will see the following : 

1
<?php echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . __('Sale!', 'woocommerce' ) . '</span>', $post, $product ); ?>
In the above code, it has created a name filter hook is woocommerce_sale_flash and you can edit the HTML content that you have seen it, and it also contains two additional parameters added to the variable we can use.
And when you need to use to filter, we will have the code  to revise after the content that we want. Example:
1
2
3
4
5
6
/* Testing Filter Hook */
function tp_sale_flash( $output ) {
 $output = '<span class="onsale ">' . __( 'sales', 'woocommerce' ) .'</span>';
 return $output;
}
add_filter( 'woocommerce_sale_flash', 'tp_sale_flas' )
 You can customize what type code, as long as when we filter we remember return the content which should show up

Conclusion

How to use hook in WordPress in genaral and Woocommerce in particular only this much that, important is how we will apply for snappy. In fact, I see we have not needed now so we don’t know what to do with it but when you need to customize Woocomerce, let remember hook.
Thanks for your reading. If you have any question with this blog, don’t hesitate to leave a comment. I will reply to it as soon as possible.

Comments

  1. Avatar dert

    hi,

    how to remove p tags from category description woocommerce?

    not work “remove_filter(‘term_description’,’wpautop’);”

    thanks

Leave a Reply

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

[Flash Sale!] Grab 25% OFF for your PRO license. Limited time! Browse your themes!
[Summer Sale!] Just $1 per Premium Theme when you join Club Membership! 25% OFF on today, check it out!