• I’m trying to display products that are related either by category or attributes

    I put the following in functions.php but it doesn’t seem to work for me

    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' ) );
        $collections   = wc_get_product_terms( $product->id, 'pa_collection', array( 'fields' => 'slugs' ) );    
    
        unset( $args['post__in'] );
        $args['tax_query'] = array( 
            'relation' => 'OR',
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => $cats,
            ),
            array(
                'relation' => 'OR',
                array(
                    'taxonomy' => 'pa_brand',
                    'field'    => 'slug',
                    'terms'    => $brands,
                ),
                array(
                    'taxonomy' => 'pa_collection',
                    'field'    => 'slug',
                    'terms'    => $collections,
                )
            )
        );
    
        return $args;
    }
    add_filter('woocommerce_related_products_args', 'custom_related_product_args');

    I can’t figure out where I’m going wrong?

    [ Please do not bump. ]

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WooCommerce Related Products by Attribute’ is closed to new replies.