How to prevent automatic image variant switching on load (Studio Theme)

Topic summary

A user encountered an issue with the Shopify Studio theme where variant images automatically replaced the main product image on page load, rather than displaying the primary product image first.

Problem Context:

  • The default Shopify behavior uses JavaScript to swap images when variants are selected
  • Existing solutions for the Dawn theme weren’t applicable to Studio theme
  • The user needed custom code modifications but couldn’t locate the correct files

Attempted Solutions:

  • One suggestion involved adding JavaScript to the theme.liquid file to force image reset after variant changes (unsuccessful)
  • Another approach recommended modifying the main product template with conditional logic to skip image changes on first load

Working Solution:
The issue was resolved by modifying the product-media-gallery.liquid file. The key change involved:

  • Adding assign featured_media = product.featured_media to the liquid logic
  • Updating the conditional logic to check if media.id == featured_media.id and forloop.index == 1 instead of checking for null variant featured media

This ensures the main product image displays first on page load, preventing automatic variant image switching. The user confirmed this solution worked perfectly.

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

Hi,

whenever I select the link with image variants the main product image is not displayed first. The theme I am using is “Studio”.

Ideally I would like the Product Image to be on display first and to prevent automatic switching of variant images.

I have spent several hours on the web, but the references to suggested code changes cannot be found in the Studio theme.

I would appreciate any suggestion to help remedy this little problem.

Thanks

@KDee4411

This is the default behavior of Shopify theme. It uses JavaScript to swaps images when variants are selected. You need to customize the JavaScript and Liquid code to prevent automatic image variant switching when load.

Hello how are you doing are you active with your store

Hi,

Hope this will help

  • Locate Main Product Template (something sections/main-product.liquid or sections/product.liquid)
  • Find Where Variant Images Are Swapped
  • Disable Auto-Switching on Page Load

Javascript example

let firstLoad = true;

function updateVariantImage(variant) {
  if (firstLoad) {
    firstLoad = false;
    return; // Skip image change
  }

  // Normal image switching code here
}

  • Alternative: Remove Selected Variant from URL

Hi, Thanks for the reply, fully understand this is a code change, I just dont know what to change. As previously mentioned, after a lot of research I am unable to find the correct code for the theme ‘Studio’; there are plenty of references to ‘Dawn’, but the suggested code for ‘Dawn’ cannot be replicated in ‘Studio’, the associated reference code cannot be found..

Any ideas of the correct code to use?

@KDee4411

Yes, you’re right there is no exact code available for Studio theme because It needs to check the theme code and check where the problem exists.

But if you want to try the code then here it is. Please add the following code inside the head section of theme.liquid file.

<script>
  document.addEventListener('variant:change', function(e) {
    const firstImage = document.querySelector('[data-media-id]');
    if (firstImage) {
      firstImage.click(); 
    }
  });
</script>

I am not sure if this works for you but this can force reset the image after variant change. Hope this helps!

I’m truly grateful you took the time to respond, thank you!

‎Here’s exactly how I’ll help your store drive consistent, high-converting traffic and increase daily sales:

‎1. Drive Buyers Actively Searching for Your Products
‎I’ll run strategic campaigns through Google’s search engine to attract visitors who are already looking for what you sell. This means more targeted traffic, higher engagement, and better chances of converting them into customers.

‎2. Optimize Your Store for Conversions
‎I’ll fully audit and improve your store’s user experience, enhancing product pages, checkout flow, and calls to action, so that every step a visitor takes moves them closer to making a purchase.

‎3. Analyze and Adjust for Daily Sales Growth
‎By tracking your store analytics, I’ll uncover where visitors drop off, what’s working, and what’s not, then make updates to convert more visitors into loyal buyers consistently.

‎Can we get started with this now?

Hi @KDee4411,

Please go to Actions > Edit code > snippets > product-media-gallery.liquid file. Please send me the code of the file, I will check it for you

{% comment %}
Renders a product media gallery. Should be used with ‘media-gallery.js’
Also see ‘product-media-modal’

Accepts:

  • product: {Object} Product liquid object
  • variant_images: {Array} Product images associated with a variant
  • limit: {Number} (optional) When passed, limits the number of media items to render

Usage:
{% render ‘product-media-gallery’ %}
{% endcomment %}

