How to Hide Related Products for a Specific Category in WooCommerce

Want to hide related products from a particular category in WooCommerce but don’t know how to?

Don’t worry, we have made it simple for you!

WooCommerce is a powerful eCommerce platform for WordPress that provides various features to enhance the user experience. One such feature is the “Related Products” section on the single product page.

This section is designed to display products that share the same category or tag, potentially increasing sales by suggesting relevant items. However, some store owners may want to remove this section to maintain a clean design, focus on specific products, or prevent distractions.

If you are looking for a simple and effective way to hide related products section in WooCommerce for a particular product category, using a PHP snippet is the best approach.

In this guide, we will walk you through the process step by step.

The most effective way to hide Related Products section is by using a small PHP snippet that removes the related products function from WooCommerce. Here’s how to do it:

Step 1: Open Your Theme’s functions.php File

To implement the code, you need to edit the functions.php file of your WordPress theme. It’s highly recommended to use a child theme to prevent losing changes when updating your main theme.

Hide Related Products from a Particular WooCommerce Product Category
Finding the functions.php file

Log into your WordPress dashboard and navigate to the functions.php file.

Learn Where to Add Custom Code Snippet in WordPress

Step 2: Adding the Code Snippet

Now, add the following PHP snippet at the end of the functions.php file to hide related products category:

/**
*  @snippet Exclude Category from Related Products | WooCommerce
*  @tutorial https://tutsflow.com/
*  @author Tutsflow
*/
function tutsflow_exclude_category_from_related_products( $related_posts, $product_id ) { if ( ! is_singular('product') ) {
return $related_posts;
}
// Define the category slug or ID you want to exclude
$excluded_category = '23'; // Change this to your category slug or ID

foreach ( $related_posts as $key => $related_post_id ) {
    if ( has_term( $excluded_category, 'product_cat', $related_post_id ) ) {
        unset( $related_posts[$key] );
    }
}

return $related_posts;
}
add_filter( 'woocommerce_related_products', 'tutsflow_exclude_category_from_related_products', 10, 2 );
Hide Related Products from a Particular WooCommerce Product Category
Hide Related Products from a Particular WooCommerce Product Category

Step 3: Save and Test the Changes

After adding the code:

  • Click the “Update File” button to save changes.
  • Visit any single product page on your WooCommerce store, we have removed related products for only Keyboard Category (ID:22)
  • Check whether the “Related Products” section has been removed.
Check whether the "Related Products" section has been removed
Check whether the “Related Products” section has been removed

As you can see, no related products are shown for the keyboard category, but for mouse category, we can still see related products as shown below:

Related Products Visible for other categories
Related Products Visible for other categories

If the related products are still visible, ensure that:

  • Your theme does not override the default WooCommerce layout.
  • The code is correctly placed in the functions.php file.
  • You have cleared your cache (browser cache and any caching plugin cache).

How to Find the Category ID of the Product?

If you are having trouble finding your category id for the product, here are simple steps you can follow to know your category ID:

  • Log in to your WordPress Admin.
  • Navigate to Products > Categories.
  • Hover over the category name you want to find the ID for.
  • Look at the URL in the address bar of your browser (or click to open the category).

In that address bar, search for the tag_ID= part of the URL. The number after the equals symbol is the category ID.

There are several reasons why you might want to remove the “Related Products” section from your WooCommerce product pages:

  1. Better Product Focus: By removing related products, you can keep the user’s attention on the main product, improving the chances of conversion.
  2. Custom Design Needs: Some themes or custom product layouts may not require related products, and keeping them might disrupt the page design.
  3. Improved Page Load Speed: Fewer elements on the page can lead to faster load times, which is crucial for user experience and SEO.
  4. Better Control Over Product Recommendations: You may want to use a custom upsell or cross-sell system instead of WooCommerce’s automated related products.

Final Thoughts

To hide Related Products section from WooCommerce product pages is a straightforward process that can help improve user experience, site speed, and product focus.

By adding a simple PHP snippet to your theme’s functions.phpfile, you can quickly disable related products without affecting other WooCommerce functionalities.

If you ever decide to bring back the “Related Products” section, simply remove the snippet from your functions.php file or deactivate the snippet in your Code Snippets plugin.

If you want to learn more such WooCommerce tutorials for customizations in your store, Tutsflow has got you covered!

Deals & Discount

Coupons

Unbounce Coupon

Save 35% off your first year

Cloudways Coupon

Save 20% off first 1 months

Featured

WordPress Plugins

PatternsWP

WordPress Block Patterns Library

ClonePress

Duplicate Pages, Posts & CPT