Support » Plugin: WooCommerce » Exclude category from Related Products

  • Resolved sroskylos

    (@sroskylo1)


    Is it possible to exclude a category from Related Products?

    In version 1.6.4 it was working with the following code

    
    $args = apply_filters( 'woocommerce_related_products_args', array(
    	'post_type'            => 'product',
    	'ignore_sticky_posts'  => 1,
    	'no_found_rows'        => 1,
    	'posts_per_page'       => $posts_per_page,
    	'orderby'              => $orderby,
    	'post__in'             => $related,
    	'post__not_in'         => array( $product->id ),
    	//Mod Code starts
    	'post__not_in' => array($product->id),
    	'tax_query' => array(array(
    	'taxonomy' => 'product_cat',
    	'field' => 'slug',
    	'terms' => array( 'exclude-category-slug' ), // Don't display products in the packs category in related products
    	'operator' => 'NOT IN'))
    	//Mod Code Ends
    ) );
    
    $products                    = new WP_Query( $args );
    $woocommerce_loop['name']    = 'related';
    $woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $columns );
    
    if ( $products->have_posts() ) : ?>
    

    After last updates the related.php changed.

    Any help?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Any one?????

    Try this,

    function exclude_brands_from_related( $categories ){
    // array of category id’s that should be excluded
    $exclude_cats = array( ‘100’, ‘101’, ‘102’);

    foreach( $categories as $index => $cat ){
    if( in_array( $cat->term_id, $exclude_cats ) ){
    unset($categories[$index]);
    }
    }

    return $categories;
    }

    add_filter( ‘woocommerce_get_related_product_cat_terms’, ‘exclude_brands_from_related’ );

    Source:
    https://wordpress.stackexchange.com/a/241462

    Plugin Support Shane Eckert

    (@shaneeckert)

    Automattic Happiness Engineer

    Hey sotos,

    Did the reply from harshal work?

    Please let us know if you need any more help with this.

    Cheers!

    Hi, I cant get this working either… has anyone managed to find a solution?

    • This reply was modified 2 years, 3 months ago by janey1986.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Exclude category from Related Products’ is closed to new replies.