How to dynamically display variant metafields in Shopify Expanse theme?

Topic summary

Issue: A Shopify store owner needed to dynamically display variant-specific metafields (predicted arrival dates) on product pages using the Expanse theme. The metafield displayed correctly but didn’t update when users selected different product variants.

Attempted Solution: The user duplicated the existing variant SKU code and modified it to render variant metafields, creating a custom <variant-meta> element with associated JavaScript to listen for variant changes.

Troubleshooting Process:

  • Another user requested store access and suggested checking console errors and verifying the variant change script was firing
  • Investigation revealed the <variant-meta> tag wasn’t being rendered in the DOM
  • The custom variant-meta.js file wasn’t loading/initializing properly in the document

Resolution: The original poster solved the issue independently by:

  • Using Chrome’s debug console to confirm the JavaScript file wasn’t initializing
  • Extracting the script from variant-meta.js and adding it to another JavaScript file that was already loading successfully
  • After this change, the variant metafield updated dynamically as expected

Status: Resolved. The solution involved relocating the JavaScript code rather than fixing the module import issue.

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

I am trying to display a variant metafield that has a “predicted date of arrival” on my product pages. I have already created the variant metafield and added the values.

I am also able to display the current variant metafield in my product pages. However, the value does NOT change when I select a different variant.

I am using the Expanse theme and I have duplicated the code for displaying variant SKUs in the product pages. I have edited this code to render the variant metafield.

Can someone check my code and help me fix the issue? I’d really appreciate it.

Here is the working code for Variant SKUs:

{%- liquid
  assign product = section.settings.product | default: product
-%}

<variant-sku data-product-id="{{ product.id }}" data-section-id="{{ section.id }}">
  {%- if variant.sku != blank -%}
    SKU: {{ variant.sku }}
  {%- endif -%}
</variant-sku>

<script type="module">  import 'components/variant-sku'
</script>

import { EVENTS, subscribe } from '@archetype-themes/utils/pubsub'

class VariantSku extends HTMLElement {
  connectedCallback() {
    this.variantChangeUnsubscriber = subscribe(
      `${EVENTS.variantChange}:${this.dataset.sectionId}:${this.dataset.productId}`,
      this.handleVariantChange.bind(this)
    )
  }

  disconnectedCallback() {
    this.variantChangeUnsubscriber?.()
  }

  handleVariantChange({ detail }) {
    const { html, sectionId, variant } = detail

    if (!variant) {
      this.textContent = ''
      return
    }

    const skuSource = html.querySelector(`[data-section-id="${sectionId}"] variant-sku`)

    if (skuSource) {
      this.textContent = skuSource.textContent
    }
  }
}

customElements.define('variant-sku', VariantSku)

Here is my NON working code for displaying the Variant Metafield:

{%- liquid
  assign product = section.settings.product | default: product
-%}

<variant-meta data-product-id="{{ product.id }}" data-section-id="{{ section.id }}">
   ( ships approx: {{ variant.metafields.variant.date | replace: 'predicted-arrival-','' }} )
</variant-meta>

<script type="module">  import 'components/variant-metafield'
</script>

import { EVENTS, subscribe } from '@archetype-themes/utils/pubsub'

class VariantMeta extends HTMLElement {
  connectedCallback() {
    this.variantChangeUnsubscriber = subscribe(
      `${EVENTS.variantChange}:${this.dataset.sectionId}:${this.dataset.productId}`,
      this.handleVariantChange.bind(this)
    )
  }

  disconnectedCallback() {
    this.variantChangeUnsubscriber?.()
  }

  handleVariantChange({ detail }) {
    const { html, sectionId, variant } = detail

    if (!variant) {
      this.textContent = ''
      return
    }

    const metaSource = html.querySelector(`[data-section-id="${sectionId}"] variant-meta`)

    if (metaSource) {
      this.textContent = metaSource.textContent
    }
  }
}

customElements.define('variant-meta', VariantMeta)

Hi @AugustJr ! Could you please provide the link to your store and password if that is required? Are you receiving any errors in the console? Have you tried adding any console.log()s to see if they appear in the console to test your metafields script?

One thing that comes to mind, have you tried to hook into the current SKU variant change script? If you know it is already firing for the SKUs it might be worth a try. If that doesn’t work you could always create a custom script to listen for the variant change and then update variant metafield.

If I managed to help you then, don’t forget to Like it and Mark it as the Solution!

Kind regards,

Mark

Hi @NovakNorth906

Here’s the URL to my store - https://oneworldcollection.com.au/
And here’s a product with variants that has the “variant.date” metafield - Airlie 3 Seater Slip Cover Sofa | OneWorld Collection – OneWorld Collection

As you can see on the product page, there is a “Pre Order: (date)” text.
This is where the variant.metafields.variant.date is displaying and should dynamically change based on the variant selected.

Currently, it only displays the first_selected variant metafield and this value won’t change even after selecting a different variant.
You’ll also notice that the SKU (below the product title) is dynamic and changes when a different variant is selected.

The variant SKU liquid code / js is where I got the code that I am using for my variant metafields.
So, there could be a conflict with it.

@AugustJr Thanks for providing the info! I just took a look in chrome dev tools and it appears that the tag you created is not being rendered. When I inspected the date metafield here is what is being rendered.


   ( ships approx: 15-Nov )
 

This might be where your problem is coming from because your query selector can’t find the element. Can you verify that this has been included into your theme file correctly?

{%- liquid
  assign product = section.settings.product | default: product
-%}

Hi @NovakNorth906

Apologies.

I added the liquid codes that should render the variant metafield on a duplicate theme.
The LIVE theme doesn’t have these codes.

Here’s a preview link of the duplicate theme where I added the codes - https://qi8df9yxtex7ab94-2754609263.shopifypreview.com
Can you check this instead? Thank you for your help.

@AugustJr Feeling hopeful that this might work! Try replacing your script tag with the following below the liquid you are using to render the variant metafield.


If I managed to help you then, don’t forget to Like it and Mark it as the Solution!

Kind regards,

Mark

Hi @NovakNorth906

I changed the script tag, but it didn’t work.

I have attached screenshots of the 3 liquid files I am working on.
Maybe you can check these screenshot and spot the issue.

Again, thank you so much for your time.

I have managed to solve this issue on my own.
The issue was that the file variant-meta.js wasn’t getting loaded into the document.

I checked this using the debug console on Chrome and saw that the file wasn’t initialized.
For some reason, I don’t know why the variant-meta.js isn’t getting loaded.

What I did to fix the issue was extract the script from the variant-meta.js file and add it to another file that I knew was getting loaded.
Once I did this, everything worked perfectly.

Anyway, hope this will help others.

1 Like

@AugustJr Glad you were able to get it figured out!!