May 5, 2022
浏览次数 : 11
Either way, the function is always the same. You simply need to understand how to use it. Enjoy!
Meet the has_term() WordPress function
WordPress comes to the rescue with the has_term() function, which accepts 3 parameters:
- The term (category, tab, custom term) name, term ID, slug or array of them
- The taxonomy name (default or custom)
- The post ID or post object, in case you don’t want to use the “current post”
It basically goes like this: you define a term you want to check against the post object e.g. “Rock”, you specify the default or custom taxonomy name (in the Jazz example it could be you’re using a “Music” custom taxonomy) and finally you may specify the post ID:
Easy peasy. Now it’s time to apply this to WooCommerce and see where it could come in handy. WooCommerce comes already with custom taxonomies: “product_cat” for product categories and “product_tag” for product tags. That’s all we need to use.
WooCommerce: check if current product belongs to product category @ Single Product Page
This is the easiest case scenario. You’re customizing the single product page and want to show a specific banner in case the current product belongs to a certain category.
The good thing is that we are on the single product page and therefore we know the “current post” exists and we don’t need the third has_term() parameter.
I’ve picked the woocommerce_before_single_product hook to display the banner above the single product template, but feel free to chose any other single product page hooks. Code goes like this:
Only if the single product we’re looking at belongs to the “tables” category, the image will display. Otherwise, nothing will happen.
WooCommerce: check if current product belongs to product tag @ Single Product Page
Similarly, you can check the current product against a given product_tag:
WooCommerce: check if current product belongs to product category @ Shop Page
Let’s complicate things.
We’re now in the Shop page, or in a product list section generated by a shortcode. That’s called the “loop”. In this case, you have multiple products in the same page, so we can’t really use the same method as above as the “current post” won’t be a product ID – instead it will be the current page ID.
We therefore need to “calculate” the ID based on the position we’re inside the “loop”. That’s easier coded than said actually.
So, let’s try to print a message under EVERY product in the Shop page that belong to a given category e.g. “Chairs”. I’ve used the woocommerce_after_shop_loop_item but feel free to pick any other shop hooks from my visual hook guide.
As you can see inside the snippet, I first need to find out the “loop product ID” because this needs to run for each product in the loop, and after that I can go back using the has_term() conditional:
WooCommerce: check if current product belongs to product tag @ Shop Page
WooCommerce: check if a product ID belongs to product category
Let’s say you are outside the loop and not on the single product page. That means you have no “current product” to deal with, no global $product you can call, and you’re stuck.
Thankfully, the has_term() function accepts the post ID as third parameter so you can run your conditional check even on the Homepage, inside a shortcode, inside a WooCommerce order email, and so on.
You simply need to specify the ID you desire.
In this case study I wish to display something in the footer if product ID 59 belongs to a certain category. It could be for a site-wide advertising banner for example