Make the Related Products Query more precise

This topic has 4 replies, 3 voices, and was last updated 18 minutes ago ago by Jack Richardson

  • Avatar: Digital-Boutique
    Digital-Boutique
    Participant
    December 4, 2025 at 05:41

    Hello,

    Our site currently has hundreds of products, and will very likely grow quickly to include many 100’s more. We are adding products en-masse, via CSV import tools.

    Our client will not have time to hand-edit every product, and carefully select “Linked Products” from a list of 100’s in a shopping cart that will have 100’s or 1000’s of products.

    We need to improve the query logic that gets applied when generating the “Related Products” at the bottom of each single product page.

    Example:
    If viewing a product with the following Taxonomy Combination:
    Brand: AllMade
    Categories: Men’s, Tops, Sweatshirts

    Then related products should show products of the same Taxonomy Combination. Is there a way to try and make the Related Product query logic a bit more precise?

    3 Answers
    Avatar: Samir Malik
    Samir Malik
    Support staff
    December 4, 2025 at 06:03

    Hello, Digital-Boutique,

    Unfortunately, this feature is not available in our XStore theme and it would require too many customization code to archive.

    We highly encourage you to share your suggestion on our Taskboard at https://www.8theme.com/taskboard/. If your idea garners sufficient support from other customers, our development team will carefully evaluate the possibility of including it in a future update.

    Should you have any further questions or require assistance, please do not hesitate to contact us.

    Best Regards,
    8Theme’s Team

    Avatar: Digital-Boutique
    Digital-Boutique
    Participant
    December 4, 2025 at 06:29

    OK – Understood.
    I will go and add it to the taskboard.
    Meanwhile, do you have a suggestion of logic I can add to our functions.php?
    Or perhaps a plugin that allows for adding better global logic such as:
    “apply all the same taxonomy as current product”?

    Avatar: Jack Richardson
    Jack Richardson
    Support staff
    December 4, 2025 at 10:32

    Hello @Digital-Boutique,

    You may use the following snippet as a starting point to include in your child theme’s functions.php file:

    add_filter( 'woocommerce_related_products_args', function( $args ) {
        global $product;
    
        if ( ! $product || ! is_a( $product, 'WC_Product' ) ) {
            return $args;
        }
    
        $product_id = $product->get_id();
    
        $brand_terms = wp_get_post_terms(
            $product_id,
            'brand',
            array( 'fields' => 'ids' )
        );
    
        $cat_terms = wp_get_post_terms(
            $product_id,
            'product_cat',
            array( 'fields' => 'ids' )
        );
    
        if ( empty( $brand_terms ) && empty( $cat_terms ) ) {
            return $args;
        }
    
        $tax_query = array(
            'relation' => 'AND',
        );
    
        if ( ! empty( $brand_terms ) ) {
            $tax_query[] = array(
                'taxonomy' => 'brand',
                'field'    => 'term_id',
                'terms'    => $brand_terms,
                'operator' => 'IN',
            );
        }
    
        if ( ! empty( $cat_terms ) ) {
            foreach ( $cat_terms as $cat_id ) {
                $tax_query[] = array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id',
                    'terms'    => array( $cat_id ),
                    'operator' => 'IN',
                );
            }
        }
    
        $args['tax_query'] = $tax_query;
        $args['post__not_in'] = array( $product_id );
    
        if ( isset( $args['post__in'] ) && ! empty( $args['post__in'] ) ) {
            $args['post__in'] = $args['post__in'];
        }
    
        return $args;
    }, 10 );

    Please note that after reviewing your request, we have confirmed that it requires custom development, which is outside the scope of Envato’s standard support policy. Therefore, we are unable to provide a complete ready-made solution, but we can offer some guidance.

    If you would like to have this customization completed efficiently and on time, we recommend submitting your request to our trusted partner, WP Kraken.

    You will receive:
    – A prompt quote and timeline
    – A turnkey implementation by a vetted team
    – A warranty for the delivered work

    We are confident that this solution will be helpful and hope you will find it beneficial.

    Kind regards,
    Jack Richardson
    8Theme Team

    Content is visible for topic creator and
    support staff only.
  • Viewing 4 results - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.