Remove trailing .00's from SellUp

Modified on Thu, 18 Jul at 8:37 AM

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>

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article