{%- liquid
if section.settings.hide_variants and variant_images.size == product.media.size
assign single_media_visible = true
endif

if limit == 1
assign single_media_visible = true
endif

assign media_count = product.media.size
if section.settings.hide_variants and media_count > 1 and variant_images.size > 0
assign media_count = media_count | minus: variant_images.size | plus: 1
endif

if media_count == 1 or single_media_visible
assign single_media_visible_mobile = true
endif

if media_count == 0 or single_media_visible_mobile or section.settings.mobile_thumbnails == ‘show’ or section.settings.mobile_thumbnails == ‘columns’ and media_count < 3
assign hide_mobile_slider = true
endif

if section.settings.media_size == ‘large’
assign media_width = 0.65
elsif section.settings.media_size == ‘medium’
assign media_width = 0.55
elsif section.settings.media_size == ‘small’
assign media_width = 0.45
endif
-%}

<media-gallery
id=“MediaGallery-{{ section.id }}”
role=“region”
{% if section.settings.enable_sticky_info %}
class=“product__column-sticky”
{% endif %}
aria-label=“{{ ‘products.product.media.gallery_viewer’ | t }}”
data-desktop-layout=“{{ section.settings.gallery_layout }}”

{{ 'accessibility.skip_to_product_info' | t }}
    {%- if product.selected_or_first_available_variant.featured_media != null -%} {%- assign featured_media = product.selected_or_first_available_variant.featured_media -%}
  • {%- assign media_position = 1 -%} {% render 'product-thumbnail', media: featured_media, media_count: media_count, position: media_position, desktop_layout: section.settings.gallery_layout, mobile_layout: section.settings.mobile_thumbnails, loop: section.settings.enable_video_looping, modal_id: section.id, xr_button: true, media_width: media_width, media_fit: section.settings.media_fit, constrain_to_viewport: section.settings.constrain_to_viewport, lazy_load: false %}
  • {%- endif -%} {%- for media in product.media -%} {% if media_position >= limit or media_position >= 1 and section.settings.hide_variants and variant_images contains media.src %} {% continue %} {% endif %}
        {%- unless media.id == product.selected_or_first_available_variant.featured_media.id -%}
          <li
            id="Slide-{{ section.id }}-{{ media.id }}"
            class="product__media-item grid__item slider__slide{% if single_media_visible %} product__media-item--single{% endif %}{% if product.selected_or_first_available_variant.featured_media == nil and forloop.index == 1 %} is-active{% endif %}{% if media.media_type != 'image' %} product__media-item--full{% endif %}{% if settings.animations_reveal_on_scroll %} scroll-trigger animate--fade-in{% endif %}"
            data-media-id="{{ section.id }}-{{ media.id }}"
          >
            {%- liquid
              assign media_position = media_position | default: 0 | plus: 1
              assign lazy_load = false
              if media_position > 1
                assign lazy_load = true
              endif
            -%}
            {% render 'product-thumbnail',
              media: media,
              media_count: media_count,
              position: media_position,
              desktop_layout: section.settings.gallery_layout,
              mobile_layout: section.settings.mobile_thumbnails,
              loop: section.settings.enable_video_looping,
              modal_id: section.id,
              xr_button: true,
              media_width: media_width,
              media_fit: section.settings.media_fit,
              constrain_to_viewport: section.settings.constrain_to_viewport,
              lazy_load: lazy_load
            %}
          </li>
        {%- endunless -%}
      {%- endfor -%}
    </ul>
    <div class="slider-buttons quick-add-hidden{% if hide_mobile_slider %} small-hide{% endif %}">
      <button
        type="button"
        class="slider-button slider-button--prev"
        name="previous"
        aria-label="{{ 'general.slider.previous_slide' | t }}"
      >
        <span class="svg-wrapper">
          {{- 'icon-caret.svg' | inline_asset_content -}}
        </span>
      </button>
      <div class="slider-counter caption">
        <span class="slider-counter--current">1</span>
        <span aria-hidden="true"> / </span>
        <span class="visually-hidden">{{ 'general.slider.of' | t }}</span>
        <span class="slider-counter--total">{{ media_count }}</span>
      </div>
      <button
        type="button"
        class="slider-button slider-button--next"
        name="next"
        aria-label="{{ 'general.slider.next_slide' | t }}"
      >
        <span class="svg-wrapper">
          {{- 'icon-caret.svg' | inline_asset_content -}}
        </span>
      </button>
    </div>
    
    {%- if first_3d_model -%} {{- 'icon-3d-model.svg' | inline_asset_content -}} {{ 'products.product.xr_button' | t }} {%- endif -%} {%- liquid assign is_not_limited_to_single_item = false if limit == null or limit > 1 assign is_not_limited_to_single_item = true endif -%} {%- if is_not_limited_to_single_item and media_count > 1 and section.settings.gallery_layout contains 'thumbnail' or section.settings.mobile_thumbnails == 'show' -%} {{- 'icon-caret.svg' | inline_asset_content -}}
      {%- capture sizes -%} (min-width: {{ settings.page_width }}px) calc(({{ settings.page_width | minus: 100 | times: media_width | round }} - 4rem) / 4), (min-width: 990px) calc(({{ media_width | times: 100 }}vw - 4rem) / 4), (min-width: 750px) calc((100vw - 15rem) / 8), calc((100vw - 8rem) / 3) {%- endcapture -%}
          {%- if featured_media != null -%}
            {%- liquid
              capture media_index
                if featured_media.media_type == 'model'
                  increment model_index
                elsif featured_media.media_type == 'video' or featured_media.media_type == 'external_video'
                  increment video_index
                elsif featured_media.media_type == 'image'
                  increment image_index
                endif
              endcapture
              assign media_index = media_index | plus: 1
            -%}
            <li
              id="Slide-Thumbnails-{{ section.id }}-0"
              class="thumbnail-list__item slider__slide{% if section.settings.hide_variants and variant_images contains featured_media.src %} thumbnail-list_item--variant{% endif %}"
              data-target="{{ section.id }}-{{ featured_media.id }}"
              data-media-position="{{ media_index }}"
            >
              {%- capture thumbnail_id -%}
                Thumbnail-{{ section.id }}-0
              {%- endcapture -%}
              <button
                class="thumbnail global-media-settings global-media-settings--no-shadow"
                aria-label="{%- if featured_media.media_type == 'image' -%}{{ 'products.product.media.load_image' | t: index: media_index }}{%- elsif featured_media.media_type == 'model' -%}{{ 'products.product.media.load_model' | t: index: media_index }}{%- elsif featured_media.media_type == 'video' or featured_media.media_type == 'external_video' -%}{{ 'products.product.media.load_video' | t: index: media_index }}{%- endif -%}"
                aria-current="true"
                aria-controls="GalleryViewer-{{ section.id }}"
                aria-describedby="{{ thumbnail_id }}"
              >
                {{
                  featured_media.preview_image
                  | image_url: width: 416
                  | image_tag:
                    loading: 'lazy',
                    sizes: sizes,
                    widths: '54, 74, 104, 162, 208, 324, 416',
                    id: thumbnail_id,
                    alt: featured_media.alt
                  | escape
                }}
              </button>
            </li>
          {%- endif -%}
          {%- for media in product.media -%}
            {%- unless media.id == product.selected_or_first_available_variant.featured_media.id -%}
              {%- liquid
                capture media_index
                  if media.media_type == 'model'
                    increment model_index
                  elsif media.media_type == 'video' or media.media_type == 'external_video'
                    increment video_index
                  elsif media.media_type == 'image'
                    increment image_index
                  endif
                endcapture
                assign media_index = media_index | plus: 1
              -%}
              <li
                id="Slide-Thumbnails-{{ section.id }}-{{ forloop.index }}"
                class="thumbnail-list__item slider__slide{% if section.settings.hide_variants and variant_images contains media.src %} thumbnail-list_item--variant{% endif %}"
                data-target="{{ section.id }}-{{ media.id }}"
                data-media-position="{{ media_index }}"
              >
                {%- if media.media_type == 'model' -%}
                  <span class="thumbnail__badge" aria-hidden="true">
                    <span class="svg-wrapper">
                      {{- 'icon-3d-model.svg' | inline_asset_content -}}
                    </span>
                  </span>
                {%- elsif media.media_type == 'video' or media.media_type == 'external_video' -%}
                  <span class="thumbnail__badge" aria-hidden="true">
                    <span class="svg-wrapper">
                      {{- 'icon-play.svg' | inline_asset_content -}}
                    </span>
                  </span>
                {%- endif -%}
                {%- capture thumbnail_id -%}
                  Thumbnail-{{ section.id }}-{{ forloop.index }}
                {%- endcapture -%}
                <button
                  class="thumbnail global-media-settings global-media-settings--no-shadow"
                  aria-label="{%- if media.media_type == 'image' -%}{{ 'products.product.media.load_image' | t: index: media_index }}{%- elsif media.media_type == 'model' -%}{{ 'products.product.media.load_model' | t: index: media_index }}{%- elsif media.media_type == 'video' or media.media_type == 'external_video' -%}{{ 'products.product.media.load_video' | t: index: media_index }}{%- endif -%}"
                  {% if media == product.selected_or_first_available_variant.featured_media
                    or product.selected_or_first_available_variant.featured_media == null
                    and forloop.index == 1
                  %}
                    aria-current="true"
                  {% endif %}
                  aria-controls="GalleryViewer-{{ section.id }}"
                  aria-describedby="{{ thumbnail_id }}"
                >
                  {{
                    media.preview_image
                    | image_url: width: 416
                    | image_tag:
                      loading: 'lazy',
                      sizes: sizes,
                      widths: '54, 74, 104, 162, 208, 324, 416',
                      id: thumbnail_id,
                      alt: media.alt
                    | escape
                  }}
                </button>
              </li>
            {%- endunless -%}
          {%- endfor -%}
        </ul>
        <button
          type="button"
          class="slider-button slider-button--next{% if media_count <= 3 %} small-hide{% endif %}{% if media_count <= 4 %} medium-hide large-up-hide{% endif %}"
          name="next"
          aria-label="{{ 'general.slider.next_slide' | t }}"
          aria-controls="GalleryThumbnails-{{ section.id }}"
          data-step="3"
        >
          <span class="svg-wrapper">
            {{- 'icon-caret.svg' | inline_asset_content -}}
          </span>
        </button>
      </slider-component>
      

      {%- endif -%}

