• Resolved caleaaki

    (@caleaaki)


    Hello,

    I’m having problems trying to get Woocommerce to ignore product tags when pulling related products to show on a product page. We have four main categories in our store: European Antiques, Classical Furniture, Lifestyle Furniture, and Nostalgia Decoration. Products under European Antiques show up as related products under Lifestyle Furniture due to tags, for example. I’m ok with related products coming from the parent category or the sub-category they are in but I need the tags to be ignored.

    I’ve tried the code listed below but they haven’t helped much.

    add_filter( 'woocommerce_get_related_product_tag_terms', function( $term_ids, $product_id ){
        return array();
    }, 10, 2 );

    and

    add_filter( 'woocommerce_product_related_posts_relate_by_tag', '__return_false' );

    Is there anything else I can try or should that code work?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Returning false on woocommerce_product_related_posts_relate_by_tag is the correct path to take. Note that related products are cached, so you might just not be noticing the results yet.

    Clear cache & transients after setting up the filter to see results ??

    Steve

    (@thewebsmiths)

    Hi,
    I’m having the same issue.
    I’ve added the above to functions.php, cleared transients (I have no caching at present) but still show related tags as well as category products.
    I just want the category products.
    Any more ideas?
    Thanks in advance.
    Steve

    Can you guys tell me the right code to use? I’ve searching for this like months.

    I use the below function successfully in the past. This goes in your themes functions.php file…

    // Get Related Products from SAME Sub-category
    add_filter( 'woocommerce_product_related_posts', 'my_custom_related_products' );
    function custom_related_products($product){
        global $woocommerce;
        // Related products are found from category and tag
        $tags_array = array(0);
        $cats_array = array(0);
        // Get tags
        $terms = wp_get_post_terms($product->id, 'product_tag');
        foreach ( $terms as $term ) $tags_array[] = $term->term_id;
        // Get categories
        $terms = wp_get_post_terms($product->id, 'product_cat');
        foreach ( $terms as $key => $term ){
            $check_for_children = get_categories(array('parent' => $term->term_id, 'taxonomy' => 'product_cat'));
            if(empty($check_for_children)){
                $cats_array[] = $term->term_id;
            }
        }
        // Don't bother if none are set
        if ( sizeof($cats_array)==1 && sizeof($tags_array)==1 ) return array();
        // Meta query
        $meta_query = array();
        $meta_query[] = $woocommerce->query->visibility_meta_query();
        $meta_query[] = $woocommerce->query->stock_status_meta_query();
        $meta_query   = array_filter( $meta_query );
        // Get the posts
        $related_posts = get_posts( array(
                'orderby'        => 'rand',
                'posts_per_page' => $limit,
                'post_type'      => 'product',
                'fields'         => 'ids',
                'meta_query'     => $meta_query,
                'tax_query'      => array(
                    'relation'      => 'OR',
                    array(
                        'taxonomy'     => 'product_cat',
                        'field'        => 'id',
                        'terms'        => $cats_array
                    ),
                    array(
                        'taxonomy'     => 'product_tag',
                        'field'        => 'id',
                        'terms'        => $tags_array
                    )
                )
            ) );
        $related_posts = array_diff( $related_posts, array( $product->id ), $product->get_upsells() );
        return $related_posts;
    }
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show only products of the same category in related products’ is closed to new replies.