Are 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:
- Log into your WordPress site and access the Dashboard as the admin user.
- 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.
- Add the following code to the php file:
* 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
- 100+ Tips, Tricks & Snippets Ultimate WooCommerce Hide Guide
- WooCommerce Redirect After Checkout : Redirect to Custom Thank You Page
- How to Get Current Product Category Name in WooCommerce
- How to Duplicate a Product in WooCommerce
- How to Add Product SKU In WooCommerce
- How to Fix WooCommerce Storefront Theme Internal 500 Server Error
- How to Add Custom Add to Cart Button In WooCommerce
- How to Add Product to Cart Programmatically in WooCommerce
- How to Clear Cart on Logout In WooCommerce
- How to Create Checkbox Field in WooCommerce Checkout Page
- How to Remove Product Category Title WooCommerce
- How to Remove Related Products in WooCommerce in 3 Easy Options
- How to Set Custom Related Products In WooCommerce
- How to Import Products WooCommerce
- How to Add Wishlist In WooCommerce
- How to Add Search to Shop Page In WooCommerce
- How to Set Up WooCommerce Product Price Per Kg
- How to Add Upsell Checkout WooCommerce
- How to Export Products In WooCommerce
- How to Hide Any Tab My Account Page WooCommerce