Forcing Related Products by Main Category in WooCommerce

In a recent Business Bloomer Club Slack thread, a WooCommerce user asked how to display only products from the same main category as related items on the product page.

By default, WooCommerce pulls related products based on both category and tag associations, which can lead to broader, sometimes random, product recommendations. However, with some filtering, it’s possible to refine this behavior to display only products from the main category.

Here’s how to achieve this with custom code snippets.

Step 1: Disable Tag-Based Related Products

WooCommerce, by default, pulls related products based on both categories and tags. To focus solely on category-based recommendations, disable tag-based related products using the following filter:

add_filter( 'woocommerce_product_related_posts_relate_by_tag', '__return_false' );

This filter limits related products to only those within the same categories, helping refine the selection.

Step 2: Customize Related Products to the Main Category Only

To further narrow down related products to a specific main category, you can use the woocommerce_get_related_product_cat_terms filter. This filter allows you to specify which categories WooCommerce should consider when selecting related products.

Here’s a code snippet to force WooCommerce to only consider the main category of the current product:

add_filter( 'woocommerce_get_related_product_cat_terms', 'force_related_products_main_category', 10, 2 );

function force_related_products_main_category( $categories, $product_id ) {
    // Get all categories for the current product
    $product_categories = wc_get_product_term_ids( $product_id, 'product_cat' );

    // Check if there are categories and set the first one as "main"
    if ( ! empty( $product_categories ) ) {
        $main_category = array( $product_categories[0] ); // Replace with logic if main category differs
        return $main_category;
    }

    return $categories; // Fallback if no categories found
}

In this snippet:

  • Line 5: Retrieves all categories associated with the product.
  • Line 8: Returns only the first category as the “main” category. Adjust this logic if you have a specific primary category setup.
  • Line 10: Ensures WooCommerce only displays products from this primary category.

Step 3: Clear WooCommerce Transients

Once you’ve implemented this code, clear WooCommerce transients to ensure related products reflect the updated settings. Go to WooCommerce > Status > Tools and select Clear transients.

Conclusion

By using these filters, you can display related products only from the main category in WooCommerce, creating more targeted and relevant recommendations for customers. This customization provides a streamlined approach to managing related products, focusing on the main category without relying on broader tag associations.

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Reply

Your email address will not be published. Required fields are marked *