0

I'm trying to exclude products from two specific categories from showing up in the related products on the content single products page. The closest idea I have stumbled upon is this code from http://docs.woothemes.com/document/exclude-a-category-from-the-shop-page/. Any idea how to modify it for related products and NOT for the shop page?

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;

if ( ! is_admin() && is_shop() ) {

    $q->set( 'tax_query', array(array(
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => array( 'knives' ), // Don't display products in the knives category on the shop page
        'operator' => 'NOT IN'
    )));

}

remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

closed as off-topic by Mark Kaplun, cjbj, Dave Romsey, Howdy_McGee Oct 4 '16 at 22:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

5

Old question, but ranks high in Google. Here's a solution that works with today's Woocommerce.

Add this to your functions.php or custom plugin.

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' );

And to do the same with tags:

function exclude_tags_from_related( $tags ){
    // array of tags that should be excluded
    $exclude_tags = array( 'discontinued', 'whatever', 'even-more');

    foreach( $tags as $index => $tag ){
        if( in_array( $tag->slug, $exclude_tags ) ){
            unset($tags[$index]);
        }
    }

    return $tags;
}

add_filter( 'woocommerce_get_related_product_tag_terms', 'exclude_tags_from_related' );
  • thanks for this! Can your snippet be made to exclude certain tags from related products instead of categories? We need to maintain product pages for discontinued products, but don't want them showing up in Related Products. I've started adding a "Discontinued" tag to those products and adding a watermark to the featured images to do this. – wastubbs Nov 22 '16 at 20:51
  • I added a variation that should address tags, but haven't tested it. Give it a try and let me know if it works out! @was – jgangso Nov 23 '16 at 8:03
  • @wastubbs i meant – jgangso Nov 23 '16 at 8:04
  • Thanks, @jgangso, I've added it to my theme's functions.php file. The items tagged as Discontinued are still showing up, but I believe that is because we're running WooCommerce 2.4 right now. We're working with a developer on the site and they may have made edits to the plugin's code so I'm waiting on them to update WC to 2.6. I'll report back when that's done with the final results! – wastubbs Nov 29 '16 at 20:46
  • @wastubbs I tested and revised the code a bit. Now it should work. But please be aware that if your product has any other tags that match the criteria, they will still show up. So if you want to completely hide those products from related products, then all other tags would be better removed. – jgangso Dec 1 '16 at 8:09
3

When developing with WooCommerce just look into plugin files. I found a file named related.phpin woocommerce\template\single-product\ :

$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)
) );

It's a simple query so it will be easy to exclude what you want.

Not the answer you're looking for? Browse other questions tagged or ask your own question.