Skip to content

Instantly share code, notes, and snippets.

@saadsaif97
Created June 13, 2025 11:22
Show Gist options
  • Select an option

  • Save saadsaif97/8d32397a7e02ead925d254387685d266 to your computer and use it in GitHub Desktop.

Select an option

Save saadsaif97/8d32397a7e02ead925d254387685d266 to your computer and use it in GitHub Desktop.
<script>
function updatePDPSwatches() {
document.querySelectorAll("input[type='radio']").forEach(input => {
let imageUrl = input.dataset.variantImage;
let label = input.nextElementSibling;
let swatch = label?.querySelector(".swatch");
if (imageUrl && swatch && label) {
swatch.style.backgroundImage = `url('${imageUrl}')`;
swatch.style.backgroundSize = "cover";
swatch.style.backgroundPosition = "center";
swatch.style.width = "100px";
swatch.style.aspectRatio = "2";
swatch.style.setProperty("--swatch-input--border-radius", "0");
label.style.setProperty("--swatch-input--border-radius", "0");
}
})
}
document.addEventListener("DOMContentLoaded", () => {
updatePDPSwatches()
})
document.addEventListener("PDP:updated", () => {
updatePDPSwatches()
})
</script>
@saadsaif97

Copy link
Copy Markdown
Author

you need to add the variant image url in input by adding this property in swatch-input.liquid

data-variant-image="{{ value.variant.featured_image | image_url: width: 200 }}"

@saadsaif97

Copy link
Copy Markdown
Author

then you also need to dispatch the event in renderProductInfo function in product-info.js
like this:

document.dispatchEvent(new CustomEvent("PDP:updated"))

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