Thanks, tried the suggested code, but it did not work… appreciate the help. Thx

1 Like

Hi @KDee4411,

Please change all code:

{% comment %}
  Renders a product media gallery. Should be used with 'media-gallery.js'
  Also see 'product-media-modal'

  Accepts:
  - product: {Object} Product liquid object
  - variant_images: {Array} Product images associated with a variant
  - limit: {Number} (optional) When passed, limits the number of media items to render

  Usage:
  {% render 'product-media-gallery' %}
{% endcomment %}

{%- liquid
  if section.settings.hide_variants and variant_images.size == product.media.size
    assign single_media_visible = true
  endif

  if limit == 1
    assign single_media_visible = true
  endif

  assign media_count = product.media.size
  if section.settings.hide_variants and media_count > 1 and variant_images.size > 0
    assign media_count = media_count | minus: variant_images.size | plus: 1
  endif

  if media_count == 1 or single_media_visible
    assign single_media_visible_mobile = true
  endif

  if media_count == 0 or single_media_visible_mobile or section.settings.mobile_thumbnails == 'show' or section.settings.mobile_thumbnails == 'columns' and media_count < 3
    assign hide_mobile_slider = true
  endif

  if section.settings.media_size == 'large'
    assign media_width = 0.65
  elsif section.settings.media_size == 'medium'
    assign media_width = 0.55
  elsif section.settings.media_size == 'small'
    assign media_width = 0.45
  endif

  assign featured_media = product.featured_media
-%}

