Match recipes to products

Solved

Match recipes to products

3pppz
Shopify Partner
14 1 6

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.

 

Screenshot 2024-05-28 at 4.51.22 PM.png

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>

Screenshot 2024-05-29 at 4.32.14 PM.png

 where am I going wrong?

Accepted Solution (1)

tim
Shopify Partner
3765 351 1386

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.

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com

View solution in original post

Replies 2 (2)

tim
Shopify Partner
3765 351 1386

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.

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
3pppz
Shopify Partner
14 1 6

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>