Personalized checkout and custom promotions with Shopify Scripts
I am using following javascript to get a variant from variant_id. From the script below, I could get response as variant.product_variant in debug console. Problem is the varaint object does not have .image attribute or .image.src attribute and i want the image of the variant to display in .stick-bar-thumb-thumb container. Any help on how to get variant image via javascript or any modification to below code? Thaks
unction updateThumbnailImage(variantId) {
// Construct the URL to fetch variant information
var variantUrl = '/variants/' + variantId + '.json';
// Fetch the variant data using AJAX
fetch(variantUrl)
.then(function (response) {
return response.json();
})
.then(function (variant) {
if (variant && variant.image) {
// Get the URL of the variant's image
var imageUrl = variant.image.src;
// Select the element with the class .sticky-bar-thumb-thumb
var thumbImageElement = document.querySelector('.sticky-bar-thumb-thumb');
// Update the src attribute of the image element
thumbImageElement.src=imageUrl;
}
})
.catch(function (error) {
console.error('Error fetching variant data:', error);
});
By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024