Viewing 1 replies (of 1 total)
  • Plugin Author WebToffee

    (@webtoffee)

    Hi @bernaertsmusic

    Currently, there are no settings to exclude a specific category from related products. But there is the option to select a specific category to include.

    Screenshot 2021-01-21 at 8.56.17 PM.png

    If the default WooCommerce related product option is not disabled in the plugin settings, the below code snippet can be used to exclude specific categories.

    add_filter( 'woocommerce_related_products', 'wt_exclude_product_category_from_related_products', 10, 3 );
    
    function wt_exclude_product_category_from_related_products( $related_posts, $product_id, $args ) {
    	// Add the product categories slug to exclude
    	$term_slug[] = 'fabric-patterns';
    	$term_slug[] = 'cabin';
    
    	// Fetch the product Ids in the defined product categories
    	$exclude_ids = wc_get_products( array(
    		'status'	 => 'publish',
    		'limit'		 => -1,
    		'category'	 => $term_slug,
    		'return'	 => 'ids',
    	) );
    
    	// Remove the products from the selected categories
    	return array_diff( $related_posts, $exclude_ids );
    }
    
Viewing 1 replies (of 1 total)
  • The topic ‘Related Products exclude category’ is closed to new replies.