Skip to content

Instantly share code, notes, and snippets.

@atikju
Last active September 9, 2022 09:48
Show Gist options
  • Select an option

  • Save atikju/a589629e144de71ce2816457da233e52 to your computer and use it in GitHub Desktop.

Select an option

Save atikju/a589629e144de71ce2816457da233e52 to your computer and use it in GitHub Desktop.
Shopify - change main image on bold product option swatch click
$(document).ready(function(){
var boldFinder = setInterval(function(){
if($('.bold_option_swatch').length > 0){
$('.bold_option_swatch').eq(0).find('.bold_option_value').on('click', function(){
var clutch_selected = $(this).find('input.sw_607037_276033').attr('data-option_value_key');
console.log(clutch_selected);
//returns url("....");
var imgUrl = $(this).find('span.bold_option_value_swatch span').css('background-image');
//replace the url("
//console.log(imgUrl.toString().slice(5));
var prosUrl = imgUrl.toString().slice(5);
//delete the last two charsS
var finalUrl = prosUrl.slice(0, -2);
console.log(finalUrl);
//var s = 'cat1234';
$('.product-main-image img').attr('src', finalUrl);
//var boo = $('.bold_swatch_selected span.bold_option_value_swatch span').css('background-image');
//alert(boo.slice(0, -2));
});
}
}, 1);
});
@DavidLHendrickson

Copy link
Copy Markdown

I have tried multiple times getting this to work.

I am adding it into the theme.liquid right before the{{ content_for_header }}
I wrapped it in <script></script>
And I believe I isolated what the gallery class is but I might be wrong there so I tried several classes that might be right and had no luck.

This is what I came up
<script> $('.bold_option_swatch').eq(0).find('.bold_option_value').on('click', function(){ var clutch_selected = $(this).find('input.sw_607037_276033').attr('data-option_value_key'); console.log(clutch_selected); //returns url("...."); var imgUrl = $(this).find('span.bold_option_value_swatch span').css('background-image'); //replace the url(" //console.log(imgUrl.toString().slice(5)); var prosUrl = imgUrl.toString().slice(5); //delete the last two charsS var finalUrl = prosUrl.slice(0, -2); console.log(finalUrl); //var s = 'cat1234'; $('.product-main-image img').attr('src', finalUrl); //var boo = $('.bold_swatch_selected span.bold_option_value_swatch span').css('background-image'); //alert(boo.slice(0, -2)); });</script>

My website I am attempting to make this change to is https://david-likes-to-draw.myshopify.com/products/framed-premium-canvas-pet-portrait

I create custom pet portraits and offer 3 different styles of art that are represented in the bold swatches. Any guidance would be appreciated.

Screen Shot 2021-04-23 at 10 12 01 AM

@atikju

atikju commented Apr 23, 2021

Copy link
Copy Markdown
Author

Okay David, you are doing it a little bit wrong. See the code updated.

And as I can see on your attached image, on line 84, you are missing DOM identifier. product-image-main-- I assume this is an ID. if yes, put it as #product-image-main-- or if it's a class .product-image-main--

Email me directly for any other things: ratiqur807@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment