Jump to content

[solved] Don´t display product price decimals if decimals is 0.00 ?


seog

Recommended Posts

Hi

 

I would like know how display in prestashop 1.5 product price without decimals if the decimals are .00

I tried with the next options but it don´t work

 

convertPrice price=$productPrice|string_format:"%.0f"|replace:".00":""}

convertPrice price=$productPrice|remove:".00"}

{convertPrice|string_format:"%d" price=$product.price}

 

best regards

Link to comment
Share on other sites

It is an interesting question, because you made me dig into Smarty and the source code...

And lot of searches and some experimentations, I think the syntax you look for is:

{convertPrice|replace:'.00':'' price=$productPrice}

convertPrice returns a price converted to a string according to the current currency and decimal symbol. Beware if it returns a comma as decimal separator or more than two digits...

A potentially robuster version can be:

{convertPrice|regex_replace:'/[.,]0+$/':'' price=$productPrice}

Edited by PhiLho (see edit history)
  • Like 1
Link to comment
Share on other sites

Thank for your contribution but this don´t work. I tried delete only decimals in product-list and product.tpl but only works in digits less than thousand with the next code

 

{convertPrice|string_format:"%d" price=$product.price}

 

If the price is 1000 or higher then the price displays only one digit

example: if price is 1256 with this modification only displays " 1 "

 

Do you know why?

Link to comment
Share on other sites

I don't understand. Do you mean you tried my modifiers and they failed? Honestly, I just tried them on a small Smarty test code, not in PrestaShop yet. I will try on my test shop.

 

convertPrice returns a string. I don't know a decimal string format works in PHP when given a string instead of a number. If it does an automatic conversion or if it errors.

Edited by PhiLho (see edit history)
Link to comment
Share on other sites

My |replace modifier failed on my shop, because it has comma as separator, not dot, as expected...

I adjusted my regex modifier, because the currency symbol was part of the price: with |regex_replace:'/[.,]0+ /':' ' I was successful to replace 22,00 € with 22 €.

Some adjustment might be needed in your case.

Link to comment
Share on other sites

Hi PhiLho,

 

My shop have in backoffice a dot but in frontoffice displays comma. I tried replacing product-list with the your new code but the decimals don´t disappear

{convertPrice|regex_replace:'/[.,]0+/':'' price=$product.price}

 

Do you modified any frontcontroller file ?

 

best regards

Edited by seog (see edit history)
Link to comment
Share on other sites

  • 1 year later...

Thank-you! 

 

Just one of the options that should be in back-office I think.

 

I just wanted to add that I removed the comma otherwise it was dropping any zero's after it.

 

{convertPrice|regex_replace:'/[.]0+/':'' price=$product.price}

 

Edit:  For australian currency - if needed to know.

Edited by death667b (see edit history)
Link to comment
Share on other sites

  • 3 months later...
  • 1 year later...

It is an interesting question, because you made me dig into Smarty and the source code...

And lot of searches and some experimentations, I think the syntax you look for is:

{convertPrice|replace:'.00':'' price=$productPrice}
convertPrice returns a price converted to a string according to the current currency and decimal symbol. Beware if it returns a comma as decimal separator or more than two digits...

A potentially robuster version can be:

{convertPrice|regex_replace:'/[.,]0+$/':'' price=$productPrice}

 

This works great, but it fails when the product has combinations....any ideas?

Link to comment
Share on other sites

  • 3 years later...

Prestashop 1.7

This solution is global. In my case I needed it that way. WARNING:  If you setup prestashop with more than 2 decimals, this would mess up your numbers! This solution is only for 2 decimals configuration (default).

/classes/Tools.php, line 719

Replace:

return $cldr->getPrice($price, is_array($currency) ? $currency['iso_code'] : $currency->iso_code);

With: 

$price = $cldr->getPrice($price, is_array($currency) ? $currency['iso_code'] : $currency->iso_code);
return str_replace(".00", "", $price);
Link to comment
Share on other sites

Create an account or sign in to comment

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

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...