Post by guisauer » Thu Sep 29, 2011 1:01 am

If the product price is $25.00, I want Opencart to AUTOMATICALLY display it without the decimals ($25)
If the product price is not round, $25.30, I want Opencart to display it with two decimals ($25.30)

I'm using 1.4.9.1, and I have Opencart set to 2 decimals in the Admin section.

Does anyone have an idea on how to do that?
Thank you so much!

New member

Posts

Joined
Sun Dec 06, 2009 7:51 am

Post by uksitebuilder » Thu Sep 29, 2011 1:56 am

Where ever the price is displayed in your templates you would have to wrap the price variable in a php function

example

<?php echo $price; ?>

would become

<?php echo rtrim($price, '.00'); ?>

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by guisauer » Thu Sep 29, 2011 2:38 am

How would you apply it to this?
<?php echo $products[$j]['price']; ?>

Would it be:
<?php echo rtrim($products[$j]['price'], '.00'); ?>

Also, isn't there an easier way to do this, so that it's automated, other than having to replace every single string of code that shows $price manually?

New member

Posts

Joined
Sun Dec 06, 2009 7:51 am

Post by uksitebuilder » Thu Sep 29, 2011 4:37 am

You could probably do it at the controller level because that is where the currency/tax gets calculated.

catalog/model/catalog/product.php
catalog/model/catalog/category.php

etc

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by guisauer » Thu Sep 29, 2011 5:15 am

Ok, I'll give that a try! Thanks!

New member

Posts

Joined
Sun Dec 06, 2009 7:51 am

Post by guisauer » Thu Sep 29, 2011 5:15 am

But just in case, is this correct? <?php echo rtrim($products[$j]['price'], '.00'); ?>

New member

Posts

Joined
Sun Dec 06, 2009 7:51 am

Post by uksitebuilder » Thu Sep 29, 2011 5:22 am

Yes

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by Qphoria » Thu Sep 29, 2011 5:28 am

uksitebuilder wrote:You could probably do it at the controller level because that is where the currency/tax gets calculated.

catalog/model/catalog/product.php
catalog/model/catalog/category.php

etc
I'll do you one better and have you do it at the currency library level as then you only have to do it in once place and it will affect all prices dynamically

EDIT: system/library/currency.php

Change:

Code: Select all

return $string;
to

Code: Select all

return rtrim($string, '.00');

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by guisauer » Thu Sep 29, 2011 5:37 am

awesome, I'll try it out. Thank you both very much!

New member

Posts

Joined
Sun Dec 06, 2009 7:51 am

Post by uksitebuilder » Thu Sep 29, 2011 5:37 am

Bows to the maestro

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by guisauer » Thu Sep 29, 2011 2:43 pm

Actually, instead of rtrim, I had to use str_replace:

return str_replace('.00','',$string);

apparently rtrim searches character by character, instead of a full string. So if a price was $170.00, rtrim would replace ALL zeros, including the one in the middle, resulting in this: $17

Thank you again!

New member

Posts

Joined
Sun Dec 06, 2009 7:51 am

Post by uksitebuilder » Thu Sep 29, 2011 3:31 pm

Makes a mental not to be careful with rtrim() and only use for single character trimming

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by PetracheNicolae » Tue Nov 15, 2011 6:48 am

Qphoria wrote:
uksitebuilder wrote:You could probably do it at the controller level because that is where the currency/tax gets calculated.

catalog/model/catalog/product.php
catalog/model/catalog/category.php

etc
I'll do you one better and have you do it at the currency library level as then you only have to do it in once place and it will affect all prices dynamically

EDIT: system/library/currency.php

Change:

Code: Select all

return $string;
to
return rtrim($string, '.00');
can you tell me please how can i make oc calculate prices with just 2 decimals instead of 4? thanks

New member

Posts

Joined
Tue May 24, 2011 5:40 am
Who is online

Users browsing this forum: No registered users and 78 guests