Unable to assign second varient image as default

Highlighted
Shopify Partner
8 0 0

I am trying to assign the second image of a variant as the first image on the collection listings page with not luck. Here is the code i have tried.

 

{%- assign featured_image = current_variant.image | default: product.featured_image -%}

 

{%- if settings.product_img_fx > '0' -%}
<img class="mpt-image swap-image{{_onepone}}src="{{product.images[1] | img_url: _dim , format: 'pjpg'}}" alt="{{product.images[1].alt | escape}}" srcset="{{product.images[1] | img_url: _dim, scale: 2}} 500w, {{product.images[1] | img_url: _dim, format: 'pjpg'}} 166w" sizes="(max-width: 500px) 100vw, 500px"/>
{%- endif -%}

0 Likes
Shopify Expert
9965 84 1465

There's a couple of weird things in your code to start with.

 

1.

{%- if settings.product_img_fx > '0' -%}

You're doing a math comparison against a string. Expect weird results from that.

 

2.

<img class="mpt-image swap-image{{_onepone}}src="{{product.images[1]

(I've truncated the rest of the code). You're not closing the class property and go straight into the src property.
Would have expected more like this:

<img class="mpt-image swap-image{{_onepone}}" src="{{product.images[1]

Once you've sorted those post back with any results= or new info (or just if you're now sure on what to do next).

 

As a side point, you're also not adding any checks to see if the product even has a second image. Always best to check that just in case otherwise you'll see missing images for those products with just the one.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Shopify Partner
8 0 0

Thank you Jason - i will try your suggestions.

0 Likes