In WordPress and Woocommerce, I am attempting to apply javascript within the child theme functions.php, so if a user clicks a specific product attribute color swatch, it will change a preview image (shown below the swatch selectors) with a specific ID to a new image source URL. I have very limited javascript knowledge and am fumbling my way through this.
I was able to get one single swatch (Black) to change the target image, but I cannot figure out how to repeat the command for the other swatches. Here is what I have so far:
add_action( 'woocommerce_before_single_product', 'bluetera_swatch_onclick' );
function bluetera_swatch_onclick() { ?>
<script type='text/javascript'>
var input = document.getElementById("ecorigidswatches_Black");
input.onclick = function swapproductimg() {
document.getElementById('selected-swatch').src="https://www.laserfelt.com/wp-content/uploads/2021/06/BlackHexagon.jpg";
}
</script>
<?php
}
Here is a link to the product page in question:
https://www.laserfelt.com/product/hexagon-acoustic-wall-tiles/
I was also trying to create a conditional statement that would only execute this function if the user was viewing a product in a specific category. But I would not figure out how to do that either. All of my attempts failed. We have different sets of color swatches and I am trying to figure out a way to have the script load only when it is needed.
I am also wondering if this would be the most efficient way to achieve the desired result, or if there is a better way that would improve page load speeds? I am limited to working with the existing plugin that creates these product swatch options. That is why I am having to insert the onclick function in a roundabout way.
We do not want to use any of the pre-existing swatch plugins that only changed the main product image.
I would really appreciate some pointers, tips and direction. Thanks!
— UPDATE —
Based on a tweak of Michal’s recommendation, I also tried this, but it did not work:
var black = document.getElementById("ecorigidswatches_Black");
var berry = document.getElementById("ecorigidswatches_Berry");
if (berry.checked = true) {
document.getElementById('selected-swatch').src="https://www.laserfelt.com/wp-content/uploads/2021/06/BerryHexagon.jpg";
} elseif (black.checked = true) {
document.getElementById('selected-swatch').src="https://www.laserfelt.com/wp-content/uploads/2021/06/BlackHexagon.jpg";
}
— UPDATE – PARTIAL SOLUTION —
With the help of Vladimir, I was able to get this to work, but if I click on any other product attribute or input, it changes the target img src to "undefined" and the image becomes broken. Here was my working code that is in functions.php file:
add_action( 'woocommerce_before_single_product', 'bluetera_swatch_onclick' );
function bluetera_swatch_onclick() { ?>
<script type='text/javascript'>
const images = {
'ecorigidswatches_Berry':"https://www.laserfelt.com/wp-content/uploads/2021/06/BerryHexagon.jpg",
'ecorigidswatches_Black':"https://www.laserfelt.com/wp-content/uploads/2021/06/BlackHexagon.jpg",
'ecorigidswatches_Brown':"https://www.laserfelt.com/wp-content/uploads/2021/06/BrownHexagon.jpg"
};
//select all elements with data-color attribute
const swatch = document.querySelectorAll('[id]');
swatch.forEach(element => {
element.addEventListener('click', () => {
let color = element.getAttribute('id');
document.getElementById("selected-swatch").src = images[color];
});
});
</script>
<?php
}
2
Answers
To loop the image source using an array with JavaScript:
Edit:
Creates an object images with properties red, blue and green. It selects all elements with a
data-color
attribute and attaches a click event listener. On click, it retrieves color value from the data-color attribute and updates the image source.Extra: