Please help me with this variant metafield code fix

Topic summary

A Shopify store owner using the Horizon 2.0 theme needs variant-specific measurements (Queen vs. King bed dimensions) to display dynamically in a custom accordion block. Currently, the measurements only update after a page refresh when switching variants.

The Problem:

  • Custom accordion connects to variant metafields (custom.measurements)
  • Variant selection doesn’t trigger real-time content updates
  • Horizon theme only updates limited properties by default on variant changes

The Solution:
A working fix was provided using a custom liquid block with JavaScript:

  • Replace default accordion text blocks with a “Custom liquid” block
  • Implement a variant-data custom element that listens for variant:update events
  • The JavaScript automatically updates content when variants change without page refresh
  • Code example and live demo link included

Status: Resolved with a complete code solution that extends Horizon’s functionality to support dynamic variant metafield updates.

Summarized with AI on October 23. AI used: claude-sonnet-4-5-20250929.

By default, Horizon updates only a handful of properties on product page with variant selection.
And it is not possible to use variant metafields as Dynamic sources.

It’s possible though to extend theme functionality by adding a “Custom liquid” block which will update its content on variant change.

You can do it like this – Add an “Accordion Row” block, but instead of default text blocks add a “Custom liquid” one:

Into “Custom liquid” block paste the code like this – you need to change it to use your metafield:

{% # the ID parameter must use uniq on the page %}
<variant-data class="rte" id="availability-{{section.id}}">
  {% #  output your metafield value %}
  {{ closest.product.selected_or_first_available_variant.metafields.availability["14-Lyndon-Street"] }}
</variant-data>

<script>
  if (!customElements.get('variant-data')) {
    class VariantData extends HTMLElement {
      constructor() { super(); }
      connectedCallback(){
        document.addEventListener('variant:update', ({detail}) => {
          const newContent = detail.data?.html?.getElementById(this.id)?.innerHTML;
          if (newContent) this.innerHTML = newContent;
        });
      }
    }
    customElements.define('variant-data', VariantData )
  };
</script>

The Javascript code here will listen to events fired by the Horizon theme code and update this variant-data element accordingly.

See it working here.

Similar solution for Dawn family themes: Variant Metafield reference is Giving Invalid Error - #3 by tim_1


if my post is helpful, please like it ♡ and mark as a solution -- this will help others find it
1 Like