<media-gallery
  id="MediaGallery-{{ section.id }}"
  role="region"
  {% if section.settings.enable_sticky_info %}
    class="product__column-sticky"
  {% endif %}
  aria-label="{{ 'products.product.media.gallery_viewer' | t }}"
  data-desktop-layout="{{ section.settings.gallery_layout }}"
>
  <div id="GalleryStatus-{{ section.id }}" class="visually-hidden" role="status"></div>
  <slider-component id="GalleryViewer-{{ section.id }}" class="slider-mobile-gutter">
    <a class="skip-to-content-link button visually-hidden quick-add-hidden" href="#ProductInfo-{{ section.id }}">
      {{ 'accessibility.skip_to_product_info' | t }}
    </a>
    <ul
      id="Slider-Gallery-{{ section.id }}"
      class="product__media-list contains-media grid grid--peek list-unstyled slider slider--mobile"
      role="list"
    >
      {%- if featured_media -%}
        <li
          id="Slide-{{ section.id }}-{{ featured_media.id }}"
          class="product__media-item grid__item slider__slide is-active{% if single_media_visible %} product__media-item--single{% endif %}{% if featured_media.media_type != 'image' %} product__media-item--full{% endif %}{% if settings.animations_reveal_on_scroll %} scroll-trigger animate--fade-in{% endif %}"
          data-media-id="{{ section.id }}-{{ featured_media.id }}"
        >
          {%- assign media_position = 1 -%}
          {% render 'product-thumbnail',
            media: featured_media,
            media_count: media_count,
            position: media_position,
            desktop_layout: section.settings.gallery_layout,
            mobile_layout: section.settings.mobile_thumbnails,
            loop: section.settings.enable_video_looping,
            modal_id: section.id,
            xr_button: true,
            media_width: media_width,
            media_fit: section.settings.media_fit,
            constrain_to_viewport: section.settings.constrain_to_viewport,
            lazy_load: false
          %}
        </li>
      {%- elsif product.selected_or_first_available_variant.featured_media != null -%}
        {%- assign featured_media = product.selected_or_first_available_variant.featured_media -%}
        <li
          id="Slide-{{ section.id }}-{{ featured_media.id }}"
          class="product__media-item grid__item slider__slide is-active{% if single_media_visible %} product__media-item--single{% endif %}{% if featured_media.media_type != 'image' %} product__media-item--full{% endif %}{% if settings.animations_reveal_on_scroll %} scroll-trigger animate--fade-in{% endif %}"
          data-media-id="{{ section.id }}-{{ featured_media.id }}"
        >
          {%- assign media_position = 1 -%}
          {% render 'product-thumbnail',
            media: featured_media,
            media_count: media_count,
            position: media_position,
            desktop_layout: section.settings.gallery_layout,
            mobile_layout: section.settings.mobile_thumbnails,
            loop: section.settings.enable_video_looping,
            modal_id: section.id,
            xr_button: true,
            media_width: media_width,
            media_fit: section.settings.media_fit,
            constrain_to_viewport: section.settings.constrain_to_viewport,
            lazy_load: false
          %}
        </li>
      {%- endif -%}
      {%- for media in product.media -%}
        {% if media_position >= limit
          or media_position >= 1
          and section.settings.hide_variants
          and variant_images contains media.src
        %}
          {% continue %}
        {% endif %}

        {%- unless media.id == featured_media.id -%}
          <li
            id="Slide-{{ section.id }}-{{ media.id }}"
            class="product__media-item grid__item slider__slide{% if single_media_visible %} product__media-item--single{% endif %}{% if featured_media == nil and forloop.index == 1 %} is-active{% endif %}{% if media.media_type != 'image' %} product__media-item--full{% endif %}{% if settings.animations_reveal_on_scroll %} scroll-trigger animate--fade-in{% endif %}"
            data-media-id="{{ section.id }}-{{ media.id }}"
          >
            {%- liquid
              assign media_position = media_position | default: 0 | plus: 1
              assign lazy_load = false
              if media_position > 1
                assign lazy_load = true
              endif
            -%}
            {% render 'product-thumbnail',
              media: media,
              media_count: media_count,
              position: media_position,
              desktop_layout: section.settings.gallery_layout,
              mobile_layout: section.settings.mobile_thumbnails,
              loop: section.settings.enable_video_looping,
              modal_id: section.id,
              xr_button: true,
              media_width: media_width,
              media_fit: section.settings.media_fit,
              constrain_to_viewport: section.settings.constrain_to_viewport,
              lazy_load: lazy_load
            %}
          </li>
        {%- endunless -%}
      {%- endfor -%}
    </ul>
    <div class="slider-buttons quick-add-hidden{% if hide_mobile_slider %} small-hide{% endif %}">
      <button
        type="button"
        class="slider-button slider-button--prev"
        name="previous"
        aria-label="{{ 'general.slider.previous_slide' | t }}"
      >
        <span class="svg-wrapper">
          {{- 'icon-caret.svg' | inline_asset_content -}}
        </span>
      </button>
      <div class="slider-counter caption">
        <span class="slider-counter--current">1</span>
        <span aria-hidden="true"> / </span>
        <span class="visually-hidden">{{ 'general.slider.of' | t }}</span>
        <span class="slider-counter--total">{{ media_count }}</span>
      </div>
      <button
        type="button"
        class="slider-button slider-button--next"
        name="next"
        aria-label="{{ 'general.slider.next_slide' | t }}"
      >
        <span class="svg-wrapper">
          {{- 'icon-caret.svg' | inline_asset_content -}}
        </span>
      </button>
    </div>
  </slider-component>
  {%- if first_3d_model -%}
    <button
      class="button button--full-width product__xr-button"
      type="button"
      aria-label="{{ 'products.product.xr_button_label' | t }}"
      data-shopify-xr
      data-shopify-model3d-id="{{ first_3d_model.id }}"
      data-shopify-title="{{ product.title | escape }}"
      data-shopify-xr-hidden
    >
      <span class="svg-wrapper">
        {{- 'icon-3d-model.svg' | inline_asset_content -}}
      </span>
      {{ 'products.product.xr_button' | t }}
    </button>
  {%- endif -%}
  {%- liquid
    assign is_not_limited_to_single_item = false
    if limit == null or limit > 1
      assign is_not_limited_to_single_item = true
    endif
  -%}
  {%- if is_not_limited_to_single_item
    and media_count > 1
    and section.settings.gallery_layout contains 'thumbnail'
    or section.settings.mobile_thumbnails == 'show'
  -%}
    <slider-component
      id="GalleryThumbnails-{{ section.id }}"
      class="thumbnail-slider slider-mobile-gutter quick-add-hidden{% unless section.settings.gallery_layout contains 'thumbnail' %} medium-hide large-up-hide{% endunless %}{% if section.settings.mobile_thumbnails != 'show' %} small-hide{% endif %}{% if media_count <= 3 %} thumbnail-slider--no-slide{% endif %}"
    >
      <button
        type="button"
        class="slider-button slider-button--prev{% if media_count <= 3 %} small-hide{% endif %}{% if media_count <= 4 %} medium-hide large-up-hide{% endif %}"
        name="previous"
        aria-label="{{ 'general.slider.previous_slide' | t }}"
        aria-controls="GalleryThumbnails-{{ section.id }}"
        data-step="3"
      >
        <span class="svg-wrapper">
          {{- 'icon-caret.svg' | inline_asset_content -}}
        </span>
      </button>
      <ul
        id="Slider-Thumbnails-{{ section.id }}"
        class="thumbnail-list list-unstyled slider slider--mobile{% if section.settings.gallery_layout == 'thumbnail_slider' %} slider--tablet-up{% endif %}"
      >
        {%- capture sizes -%}
          (min-width: {{ settings.page_width }}px) calc(({{ settings.page_width | minus: 100 | times: media_width | round }} - 4rem) / 4),
          (min-width: 990px) calc(({{ media_width | times: 100 }}vw - 4rem) / 4),
          (min-width: 750px) calc((100vw - 15rem) / 8),
          calc((100vw - 8rem) / 3)
        {%- endcapture -%}

        {%- if featured_media != null -%}
          {%- liquid
            capture media_index
              if featured_media.media_type == 'model'
                increment model_index
              elsif featured_media.media_type == 'video' or featured_media.media_type == 'external_video'
                increment video_index
              elsif featured_media.media_type == 'image'
                increment image_index
              endif
            endcapture
            assign media_index = media_index | plus: 1
          -%}
          <li
            id="Slide-Thumbnails-{{ section.id }}-0"
            class="thumbnail-list__item slider__slide{% if section.settings.hide_variants and variant_images contains featured_media.src %} thumbnail-list_item--variant{% endif %}"
            data-target="{{ section.id }}-{{ featured_media.id }}"
            data-media-position="{{ media_index }}"
          >
            {%- capture thumbnail_id -%}
              Thumbnail-{{ section.id }}-0
            {%- endcapture -%}
            <button
              class="thumbnail global-media-settings global-media-settings--no-shadow"
              aria-label="{%- if featured_media.media_type == 'image' -%}{{ 'products.product.media.load_image' | t: index: media_index }}{%- elsif featured_media.media_type == 'model' -%}{{ 'products.product.media.load_model' | t: index: media_index }}{%- elsif featured_media.media_type == 'video' or featured_media.media_type == 'external_video' -%}{{ 'products.product.media.load_video' | t: index: media_index }}{%- endif -%}"
              aria-current="true"
              aria-controls="GalleryViewer-{{ section.id }}"
              aria-describedby="{{ thumbnail_id }}"
            >
              {{
                featured_media.preview_image
                | image_url: width: 416
                | image_tag:
                  loading: 'lazy',
                  sizes: sizes,
                  widths: '54, 74, 104, 162, 208, 324, 416',
                  id: thumbnail_id,
                  alt: featured_media.alt
                | escape
              }}
            </button>
          </li>
        {%- endif -%}
        {%- for media in product.media -%}
          {%- unless media.id == featured_media.id -%}
            {%- liquid
              capture media_index
                if media.media_type == 'model'
                  increment model_index
                elsif media.media_type == 'video' or media.media_type == 'external_video'
                  increment video_index
                elsif media.media_type == 'image'
                  increment image_index
                endif
              endcapture
              assign media_index = media_index | plus: 1
            -%}
            <li
              id="Slide-Thumbnails-{{ section.id }}-{{ forloop.index }}"
              class="thumbnail-list__item slider__slide{% if section.settings.hide_variants and variant_images contains media.src %} thumbnail-list_item--variant{% endif %}"
              data-target="{{ section.id }}-{{ media.id }}"
              data-media-position="{{ media_index }}"
            >
              {%- if media.media_type == 'model' -%}
                <span class="thumbnail__badge" aria-hidden="true">
                  <span class="svg-wrapper">
                    {{- 'icon-3d-model.svg' | inline_asset_content -}}
                  </span>
                </span>
              {%- elsif media.media_type == 'video' or media.media_type == 'external_video' -%}
                <span class="thumbnail__badge" aria-hidden="true">
                  <span class="svg-wrapper">
                    {{- 'icon-play.svg' | inline_asset_content -}}
                  </span>
                </span>
              {%- endif -%}
              {%- capture thumbnail_id -%}
                Thumbnail-{{ section.id }}-{{ forloop.index }}
              {%- endcapture -%}
              <button
                class="thumbnail global-media-settings global-media-settings--no-shadow"
                aria-label="{%- if media.media_type == 'image' -%}{{ 'products.product.media.load_image' | t: index: media_index }}{%- elsif media.media_type == 'model' -%}{{ 'products.product.media.load_model' | t: index: media_index }}{%- elsif media.media_type == 'video' or media.media_type == 'external_video' -%}{{ 'products.product.media.load_video' | t: index: media_index }}{%- endif -%}"
                {% if media == featured_media
                  or featured_media == null
                  and forloop.index == 1
                %}
                  aria-current="true"
                {% endif %}
                aria-controls="GalleryViewer-{{ section.id }}"
                aria-describedby="{{ thumbnail_id }}"
              >
                {{
                  media.preview_image
                  | image_url: width: 416
                  | image_tag:
                    loading: 'lazy',
                    sizes: sizes,
                    widths: '54, 74, 104, 162, 208, 324, 416',
                    id: thumbnail_id,
                    alt: media.alt
                  | escape
                }}
              </button>
            </li>
          {%- endunless -%}
        {%- endfor -%}
      </ul>
      <button
        type="button"
        class="slider-button slider-button--next{% if media_count <= 3 %} small-hide{% endif %}{% if media_count <= 4 %} medium-hide large-up-hide{% endif %}"
        name="next"
        aria-label="{{ 'general.slider.next_slide' | t }}"
        aria-controls="GalleryThumbnails-{{ section.id }}"
        data-step="3"
      >
        <span class="svg-wrapper">
          {{- 'icon-caret.svg' | inline_asset_content -}}
        </span>
      </button>
    </slider-component>
  {%- endif -%}
</media-gallery>

If I helped you, then a Like would be truly appreciated.

1 Like

@namphan Thank you so much for your suggested code changes! This worked perfectly and solved my problem. I really appreciate your contribution. It’s wonderful to have such supportive people around! If anyone else is facing similar issues with undefined variables, don’t hesitate to reach out. Your help makes a huge difference!

Hi @KDee4411,

It’s my pleasure