Jump to content

Can someone help me change coding to get rid of decimal places in my pricing?

Recommended Posts

Site URL: https://rocksforlife.com

Could anyone help me out removing two last zeroes from product price?
 
Example: 15,000.00 to 15,000 (so still keeping the comma in between)
 
The code I used (and which is not achieving my desired result):
 
<script>!function(){function reformatPrice(price){var priceClone=price.cloneNode(!0);function reformat(){priceClone.innerHTML=price.innerHTML;var target=priceClone.querySelector(".sqs-money-native")||priceClone,textNode=priceClone.querySelector(".sqs-money-native");target.textContent.length>1?target.textContent=format(target.textContent):textNode&&(textNode.textContent=format(textNode.textContent))}function format(text){return text.replace(",","").replace(/\.\d{2}/,"")}function watch(){var observer;new MutationObserver(reformat).observe(price,{childList:!0})}priceClone.classList.add("product-price-clone"),price.parentNode.insertBefore(priceClone,price.nextElementSibling),price.style.display="none",reformat(),watch()}ready(".product-price:not(.product-price-clone),.original-price:not(.product-price-clone)",reformatPrice)}();</script>
Link to comment

Update: Please note that 'pricing' answers provided earlier than 23 August 2022 no longer function as intended. This is due to a Squarespace update on this date.

Edited by paul2009

About me: I've been a SQSP User for 18 yrs. I was invited to join the Circle when it launched in 2016. I have been a Circle Leader since 2017. I don't work for Squarespace. I value honesty, transparency, diversity and good design ♥.
Work: I founded and run SF.DIGITAL, building Squarespace Extensions to supercharge your commerce website. 
Content: Views and opinions are my own. Links in my posts may refer to SF.DIGITAL products or may be affiliate links.
Forum advice is free. You can thank me by clicking one of the feedback emojis below. Coffee is optional.

Link to comment
  • 2 years later...
  • 4 months later...

Hi,
I've spent ages figuring this out for my site. So thought that you'd like the code too.

1. To remove the decimals from pricing in 7.1. 
e.g. so that £100.00 becomes £100 add the below script to your footer using code injection:

<script>
document.addEventListener("DOMContentLoaded", function() {
  // Selecting the price elements
  var priceAmounts = document.querySelectorAll('.pricing-plan-price-amount');
  priceAmounts.forEach(function(price) {
    var text = price.textContent;
    var updatedText = text.split('.')[0]; // This removes the decimal part
    price.textContent = updatedText;
  });

  // Keeping the billing period text intact
  var billingPeriods = document.querySelectorAll('.pricing-plan-price-billing-period');
  billingPeriods.forEach(function(period) {
    // We don't need to change this, but we include it to avoid accidentally altering it
    var text = period.textContent;
    period.textContent = text;
  });
});
</script>


2. To remove the comma from pricing.
E.g. so that £1,000 becomes £1000 add the below script to your footer using code injection:

<script>
document.addEventListener("DOMContentLoaded", function() {
  // Selecting the price elements
  var priceAmounts = document.querySelectorAll('.pricing-plan-price-amount');
  priceAmounts.forEach(function(price) {
    var text = price.textContent;
    var updatedText = text.split('.')[0]; // Removes the decimal part
    updatedText = updatedText.replace(/,/g, ''); // Removes commas
    price.textContent = updatedText;
  });

  // Keeping the billing period text intact
  var billingPeriods = document.querySelectorAll('.pricing-plan-price-billing-period');
  billingPeriods.forEach(function(period) {
    // No change needed here
    var text = period.textContent;
    period.textContent = text;
  });
});
</script>

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.