All things Shopify and commerce
I have a Metaobject for Ingredients with a name, icon, etc. I have a Products metafield Product List for related products. Within the Product List loop, I would like to also iterate through the Ingredient Metobject List and get the name and icon. All my attempts to do this are returning null.
Outside of this Product List metafiled loop, I can fetch the info fine, via liquid or using blocks. But I need to fetch it from the realted products metafield, not the main product.
How can I modify second loop?
First loop:
Solved! Go to the solution
This is an accepted solution.
product_ref and .value fixed it for me. for anyone who is similarly frustrated.
Hey @jpoulos
To fetch ingredients for each product within the product list metafield loop, you’ll need to adjust your Liquid code to retrieve feature ingredients featured-ingredients for each product within your product.metafields.custom.pair with loop rather than for the main product directly. Here’s how you can do it by moving the featured_ingredients assignment inside the loop, so it references each product individually:
Adjusted Code with Nested Loop:
{%- for product in product.metafields.custom.pair_with.value limit: 4 -%}
<div class="swiper-slide card-product-slider__slide">
{%- render 'card-pair-with' with product_ref: product, section_id: section.id, card_index: forloop.index, card_product_layout: settings.card_product_layout -%}
{% assign featured_ingredients = product.metafields.custom.featured_ingredients %}
<div class="hover-info d-flex flex-wrap">
{% if featured_ingredients %}
{% for ingredient_id in featured_ingredients %}
{% assign ingredient = ingredient_id | metaobject %}
{% if ingredient %}
<div class="col-12 col-sm-6 d-flex flex-column justify-content-end align-items-center">
<div class="icon">
<img
class="grid-product__image lazyloaded"
src="{{ ingredient.icon | image_url }}"
alt="{{ ingredient.ingredient }}"
>
</div>
<div class="stat">{{ ingredient.ingredient }}</div>
</div>
{% endif %}
{% endfor %}
{% else %}
<p>No ingredients found.</p>
{% endif %}
</div>
</div>
{%- endfor -%}
This approach ensures that you’re accessing featured_ingredients for each product dynamically rather than statically from the main product.
If I was able to help you, please don't forget to Like and mark it as the Solution!
If you’re looking for expert help with customization or coding, I’d be delighted to support you. Please don’t hesitate to reach out via the email in my signature below—I’m here to help bring your vision to life!
Best Regard,
Rajat Sharma
Thanks Rajweb, will also try this and let you know. I did find a different fix that worked as well.
If I was able to help you, please don't forget to Like and mark it as the Solution!
thanks
For my situation your answer is not the solution.
This works:
{% assign featured_ingredients = product_ref.metafields.custom.featured_ingredients.value %}
This does not:
{% assign featured_ingredients = product.metafields.custom.featured_ingredients.value %}
This is an accepted solution.
product_ref and .value fixed it for me. for anyone who is similarly frustrated.
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024