Last active
April 29, 2021 21:20
-
-
Save michelarteta/bcf9d190044c6164b693b4383fe56c77 to your computer and use it in GitHub Desktop.
Shopify Mutation Observer
This file contains 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
function checkoutUpdates() { | |
const prices = document.querySelectorAll('.product__price'); | |
prices.forEach((item) => { | |
const productPrice = item; | |
const productName = item.closest('tr').querySelector('.product__description__name'); | |
productName.appendChild(productPrice); | |
}); | |
} | |
let mutationIsHappened = false | |
document.addEventListener("DOMContentLoaded", function() { | |
checkoutUpdates() | |
const tagsContainerEl = document.querySelector('.sidebar__content .order-summary__sections'); | |
new MutationObserver((mutations) => { | |
mutations.forEach(({type, target}) => { | |
if (target.classList.contains('order-summary__sections')) { | |
if (mutationIsHappened === false) { | |
mutationIsHappened = true; | |
checkoutUpdates(); | |
} else { | |
mutationIsHappened = false; | |
} | |
} | |
}); | |
}).observe(tagsContainerEl, { childList: true, subtree: true }) | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment