Created
June 13, 2025 11:22
-
-
Save saadsaif97/8d32397a7e02ead925d254387685d266 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
Author
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
you need to add the variant image url in input by adding this property in
swatch-input.liquid