Personalized checkout and custom promotions with Shopify Scripts
Hello,
I would like to show conditional metafield for each cart item added inside cart. I created a for loop with if condition. code is working but for loop prints all metafields for each cart item rather than relevant metafield value. See the attached screenshot. Thanks for the help
{% for item in cart.items %}
{% if item.variant.metafields.variant.stocked_sku %}
<p>Usually Ships in {{ item.variant.metafields.variant.stocked_lead_time }} Days</p>
{% else %}
<p>Made to Order: Usually Ships in {{ item.variant.metafields.variant.mto_lead_time }} Days</p>
{% endif %}
{% endfor %}
Why is your code within a for loop?
The cart page would already be looping over the items so it looks like you are adding a second loop that isn't needed. Do you get better results if you remove the for loop and just leave the contents of it?
Your code also assumes that either of those metafields exist and it might not always be the case. Adding something to catch that (and just skip showing anything) would be a good idea.
I also want to know how to solve this problem!
2m ago Learn the essential skills to navigate the Shopify admin with confidence. T...
By Shopify Feb 12, 2025Learn how to expand your operations internationally with Shopify Academy’s learning path...
By Shopify Feb 4, 2025Hey Community, happy February! Looking back to January, we kicked off the year with 8....
By JasonH Feb 3, 2025