-
Notifications
You must be signed in to change notification settings - Fork 0
/
variant-image-changer
57 lines (49 loc) · 2.96 KB
/
variant-image-changer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
(function() {
var handleSelection = function() {
// refers to the select dropdown and detects a change in selection
// replace the 'Colour' with whatever variant type you have.
// IT IS CASE SENSITIVE!
$("select[data-variant-option-name='Colour']").on('change', function() {
var name = this.value // Gets the name of the selected variant
if (name !== '') {
// If not empty (so if something is selected) do something
// IMPORTANT: replace ProductItem-gallery-thumbnails-item-image with whatever
// class you found
// Reduce the opacity of all images
$('.ProductItem-gallery-thumbnails-item-image').css('opacity', "0.2");
// Set the opacity of the selected variant back to 1
$('.ProductItem-gallery-thumbnails-item-image[alt^="' + name + '"]').css('opacity', "1");
// Simulate a click on the first image corresponding the selected variant
// This will change the large image
$('.ProductItem-gallery-thumbnails-item-image[alt^="' + name + '"]').click();
} else {
// This triggers when nothing is selected in the variant selector
// Set the opacity of all thumbnails to 1
$('.ProductItem-gallery-thumbnails-item-image').css('opacity', "1");
}
});
$('.ProductItem-gallery-thumbnails-item-image').click(function() {
// Get the alt name of clicked thumbnail
var altname = $(this).attr('alt');
// Get current variant selection (change Colour to your variant nam
var currentselection = $("select[data-variant-option-name='Colour']").val();
// --------
// Check if they match
if (altname.indexOf(currentselection) != 0) {
// No match so enable all thumbnails again.
// Again you will need your thumbnail class name here
// If you don't want this, remove the block between the "--------"
$('.ProductItem-gallery-thumbnails-item-image').css('opacity', "1");
}
else {}
});
// -------
};
// Initialize the fn on site first-load.
document.addEventListener("DOMContentLoaded", handleSelection);
// Reinit. the fn on each new AJAX-loaded page.
window.addEventListener("mercury:load", handleSelection);
})();
</script>