how do I make the sku section on product page dynamic?

Solved

how do I make the sku section on product page dynamic?

michelle012
Excursionist
52 0 9

I added this code to the product.liquid with makes the sku visible bunt not dynamic without refreshing the page.

 

{%- when 'sku' -%}
<p
class="product__sku{% if block.settings.text_style == 'uppercase' %} caption-with-letter-spacing{% elsif block.settings.text_style == 'subtitle' %} subtitle{% endif %}{% if product.selected_or_first_available_variant.sku.size == 0 %} visibility-hidden{% endif %}"
id="Sku-{{ section.id }}"
role="status"
{{ block.shopify_attributes }}
>
<span class="visually-hidden">{{ 'products.product.sku' | t }}:</span>
{{- product.selected_or_first_available_variant.sku -}}
</p>

 

does anyone have a solution on how the make the custom sku section dynamic?

Accepted Solution (1)
tim
Shopify Partner
4329 500 1589

This is an accepted solution.

The idea is that you go to your themes, click "Customize", the go to product page and add a block or a section "Liquid", then paste my code in this block/section setting.

 

Otherwise, you can find your "sku" block and add my code right below.

 

At the moment, I do not see my code included on your product page(s).

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com

View solution in original post

Replies 9 (9)

StudioEnchant
Shopify Partner
277 45 45

Hey @michelle012,

I would love to help out but I'm not sure what you are trying to achieve, so if you could ellaborate a little more and maybe provide an example from another website it will be great.

If you mean to just show the SKU on the product page, you can add that block inside your theme's customization, just edit the product template.

Sincerely,
Lior from Studio Enchant

If we helped you please Like this reply and Mark it as Solution! ❤️

Chat with us on WhatsApp


Click here to enjoy 3 months of Shopify for $1/month on select plans.
michelle012
Excursionist
52 0 9

thankyou for your responds. normally when you have a sku on the product page with multiple variants the sku should switch dynamically when another variant is selected. this is not the case with my sku. so my question is how to make the sky dynamically change when another variant is selected

Dotsquares
Shopify Partner
390 25 52

hi @michelle012 


To make the SKU section dynamic—so it updates automatically when a customer selects a different variant—you'll need to hook into Shopify's variant change event using a bit of JavaScript.

Here’s a quick way to make your custom SKU section update without a page refresh:

 

1. Add an ID to your SKU container (you already did this – perfect!):

<p id="product-sku">{{ product.selected_or_first_available_variant.sku }}</p>

 

2. Add this JavaScript to your theme (either at the bottom of product.liquid or in your main JS file):

document.addEventListener('DOMContentLoaded', function () {
  const skuElement = document.getElementById('product-sku');

  document.querySelector('form[action^="/cart/add"]')?.addEventListener('change', function (event) {
    const selectedVariantId = document.querySelector('[name="id"]')?.value;

    if (!selectedVariantId || !window.meta || !meta.product) return;

    const selectedVariant = meta.product.variants.find(v => v.id == selectedVariantId);

    if (selectedVariant && skuElement) {
      skuElement.textContent = selectedVariant.sku || '';
    }
  });
});

 

This script listens for variant changes and updates the SKU text accordingly.

Dotsquares Ltd


Problem Solved? ✔ Accept and Like solution to help future merchants.


Shopify Partner Directory | Trustpilot | Portfolio
michelle012
Excursionist
52 0 9

I tried your solution but are still not able to solve the issue. Hope you have another way around

tim
Shopify Partner
4329 500 1589

Did not expect Pipeline to not have a "SKU" block, sorry.

 

Create a "Liquid" block and add this code:

<script>
if ('function' != typeof updateSKU) {
  window.updateSKU = (evt) => {
    let container = evt.target;
    let variant = evt.detail.variant;
    if (!container || !variant) return;
    let skuElement = container.closest('.product__main__content')?.querySelector('.product__sku');
    if (skuElement) skuElement.innerHTML = `<span class="visually-hidden">SKU:</span>${ variant.sku}`;
  };

  document.addEventListener('theme:variant:change', updateSKU);
}
</script>
If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
michelle012
Excursionist
52 0 9

hi thankyou for your response. I tried and added the code to another block but still no result of the dynamic change. hoping you may have another solution

tim
Shopify Partner
4329 500 1589

Can I see the link where my code is added?

 

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
michelle012
Excursionist
52 0 9

I added the code in a snippet

tim
Shopify Partner
4329 500 1589

This is an accepted solution.

The idea is that you go to your themes, click "Customize", the go to product page and add a block or a section "Liquid", then paste my code in this block/section setting.

 

Otherwise, you can find your "sku" block and add my code right below.

 

At the moment, I do not see my code included on your product page(s).

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com