How do I assign colour variant to product image for that specific colour on dawn theme 15.0 - code

@kpsclothing its done alhamdulillah. No multiple steps in this one. Just open your product-variant-options.liquid file and replace this code.

{% comment %}
Renders product variant options

Accepts:

  • product: {Object} product object.
  • option: {Object} current product_option object.
  • block: {Object} block object.
  • picker_type: {String} type of picker to dispay

Usage:
{% render ‘product-variant-options’,
product: product,
option: option,
block: block
picker_type: picker_type
%}
{% endcomment %}
{%- liquid
assign product_form_id = ‘product-form-’ | append: section.id
-%}

{%- for value in option.values -%}
{%- liquid
assign swatch_focal_point = null
if value.swatch.image
assign image_url = value.swatch.image | image_url: width: 50
assign swatch_value = ‘url(’ | append: image_url | append: ‘)’
assign swatch_focal_point = value.swatch.image.presentation.focal_point
elsif value.swatch.color
assign swatch_value = ‘rgb(’ | append: value.swatch.color.rgb | append: ‘)’
else
assign swatch_value = null
endif

assign option_disabled = true
if value.available
assign option_disabled = false
endif
-%}

{%- capture input_id -%}
{{ section.id }}-{{ option.position }}-{{ forloop.index0 -}}
{%- endcapture -%}

{%- capture input_name -%}
{{ option.name }}-{{ option.position }}
{%- endcapture -%}

{%- capture input_dataset -%}
data-product-url=“{{ value.product_url }}”
data-option-value-id=“{{ value.id }}”
{%- endcapture -%}

{%- capture label_unavailable -%}

{{- ‘products.product.variant_sold_out_or_unavailable’ | t -}}

{%- endcapture -%}

{%- if picker_type == ‘swatch’ -%}
{%- capture help_text -%}
{{ value | escape }}
{{ label_unavailable }}
{%- endcapture -%}
{%
render ‘swatch-input’,
id: input_id,
name: input_name,
value: value | escape,
swatch: value.swatch,
product_form_id: product_form_id,
checked: value.selected,
visually_disabled: option_disabled,
shape: block.settings.swatch_shape,
help_text: help_text,
additional_props: input_dataset
%}
{%- elsif picker_type == ‘button’ -%}
<input
type=“radio”
id=“{{ input_id }}”
name=“{{ input_name }}”
value=“{{ value | escape }}”
form=“{{ product_form_id }}”
{% if value.selected %}
checked
{% endif %}
{% if option_disabled %}
class=“disabled”
{% endif %}
{{ input_dataset }}

{% if product.variants[forloop.index0].featured_image != blank and option.name == ‘Color’ %}

{{ label_unavailable }}

{% else %}

{{ value -}}
{{ label_unavailable }}

{% endif %}
{%- elsif picker_type == ‘dropdown’ or picker_type == ‘swatch_dropdown’ -%}

{% if option_disabled -%} {{- 'products.product.value_unavailable' | t: option_value: value -}} {%- else -%} {{- value -}} {%- endif %} {%- endif -%} {%- endfor -%}

This should work pretty fine. Let me know how this goes. If it does please dont forget to like and mark it as solution.

You can email me for further customizations if required.

Email Me

Buy me Coffee if you feel i was able to help and i deserve it.

Thanks anyways

The code is not working on Trade theme and on Dawn theme.

Refresh is not happening on mobile view.

Refresh is not working when we select different colour or variant.

@kpsclothing I tried it on the trade theme and it did work. Will check on this

@kpsclothing I tried on the trade theme i believe it will also work on the dawn theme as well.

This is my development store url: https://4vu8miskjypbr79k-57171247244.shopifypreview.com/
Password is: reafur

Things are updating quite good, if you feel product images are not looking good well that is because i just added random photos which are not of same width and height. Other than functionality seems ok to me.

Check this out and let me know what am i missing here. Remember to check this product in my store in the image below.

Let me know. Thanks

this variant product picture looks good. kindly share the code so we can check it out.

@kpsclothing I did share the code and I am using the same obviously. Works fine here.

Did you check the functionality? I mean the images are changing on selection of variant both on desktop and mobile as well.

My store is using the trade theme now.

All functionality is working fine.

@kpsclothing try it on yours now and let me know if everything works well.

You can email me for further customizations if required or regarding any web developer projects in future

Email Me

Buy me Coffee if you feel i was able to help and i deserve it.

Thanks anyways

need to show only selected variant images on mobile view when swiped, and on desktop view, the primary image remains the same on all sections. also, show product image on variant swatches

@kpsclothing except for the last one of showing images in variant swatches does my development store has all the functionality you are asking for.

So on selection only the selected color products are shown even when sliding only the selected color products are shown.

Are you using thumbnail layout to show images in your desktop and mobile? If that’s so then the code is a bit different for that. This might be the reason you are facing some issues.

yes, we are using thumbnail layout on both desktop and mobile

Hi Shadab, Thanks for all the details in the thread. I am actually looking for this solution you have mentioned … to show only images of the variant color selected in Dawn 15.0. I have created variants with image swatches, however on slected any variant color at any time, all the imgaes of all colors are shown intstead of only the imgae of the selected variant. can yo please help with this. Here is the link of the product variant page i have created for you to better understand the issue. Thanks in advance

https://www.ikonsweden.com/products/cactus-leather-bifold-card-holder-black

@IKONSWEDEN please do look at my response to vegan_stallion. I have explained the steps to achieve what you are looking for. It includes three steps, so go around carefully.

If you don’t understand something or need any assistance I will be available and definitely provide you the steps again if required which ever way suits you best.

Thanks

@kpsclothing will update you with the solution on thumbnails part, I have it i believe already.

Will also provide you with that image for variant requirement of yours. I believe I have that as well.

Thanks

Hi, I have tried it with putting up alt text to all balck images with - BLACK, all Cognac images with - COGNAC and same with Navy blue but it shows only 1 image incase of black, 2 images in case of cognac and 3 images in case of blue.

Make sure that the Alt text you are using is exactly same as the name of your color variants, by that I mean capitalisation and everything must be same.

The best is to use lowercase for both color variants and the alt text for the corresponding images, this way we are less prone to mistakes.

Please check and let me know

I will try that, however I have one query. In the alt text instead of writing only ‘black’ can we write ‘black leather wallet’ in all the images related to black wallet. Will it work or will it create error. Thanks in advance

If you are writing black leather wallet for all black wallets just make sure you name your color variant also to black leather wallet, so the name of variant color and alt text should be exactly same.