How to dynamically display variant metafields in Shopify Expanse theme?

Solved

How to dynamically display variant metafields in Shopify Expanse theme?

AugustJr
Shopify Partner
10 1 1

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 }}">
  &nbsp;( 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)


 

Accepted Solution (1)
AugustJr
Shopify Partner
10 1 1

This is an accepted solution.

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.


View solution in original post

Replies 8 (8)

NovakNorth906
Shopify Partner
41 4 5

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

AugustJr
Shopify Partner
10 1 1

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.

NovakNorth906
Shopify Partner
41 4 5

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

<div id="">
  &nbsp;( ships approx: 15-Nov )
 </div>

  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
-%}

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

<script type="module">  import 'components/variant-metafield'
</script>
AugustJr
Shopify Partner
10 1 1

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.

NovakNorth906
Shopify Partner
41 4 5

@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.

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

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

 

Kind regards,

Mark

AugustJr
Shopify Partner
10 1 1

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.

ss-1460.pngss-1461.pngss-1462.png

AugustJr
Shopify Partner
10 1 1

This is an accepted solution.

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.


NovakNorth906
Shopify Partner
41 4 5

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