Blog

Check if Product Belongs to a Product Category or Tag

Are you trying to make it work with in_category() function but nothing happens? Well, in this tutorial you will find out why.

Actually it is a very common mistake and I also did it before.

First things first, let’s begin with the fact that products in WooCommerce are a custom post type product and product categories are a custom taxonomy product_cat. When you work with custom taxonomies, do you use functions for categories/tags or for taxonomies?

Comparing to is_product_category(), WooCommerce doesn’t have its own conditional tags here, so we have to use default WordPress conditional tags like has_term().

Create Dynamic <SelectControl> with Posts in Gutenberg

Check If Current Page is a Product Category

Are you using is_category() and nothing works?… This tutorial is just for you then.

Please keep in mind, that product categories in WooCommerce are just a WordPress taxonomy product_cat. It means that it is possible to use all the conditional tags related to taxonomies as well.

But let’s begin with WooCommerce conditional tags first.

  • is_product_category() – returns true on every product category archive page,
  • is_product_category( $category ) – you can check if you are on a certain product category page just by passing an ID, slug or title of a category as an argument.

Remove Product Prices

In this tutorial I will show a correct way of removing product prices from single product pages and shop page (product archives).

There are plenty of tutorials out there that recommend you to use woocommerce_get_price_html hook which is totally wrong, because that hook is intended to edit the HTML of the price element. We are not going to hide prices with CSS as well.

WooCommerce connects product prices to hooks:

So all we need to do is to disconnect price displaying functions from these hooks!

Random Product Sorting with Pagination

There are already a couple tutorials on this website related to product sorting, you can read for example, how to change a default sorting order, how to completely remove it or how to add a custom sorting option.

Today we are going to go even further and add an option to sort products in random order and retain it during pagination.

First things first, let’s add a new sorting option. There is another tutorial about that, so you can read more if you want. Right now we are going to do it with woocommerce_catalog_orderby filter hook.

How to Remove Product Sorting Dropdown

In this tutorial I will show you how to completely remove this dropdown from the shop page and product archives:

Product sorting dropdown in WooCommerce

That’s the result we are going to achieve:

remove product sorting dropdown in WooCommerce

And we are not hiding it with CSS by the way!

Change Default Product Sorting

The default WooCommerce sorting is by “menu order”, which means manually and if a manual product order is not set, the product are going to be displayed alphabetically.

Before we continue, let me show you how to sort WooCommerce products manually, maybe it is just what you need.

Go to WordPress admin, All Products page, and there click Sorting tab. After that just use drag and drop.

Reorder WooCommerce products manually in WordPress admin