How to Set WooCommerce Related Products Same Category

WooCommerce Related Products Same CategoryAre you looking for a way to display related products only from the same category or sub-category? Related Products is a section on some templates that pulls products from your store that share the same tags or categories as the current product.

Products cannot be specified in the admin. However, it can be influenced by grouping similar products in the same category or by using the same tags.

It is important to note that if you have a large WooCommerce store with lots of categories and subcategories, the related products area will randomly select products from not only the same category but any parent category above the category set for the currently displayed product.

You may want to only show related products from the same category or sub-category.

WooCommerce Related Products Same Category

In this brief tutorial, we will show you how to only show related products from the same category or sub-category.

However, to use this solution, you need to have some coding skills. We recommend creating a child theme so that your changes are not lost during an update.

Let us see how you can achieve this.

Steps to Show Related Products From the Same Category

Before we proceed, you should know that WooCommerce does cache-related products for around 10 hours. This means that you may need to test your code on a product that hasn’t been viewed.

Here are the simple steps you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will show you how to only show related products from the same category or subcategory.
  3. Add the following code to the php file:
[php] /**

* Only show products in the same subcategories in the related products area

*

* @param $terms – Terms currently being passed through

* @param $product_id – Product ID of related products request

* @return $terms/$subcategories – Terms to be included in related products query

*/

function njengah_filter_related_products_subcats_only($terms, $product_id) {

// Check to see if this product has only one category ticked

$prodterms = get_the_terms($product_id, ‘product_cat’);

if (count($prodterms) === 1) {

return $terms;

}

// Loop through the product categories and remove parent categories

$subcategories = array();

foreach ($prodterms as $k => $prodterm) {

if ($prodterm->parent === 0) {

unset($prodterms[$k]);

} else {

$subcategories[] = $prodterm->term_id;

}

}

return $subcategories;

}

add_filter( ‘woocommerce_get_related_product_cat_terms’, ‘njengah_filter_related_products_subcats_only’, 20, 2 );
[/php]

Conclusion

By now, you should be able to display related products from the same category or sub-category. Creating a child theme will ensure that your changes are not lost during an update.

You should be very careful when editing the functions.php file. This is because if you make a mistake, it will cause a critical error.

You should also remember that WooCommerce does cache related products for around 10 hours.

Similar Articles

  1. How to Get Current Product Category Name in WooCommerce
  2. How to Hide Any Tab My Account Page WooCommerce