WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
exclude specific category products from related products (6 posts)

  1. newmedia85
    Member
    Posted 8 months ago #

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

    http://wordpress.org/plugins/woocommerce/

  2. newmedia85
    Member
    Posted 8 months ago #

    I also 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)
    ) );

    Not really strong in wordpress. This is all new to me so any ideas or pointer will be very appreciated.

  3. newmedia85
    Member
    Posted 8 months ago #

    No help from anyone?

  4. GregyPooh
    Member
    Posted 3 months ago #

    Did you figure this out?
    I'm having a similar issue...

  5. mredinius
    Member
    Posted 2 months ago #

    In the related.php file you mentioned, at the end of the apply filters code, paste this :
    'post__not_in' => array($product->id),
    'tax_query' => array(array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => array( 'sold' ), // Don't display products in the sold category in related products
    'operator' => 'NOT IN'))

    I left the 'post_not_in' line in there so you could see where the 'tax_query’ portion needs to be added.
    You will need to change the 'sold' text to whatever the slug is for the category you want to exclude. Enclose the slugs in single quotes ('), separate multiple categories by commas eg. 'mycategory1','mycategory2','mycategory3'

    One side effect I have found with this code is that it will break the loop when it hits an excluded category, so if you normally display 4 related products, it may only display 1, or 2, or maybe even none...or it may display all 4. Refreshing the page will change the related products and generate a new loop, thus the number of relateds displayed. Not a particularly nice side effect, but better than displaying related products that aren't in stock ;)

  6. justemoi
    Member
    Posted 1 week ago #

    Hi, I would like to be able to use a filter for this in my custom-functions.php as first proposed by newmedia85, instead of modifying a template as indicated above. Is it feasible?

    Would anyone know how to modify the code of newmedia85?

    Thanks, any help would be appreciated :)

Reply

You must log in to post.

About this Plugin

About this Topic