Jump to content

How to remove trailing decimal .00's from product pricing

Recommended Posts

Improve your online store with our extensions.
About: Squarespace Circle Leader since 2017. I value honesty, transparency, appreciation and great design ♥.
Work: Squarespace Developer and founder of SF Digital, building the features Squarespace didn't include™.
Content: Links in my posts may refer to SF Digital products or may be affiliate links.

Buy me a coffee

Link to comment

Been there too @Brandon!

The Community team have now imported the old Answers post 👍

 

Improve your online store with our extensions.
About: Squarespace Circle Leader since 2017. I value honesty, transparency, appreciation and great design ♥.
Work: Squarespace Developer and founder of SF Digital, building the features Squarespace didn't include™.
Content: Links in my posts may refer to SF Digital products or may be affiliate links.

Buy me a coffee

Link to comment
  • 4 weeks later...

Thanks @brandon and @paul2009 for this code! It works perfectly when I first save it in the foot code injection, but as soon as I refresh a page, it reverts to the normal pricing with decimals again. I wonder if it's because our prices are in £ rather than $. I've tried swapping the currencies out in the code but I really don't know what I'm doing! Any ideas how I could get it to work?

Here's an example of what I'm working with: https://ellishampers.co.uk/hampers/magnificent-n8-the-marvel

 

UPDATE:

Just checked that the above linked worked and the decimals had disappeared! Tried in incognito and they came back again. It seems that if I refresh the page the code works, but the first time I load it the decimals are always there. In normal browsing (not incognito), the same applies but every time you load the page, you also see the decimals briefly before they disappear. Worth putting the code in the header for this or is that something that could go wrong?

Thank you! 😊

Edited by EllisCo
Link to comment

The code was written in 2016 and like many of the older code samples provided on forums, it therefore understandably doesn’t take into account the introduction of Ajax on many Squarespace templates since that time. Turning off Ajax in Site Styles will fix this but it’s just a workaround. A better solution is to replace

(function() {

with

window.Squarespace.onInitialize(Y, function() {

If like me you’d like to understand why, I wrote an explanation here: https://sf.digital/squarespace-solutions/why-doesnt-my-code-work-until-i-refresh-the-page

Edited by paul2009

Improve your online store with our extensions.
About: Squarespace Circle Leader since 2017. I value honesty, transparency, appreciation and great design ♥.
Work: Squarespace Developer and founder of SF Digital, building the features Squarespace didn't include™.
Content: Links in my posts may refer to SF Digital products or may be affiliate links.

Buy me a coffee

Link to comment
  • 3 weeks later...

Hi, thank you very much for this solution, I've found this to work great for me as well apart from a single instance when I choose two variables of a product to add to cart - such as a size and a color. When two variables are chosen, suddenly the two decimals appear again. Any idea why that might be and if there's a way to solve this?

Link to comment
  • 3 months later...
  • 11 months later...
  • 5 months later...

Hi - I am wondering if it is possible to switch between variants?

I want anything with trailing double decimals to display without them - for example $250.00 should display as $250. But I still want to keep my trailing decimals on my little numbers - for example I want $1.50 to display as that - not $1.5

Is that possible or an impossibility?

 

I have used the footer injection here - can it be edited to support this?

<script>

window.Squarespace.onInitialize(Y, function() {

       var prices = document.getElementsByClassName('sqs-money-native');

   var thisPrice;

   var i, I;

   for (i=0, I=prices.length; i<I; i++) {

       thisPrice = prices[i];

       thisPrice.innerHTML = thisPrice.innerHTML.replace(/(\.[0-9]*?)0+$/, "$1").replace(/\.$/, ""); //http://stackoverflow.com/questions/1015402/1015434#1015434

   }

   })();

Link to comment

@misimo

Please post the URL for a page on your site where we can see your issue.

If your site is not public please set up a site-wide password, if you've not already done so.

Post the password here.

Adding a site-wide password is not a security breach. Please read the documentation at the link provided to understand how it works.

Please read the documentation at the link provided on how to share a link to your site to understand how it works. A link to the backend of the your site won’t work for us, i.e. a url that contains /config/.

We can then take a look at your issue.

Find my contributions useful? Please like, upvote, mark my answer as best , and see my profile. Thanks for your support!

Link to comment
  • 3 months later...

I'm also trying to remove decimals, I've tried a code that was linked here (see below) which has worked mostly, however it turns 11.90 into 11.9 and so on. I don't want to delete all the zeros. Can anyone help?

<script>
   (function() {
       var prices = document.getElementsByClassName('sqs-money-native');
   var thisPrice;
   var i, I;
   for (i=0, I=prices.length; i<I; i++) {
       thisPrice = prices[i];
       thisPrice.innerHTML = thisPrice.innerHTML.replace(/(\.[0-9]*?)0+$/, "$1").replace(/\.$/, ""); //http://stackoverflow.com/questions/1015402/1015434#1015434
   }
   })();
</script>

Link to comment
  • 7 months later...
On 4/21/2021 at 3:43 AM, irenerodriguezca said:

For 7.1 version I found https://www.ghostplugins.com/steps/reformat-product-price-71-version

I just gave this a go, and it deleted everything after the decimals. Would you know how to modify it to only delete decimal places if they are all 0s? there are some prices on my site that are priced like 3.50 or 4.50 etc, and i would like to keep the decimal places for those.

 

Link to comment
10 hours ago, NinaNice said:

Anyone any idea on why this is?

It appears the code wasn't designed to deal with french formatted numbers. The original number format is 1 000,00 €. The code first removes the comma then removes .00 but as the original number didn't have a .00, nothing happens.

I have pointed to my Store Price Change in this thread in several posts. It should be able to handle your need. Although my code is not specifically language aware it does provide fairly flexible text changing capabilities.

The following search replace text settings should get you started.

        // search text and replacment text example line follows
        
        // '[enter search text here between single quotes]' : '[enter replacement text here between single quotes]',
        
        '/\\s*(.+)\\s*/' : '$1', // trim white space
        
        ',00 €' : ' €', // remove Deutsch and Français decimal
        
        '.00' : '',  // remove English and Nederlands decimal
        

Let us know how it goes.

Find my contributions useful? Please like, upvote, mark my answer as best , and see my profile. Thanks for your support!

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.