Remove trailing .00's from SellUp

Some stores display rounded pricing.

With the following script added to a liquid template page, e.g main-product.liquid or theme.liquid, this script will run and remove any trailing 00's from whole pricing e.g $5.00 will become $5.

Always make a backup of your theme before making changes.


<script>
document.addEventListener("DOMContentLoaded", function() {
    // Function to remove .00 from .chprice and .price elements
    function updatePrices() {
        // Update .chprice elements
        var chPrices = document.querySelectorAll('.chprice');
        chPrices.forEach(function(price) {
            var text = price.textContent;
            if (text.endsWith('.00')) {
                price.textContent = text.slice(0, -3);
            }
        });
        // Update .price elements under .detail_price
        var detailPrices = document.querySelectorAll('.detail_price .price');
        detailPrices.forEach(function(price) {
            var text = price.textContent;
            if (text.endsWith('.00')) {
                price.textContent = text.slice(0, -3);
            }
        });
    }
    // Create a MutationObserver to detect changes in the DOM
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.addedNodes.length) {
                updatePrices();
            }
        });
    });
    // Start observing the document body for added nodes
    observer.observe(document.body, { childList: true, subtree: true });
    // Run the updatePrices function initially in case .chprice and .price elements are already in the DOM
    updatePrices();
});
</script>
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.