Reference metafields from one product on another product page in a for loop

Reference metafields from one product on another product page in a for loop

TothDigital
Shopify Partner
22 1 7

My store includes some combo products that bundle together other products. I have set product metafields for info like ingredients, instructions etc. On the bundle products, I want to be able to just pull the metafield values from the individual products and display them on the bundle product page...

 

The bundle products have a 'Product List' metafield that allows the merchant to select which products are included in the bundle. I want to loop through this list and display metafields from the chosen products. Something like: 

{% assign bundle_products = product.metafields.custom.bundle.value %}
   {% for bundle_product in bundle_products %}
   {{ bundle_product.metafields.custom.ingredients.value }}
   {% endfor %}

Is something like this possible? How can I reference another product's metafield without being on that product page?

 

Replies 3 (3)

MaxDesign
Shopify Partner
213 15 84

Yes it's possible. Did you give it a shot?

You can also reference another product this way (referencing the product handle):

{{ all_products['your-product-handle'].metafields.custom.ingredients.value }}

 

Reach out to me at admin@maxdesign.expert
TothDigital
Shopify Partner
22 1 7

I tried my example but couldn't get it to work. Your example works great for getting specific product's metafields. Now I'm just trying to figure out how to make it work in a for loop

DavidCouillard
Shopify Partner
10 1 8

Using @MaxDesign example, I got it to work this way:

{% for product_related in product.metafields.custom.related_products.value %}
  {% assign product_handle = product_related.handle %}
  {{ all_products[product_handle].metafields.custom.product_style.value }}
{%- endfor %}

 
Where my related products Key is "related_products".
And the related product's Metafield Key is "product_style".

David Couillard
Ecommerce Marketing Consultant