SOLVED: Query Loop: is it possible to have Query Loop for upsells,

Query Loop: is it possible to have Query Loop for upsells, cross-sells, and related products on the single product template? I don’t want to use standard elements. Thanks.

5 Likes

Hi, I’ve found solution for related products only. But I also need cross-seel products in query loop.

1 Like

Hi @grochobibok and @tole011 ,

You can achieve it by using our Filter.

For cross sell, please replace $product->get_upsell_ids() with WC()->cart->get_cross_sells();

Regards,
Jenn

3 Likes

Great job Jenn! I love your blog :slight_smile:
Thanks a lot and regards,
Grzegorz

1 Like

Thank you! Following up on this, is it possible to query loop cross sell items in the cart based on cart content? :thinking:

Hi guys!

Has anyone displayed cross sell products using Query Loop? Does it need to edit the code? I need it badly. thanks

Have you tried as @itchycode has posted? What did not work?

Have you tried as @itchycode has posted? What did not work?

To be honest, I use Google Translate and I didn’t understand what to do. It looks like it needs php code right? I don’t know php.

Ok, it would for sure help if you knew some php but let’s try! Do you have code snippet plugin like wpcodebox or do you know how to edit the functions.php of your bricks child-theme?

Thank you my friend. Yes, I can edit functions.php

Ok, great!

Then copy the following code that i took from Filter: bricks/posts/query_vars – Bricks Academy

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
	// Change the Id 
	if( $element_id !== 'shctqn') return $query_vars;

	$product_id = get_the_ID();
	$product = wc_get_product( $product_id );

	if( ! is_a( $product, 'WC_Product' ) ) return $query_vars;

	$cross_sell_ids= $product->get_cross_sell_ids();
	$query_vars['post__in'] =  ( count( $cross_sell_ids ) > 0 )? $cross_sell_ids: [0] ;

	return $query_vars;
}, 10, 3 );

You MUST change the element_id in the code above. Replace shctqn with the id of the element where you have have the query loop applied to (if you don’t understand look at the screenshot in the article). I have replaced the function as @itchycode wrote in his post already for you in the code above. So you just need to replace the id and then copy the code in your functions.php (Please make sure that you have the bricks child theme installed).

For the query to work as expected make the settings as they were made in the screenshot in the article.

That’s it, then it should work! Feel free to ask if you are stuck! :slight_smile:

3 Likes

It worked well. Thank you my friend for your time.

cheers🥂

1 Like

This solution here posted does not work for multilang templates with Poly/Wpml. Since the Query loop has build in php now I tried this but get no result

image

What I’m missing?

I also tried {cf__crosssell_ids} in terms taxonomy field. Bricks at least done something and showed one product out of 4. No clue why only 1. Setting to “Exists” showed then various products not related. headache…

Sidenote: Why the hell is it not possible to use ones own ID??? The code here which was marked as solution works only with Bricks ID… This wont work for multilange templates with WPML/Polylang. If one could use one own ID, the solution would be a real solution

Thank you for your help!!

This does not work on cross-sell items for each variation.

For example if I have a “Widget” with differenct colors and one is “Red”, then “Widget - Red” has it’s own ID and is a valid cross sell product in WooCommerce. It can be added as a linked cross-sell product. This filter misses it and will only show “Widget”.