Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Using metafields in where clause

Using metafields in where clause

umarakhter
Tourist
4 0 1
Hello There,
 
We have just upgraded to ShopifyPlus and are currently working on rolling out a few new features on our platform. 
 
One of the features is for us to display a set of related products.
 
For example, we have an attribute called koskii_code in the product metafields. In the product page we want to show all products with the same koskii_code. A simple way for that would be to filter all products that match that koskii_code from a collection that contains all products. Have written the sudo code below. 
 
The where condition does not seem to be working correctly. We dont know if we have got the syntax wrong or there is no support to filter a collection using metafields. Any leads will be helpful.
 
 
{% assign koskii_code = product.metafields.product.koskii_code %}
{% assign alternate_colors = products | where: "metafields.product.koskii_code", koskii_code %}
Colors:{% for product in alternate_colors %}- {{ render_tumbnail }} {% endfor %}
Replies 2 (2)

Jason
Shopify Partner
11206 226 2317

That's not something that is supported, so you seeing unexpected results would be a normal result.

 

Have you considered:

  • using a dedicated collection instead?
  • Or tags?
  • Or defining the related products in a metafield vs based on a property
  • Or bringing in those products via search results with an AJAX call (perhaps that uses tags or similar too).
★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
umarakhter
Tourist
4 0 1

Hey Jason,

 

Thanks for your note.

 

I am not sure if I was clear in my earlier explanation, but the route we have taken is to define related products in a metafield. The name of the field is koskii_code and the value in that field is the same for all related products (products with the same color in this case). Hence trying to use that field to filter products. I was considering creating a collection with all products in shopify, and then from that collection use the metafield to filter the alternate colors for the current product in the product page.  

 

For the other options you have suggested, here are some notes:

 

  • using a dedicated collection instead: I am not sure what you imply by this. To get the similar products together, I have seen some solutions that resort to creating a collection for each group. However, in our case since we are currently using this for the color option, we will end up creating a huge number of collections and managing those will be hard. 
  • Or tags? : We have had tags earlier, but has proved to be hard to maintain as well. 
  • Or bringing in those products via search results with an AJAX call - since again will depend on tags, same issue as above. And Ideally, if we dont have to do ajax would like to avoid that. 

 

Thanks again for your quick response.