Skip to content

Instantly share code, notes, and snippets.

@michelarteta
Last active April 29, 2021 21:20
Show Gist options
  • Save michelarteta/bcf9d190044c6164b693b4383fe56c77 to your computer and use it in GitHub Desktop.
Save michelarteta/bcf9d190044c6164b693b4383fe56c77 to your computer and use it in GitHub Desktop.
Shopify Mutation Observer
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