Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
So i have created product list metafield in my recipe blog posts so I can add which products from my store have been used in the recipe.
Now I want in each product to have a dropdown that will show the recipes its been used in
I added this code in main-product.liquid
<select id="recipe-dropdown">
<option value="">Select Recipe</option>
{% assign current_product_id = product.id %}
{% for post in blogs.recipes.articles %}
<p>Checking {{ post.title }} for related products...</p>
{% assign product_list = article.metafields.custom.ingredients | split: ',' %}
{% if product_list contains current_product_id %}
<option value="{{ post.url }}">{{ post.title }}</option>
{% endif %}
{% endfor %}
</select>
this is what Its rendering
<select id="recipe-dropdown">
<option value="">Select Recipe</option>
</select>
where am I going wrong?
Solved! Go to the solution
This is an accepted solution.
What you're getting from your metafield is a list of product references like
["gid://shopify/Product/4096776437814","gid://shopify/Product/4096747634742","gid://shopify/Product/4096776306742"]
However, your product.id is a number, like 4096776437814 and this is why you do not have a match.
The following should work, but this is still not a recommended way to access metafield.
{% if article.metafields.custom.ingredients contains product.id %}
Proper way would be
{% for p in article.metafields.custom.ingredients.value %}
{% if p.id == product.id %}
or, I believe even like this
{% for p in article.metafields.custom.ingredients.value %}
{% if p == product %}
or, probably faster (since no loop)
{% assign ingredient_ids = article.metafields.custom.ingredients.value | map: 'id' %}
{% if ingredient_ids contains product.id %}
Now, just a reminder that when you're looping over all articles you may hit the pagination limit of 50.
This is an accepted solution.
What you're getting from your metafield is a list of product references like
["gid://shopify/Product/4096776437814","gid://shopify/Product/4096747634742","gid://shopify/Product/4096776306742"]
However, your product.id is a number, like 4096776437814 and this is why you do not have a match.
The following should work, but this is still not a recommended way to access metafield.
{% if article.metafields.custom.ingredients contains product.id %}
Proper way would be
{% for p in article.metafields.custom.ingredients.value %}
{% if p.id == product.id %}
or, I believe even like this
{% for p in article.metafields.custom.ingredients.value %}
{% if p == product %}
or, probably faster (since no loop)
{% assign ingredient_ids = article.metafields.custom.ingredients.value | map: 'id' %}
{% if ingredient_ids contains product.id %}
Now, just a reminder that when you're looping over all articles you may hit the pagination limit of 50.
thank you, this worked
<select id="recipe-dropdown" onchange="navigateToRecipe(this)">
<option value="">Select Recipe</option>
{% for post in blogs.recipes.articles %}
{% assign ingredient_ids = post.metafields.custom.ingredients.value | map: 'id' %}
{% if ingredient_ids contains product.id %}
<option value="{{ post.url }}">{{ post.title }}</option>
{% endif %}
{% endfor %}
</select>
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024