• Resolved moemauphie

    (@moemauphie)


    Hi there,

    I hope I have created this thread in the right place. I’m using this custom code to display related products based on a specific attribute, but it doesn’t work. Could you please see if I’m missing something here?

    function custom_related_product_args ( $args ){
        global $product;
    
        $cats          = wc_get_product_terms( $product->id, 'product_cat', array( 'fields' => 'slugs' ) );
        $brands        = wc_get_product_terms( $product->id, 'pa_brand', array( 'fields' => 'slugs' ) );
    
        unset( $args['post__in'] );
        $args['tax_query'] = array( 
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => $cats,
            ),
            array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'pa_brand',
                    'field'    => 'slug',
                    'terms'    => $brands,
                ),
    
            )
        );
    
        return $args;
    }
    add_filter('woocommerce_related_products_args', 'custom_related_product_args');

    Many Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Improve Related Products’ is closed to new replies.