Access product field from variation field

cn flag

There are a bunch of product images that are named same as SKU, so I'm just using field--commerce-product-variation--sku.html.twig to show them. All is fine.

I'd like to use the parent product image as fallback image for when the SKU image doesn't exist.

How do I access a parent product field from the product variation field template?

ru flag

You'll get the variation object in element['#object']

{% set variation_object = element['#object'] %}
{% set product_object = variation_object.product_id.entity %}

then from product_object you can access the image field

cn flag
That worked! Thanks. Then you can access image URI as `product_object.field_product_img.0.entity.uri.0.value`

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.