Why can't I access product variant metafields from Insert Dynamic Source?

Like the title says: Why can’t I access product variant metafields from Insert Dynamic Source for a product?
Is this something I have to add support for as a developer? Or is it only possible to access these programmatically?

Thanks!

Hi @SvenSvensson ,

Nice to see you again! It’s not possible to use the variant metafields as the dynamic data source. Yep, a bummer. But the good news is that you can do that with a proper combination of JavaScript and Liquid code. Here’s the link to a solved relevant thread.

1 Like

Hi!

Thanks for confirming that. Have they given any good reason why? I would guess it’s because not all products are variants, so they can’t be resolved.
But if it’s accessible from the liquid code, obviously they could technically solve this if they wanted to.
For example by using inheritance. If the product and the variant share the same metafield they could always fallback. Or just ignore the it if it’s not accessible for a non-variant product. Empty field or no field at all should not matter.

That’s a good question. I guess it’s due to the technical limitation of Shopify Liquid. The thing is that Liquid is preprocessed (compiled), so it can’t track the changes after the code it generates is sent to the browser. In other words, Liquid wouldn’t know that the active variant as changed until the page is refreshed. Once you refresh the product page, it would parse the active variant ID based on the product URL and you’ll be able to access the variant data with Liquid. However, refreshing the product page every time the active variant is changed is definitely not a great solution. And that’s where some JavaScript code comes handy as it allows you to manipulate the data dynamically.