Support » Plugin: WooCommerce » Change related products query by attribute

  • Resolved R3dRidl3

    (@r3dridl3)


    We have a WooCommerce shop for a client and we are trying to use the filter woocommerce_output_related_products_args to change the selection of related products.

    The function is as follows, but it’s not working.
    It only uses the category (default) for the filter. It just ignores the pa_model.

    Am I doing something wrong, missing something or using the wrong filter?

    Hope someone can help with this.

    function woocommerce_aantal_related( $args ) {
    	
    	global $product;
    	$cats = wc_get_product_terms( $product->id, 'product_cat', array( 'fields' => 'slugs' ) );
    	$types = wc_get_product_terms( $product->id, 'pa_model', array( 'fields' => 'slugs' ) );
    	$args['tax_query'] = array( 
    		'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => $cats,
    			'operator' => 'AND'
            ),
    		array(
    			'taxonomy' => 'pa_model',
    			'field'    => 'slug',
    			'terms'    => $types,
    			'operator' => 'AND'
    		),
    	);
    	$args['posts_per_page'] = 8;
    	$args['columns'] = 4;
    	return $args;
    }
    add_filter( 'woocommerce_output_related_products_args', 'woocommerce_aantal_related' );
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter R3dRidl3

    (@r3dridl3)

    Changed the code with an other filter wich works like this:

    function woocommerce_aantal_related( $args ) {
    	$args['posts_per_page'] = 8;
    	$args['columns'] = 4;
    	return $args;
    }
    add_filter( 'woocommerce_output_related_products_args', 'woocommerce_aantal_related' );
    
    function filter_related_products($args){	
    	global $product;
    	$args = array();	
    	$thiscats = wc_get_product_terms( $product->id, 'product_cat', array( 'fields' => 'slugs' ) );
    	$thistypes = wc_get_product_terms( $product->id, 'pa_model', array( 'fields' => 'slugs' ) );
    	
    	$ids_by_model_attribute = get_posts( array(
            'post_type' => 'product',
            'numberposts' => -1,
            'post_status' => 'publish',
            'fields' => 'ids',
    		'tax_query' => array(
    			'relation' => 'AND',
    				array(
    					'taxonomy' => 'product_cat',
    					'field'    => 'slug',
    					'terms'    => $cats,
    					'operator' => 'AND'
    				),
    			array(
    				'taxonomy' => 'pa_model',
    				'field'    => 'slug',
    				'terms'    => $thistypes,
    				'operator' => 'AND'
    			)
    		)
       ) );
    	
    	if($ids_by_model_attribute){
    		foreach($ids_by_model_attribute as $product_id){
    			$cats = wc_get_product_terms( $product_id, 'product_cat', array( 'fields' => 'slugs' ) );
    			$types = wc_get_product_terms( $product_id, 'pa_model', array( 'fields' => 'slugs' ) );
    			if($cats == $thiscats && $thistypes == $types){
    				array_push($args,$product_id);
    			}
    		}
    	}
    	
    	return $args;
    }
    add_filter('woocommerce_related_products','filter_related_products');
    • This reply was modified 1 year, 5 months ago by R3dRidl3.
    Plugin Support mouli a11n

    (@mouli)

    Hi there,
    I am assuming that you managed to get this working with the second code snippet so so I’m marking this thread resolved. Hopefully, you’ve been able to resolve this, but if you haven’t, please open up a new topic and we’ll be happy to help out.

    Hi @r3dridl3

    I always wondered why WooCommerce related products tends to show old products and not newer ones. I was looking for a solution for months and today I was lucky to come across this topic. I used your second code and it worked like a charm. Thanks a lot for sharing it.

    I’ve got just two questions, I’d really appreciate if you could help me.
    could you please kindly also modify the code so it only shows in-stock products?
    and in case I wanted to add two more attributes in addition to pa_model, how could I do that?

    Regards,

    • This reply was modified 1 year, 4 months ago by moemauphie.
    Thread Starter R3dRidl3

    (@r3dridl3)

    It depends on what you want to add.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change related products query by attribute’ is closed to new replies.