Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Silly prices when currency not default


tomjmul

Recommended Posts

Hi all,

 

I have been playing around with osc for the past week or so and have a tip to get rid of strange prices which show up when the user switches to a currency which is not the default.

 

For example, I use the shop by price infobox and when the currency is set to default all the price ranges are nice rounded numbers like under ?10.00, ?10.00 to ?20.00 etc. But when the user changes the currency the price ranges change to stupid amounts like $18.27 to $38.12. In short it looked bad and was driving me crazy.

 

So here is a fix. I am not a php programmer, but I know loads of PHP gurus read this forum, so please, if you can expand or improve on this, do so.

 

1. Open /includes/classes/currencies.php and find this bit:

 

// class methods
   function format($number, $calculate_currency_value = true, $currency_type = '', $currency_value = '') {
     global $currency;

     if (empty($currency_type)) $currency_type = $currency;

     if ($calculate_currency_value == true) {
       $rate = (tep_not_null($currency_value)) ? $currency_value : $this->currencies[$currency_type]['value'];
       $format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(tep_round($number * $rate, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];

 

and replace with this:

 

// class methods
   function format($number, $calculate_currency_value = true, $currency_type = '', $currency_value = '', $round=1) {
     global $currency;
     if (empty($currency_type)) $currency_type = $currency;

     if ($calculate_currency_value == true) {
       $rate = (tep_not_null($currency_value)) ? $currency_value : $this->currencies[$currency_type]['value'];
 $calc_price = $number * $rate;  
 if ($round != 1) {
	 $round = 1 / $round;
	 if (round(($calc_price*$round), 0)/$round != 0) {
   $calc_price = round(($calc_price*$round), 0)/$round;
	 } else {
   $calc_price = 1/$round;
	 }
	 
 }
 $format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(tep_round($calc_price, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];

 

Ok so what we have done here is to add an extra parameter on the end of this function so that it can also take a rounding value and calculate a sensible price based on it.

 

This value can be any decimal - some examples:

0.25 --- 3.31 -> 3.25

0.50 --- 5.71 -> 5.50

0.75 --- 5.22 -> 5.00, 5.78 --> 5.75

 

It can also be any whole number and this is what will be useful in tidying price ranges of the shop by price contrib and other places too.

 

5 --- 3.73 -> 5.00 etc.

 

There is also logic in there to prevent prices of zero so for example 1.73 (5) should give zero, but instead gives 5

 

Have I missed anything?

 

Now here's how to implement the altered function in the shop by price module, in order to display sensible price ranges, although the same method can be used anywhere in osc where prices are displayed:

 

Open /includes/boxes/shop_by_price.php and find this code:

 

  $info_box_contents[] = array('text'  => '<a href="' . tep_href_link(FILENAME_SHOP_BY_PRICE, 'range=0', 'NONSSL') . '">' . TEXT_INFO_UNDER . $currencies->format($sbp_array[0]) . '</a><br>');
for ($i=1, $ii=count($sbp_array); $i < $ii; $i++) {
   $info_box_contents[] = array('text'  => '<a href="' . tep_href_link(FILENAME_SHOP_BY_PRICE, 'range=' . $i, 'NONSSL') . '">' . TEXT_INFO_FROM . $currencies->format($sbp_array[$i-1]) . TEXT_INFO_TO . $currencies->format($sbp_array[$i]) . '</a><br>');
}
 if (MODULE_SHOPBYPRICE_OVER == True) {
   $info_box_contents[] = array('text'  => '<a href="' . tep_href_link(FILENAME_SHOP_BY_PRICE, 'range=' . $i, 'NONSSL') . '">' . $currencies->format($sbp_array[$i-1]) . TEXT_INFO_ABOVE . '</a><br>');

 

and replace with this:

  $info_box_contents[] = array('text'  => '<a href="' . tep_href_link(FILENAME_SHOP_BY_PRICE, 'range=0', 'NONSSL') . '">' . TEXT_INFO_UNDER . $currencies->format($sbp_array[0],true, '', '' , 5) . '</a><br>');
for ($i=1, $ii=count($sbp_array); $i < $ii; $i++) {
   $info_box_contents[] = array('text'  => '<a href="' . tep_href_link(FILENAME_SHOP_BY_PRICE, 'range=' . $i, 'NONSSL') . '">' . TEXT_INFO_FROM . $currencies->format($sbp_array[$i-1],true, '', '' , 5) . TEXT_INFO_TO . $currencies->format($sbp_array[$i],true, '', '' , 5) . '</a><br>');
}
 if (MODULE_SHOPBYPRICE_OVER == True) {
   $info_box_contents[] = array('text'  => '<a href="' . tep_href_link(FILENAME_SHOP_BY_PRICE, 'range=' . $i, 'NONSSL') . '">' . $currencies->format($sbp_array[$i-1],true, '', '' , 5) . TEXT_INFO_ABOVE . '</a><br>');

 

and that's it!

 

Notice the 5 on the end $currencies->format parameters - this is the part you change if you want bigger or smaller rounding increments for your range.

 

As I've said, I'm not a php programmer, so please if you can, embrace and extend and post results here.

 

Tom

OSC Image Magic

On-the-fly thumbnails, watermarks and image processing

Link to comment
Share on other sites

Oh yeah,

 

one other change that needs to be made to includes/classes/currencies.php

 

Find:

 

    function display_price($products_price, $products_tax, $quantity = 1, $round=1) {
     return $this->format(tep_add_tax($products_price, $products_tax) * $quantity);
   }

 

and replace with:

 

    function display_price($products_price, $products_tax, $quantity = 1, $round=1) {
     return $this->format(tep_add_tax($products_price, $products_tax) * $quantity,true, '', '' , $round);
   }

 

Now the function currencies->display_price can also optionally take a rounding value.

OSC Image Magic

On-the-fly thumbnails, watermarks and image processing

Link to comment
Share on other sites

  • 5 months later...

For the rounding code, how about:

 

 $calc_price = ceil($calc_price / $round) * $round;

 

This will always round up. If you have a value of 5 for the rounding and an article which costs 7, do you really want to give it away for only 5 and lose 2? Some examples:

 

0.25 --- 3.31$ -> 3.50$

0.50 --- 5.71$ -> 6.00$

0.75 --- 5.22 -> 6.00$, 5.78 --> 6.00$

5 --- 3.73$ -> 5.00$

5 --- 6.00$ -> 10.00$

Link to comment
Share on other sites

  • 3 weeks later...

I would like to make my cart round to the nearest 0.05c. I don't have the "view by price" contribution installed so just wondering where I specify the rounding value - do I do this in the classes/currencies.php file somewhere?

 

Any information is greatly appreciated.

 

Sarah

Link to comment
Share on other sites

  • 1 month later...

This mod had simply no effect om my prices (converted from default currency).

Any idea why not? No error but no change.

 

Also what should we change to alter the rounding margin 5c $1 etc. $round=1 ????

 

A useful mod for me if I could get it up and running!

 

 

Thanks in advance Charlie

 

Works now thanks.

 

Changed $round=1 to $round=.5 on lines 36 and 74 of includes\classes\currencies.php

 

Jolly good and thanks

 

Charlie

 

 

 

But unfortunately it rounds zero up to the next rounding value which is no good if you are using a mod that relies on zero value for its use, skip payment screen for FOC for instance, or if you you want to send some stuff out free of charge. Any idea how to fix this so zero stays zero and say 10c becomes 25c?

Link to comment
Share on other sites

Seem to be answering mu own questions but I changed ( in addition to changing 1 to .25 as mentioned above)

 

 

line 48 from

 

 

$calc_price = 1/$round;

 

to

 

 

$calc_price = 0;

 

and it seems to work. Zero stays zero and above zero get rounded.

 

 

Charlie

Link to comment
Share on other sites

Seem to be answering mu own questions but I changed ( in addition to changing 1 to .25 as mentioned above)

line 48 from

$calc_price = 1/$round;

 

to

$calc_price = 0;

 

and it seems to work. Zero stays zero and above zero get rounded.

Charlie

With the risk of appearing to talk to myself (!) I still can't get this mod to work properly.

 

Now all the prices get rounded to the nearest 5c but the subtotal does not meaning that a product gets listed as 3.75 with a subtotal of say 3.72.

 

 

 

Any ideas chums?

 

Charlie

Link to comment
Share on other sites

  • 4 weeks later...

hey charlie and everyone else who is having problems...sorry for not keeping an eye on this thread, I just forgot completely about it.

 

This mod was only ever meant to be used in the shop by price osc box, but I can see that people do want to use it for all prices in their store.

 

If so, here is the way to do it:

 

Charlie, you a right when you state that the $round=1 will have no effect, but it is meant to be there as a default. That is when a rounding value is not supplied to this method, a default value of 1 is given to it - As one would hope I.E. No rounding. The only place where I changed the call to this method was in the shop by price code, so that actual products prices would remain unaffected.

 

If you want ALL prices to be rounded in your store, rather than just, as originally intended, the shop by price module, then once again you are right and changing the $round = 1 will have a global effect.

 

I think I will turn this into a full contrib, whereby different rounding values can be configured in the admin panel for each currency installed.

 

Charlie, if left to your own devices, you haven't managed to figure out the problem, then I will be glad to help out. Let me know which pages or boxes you are talking about when you say the rounding doesn't happen - the one in the checkout page, cart list page, cart box....there are lots of places where a subtotal is shown?

OSC Image Magic

On-the-fly thumbnails, watermarks and image processing

Link to comment
Share on other sites

As a quick addendum,

 

Firstly, my mistake - the default value is in fact 0 and not 1

Secondly if you want to make all prices round globally across your store, the $round value has to be change in TWO places of the modified code initially posted.

 

Here:

function format($number, $calculate_currency_value = true, $currency_type = '', $currency_value = '', $round=0) {

 

And here:

function display_price($products_price, $products_tax, $quantity = 1, $round=0) {

OSC Image Magic

On-the-fly thumbnails, watermarks and image processing

Link to comment
Share on other sites

  • 2 weeks later...

Works like a charm! You should definitely upload it as a contribution.

 

Good Work!

 

--Peter

CE PHOENIX SUPPORTER

Support the Project, go PRO and get access to certified add ons

Full-time I am a C-suite executive of a large retail company in Australia. In my spare time, I enjoying learning about web-design.

Download the latest version of CE Phoenix from gitHub here

Link to comment
Share on other sites

I could run a successful business if it weren't for the customers.

 

The principle for this mod is great but I can just imagine a customer nit-picking over 3c and a faulty addition. The call will cost more, but will he/she call - yes!

 

The problems is this

 

If A+B=C

 

A(rounded) + B(rounded) does not always = C(rounded)

 

I think we can solve this one, for Pay Pal too. Tomjmul is almost there.

 

What would happen if we simply rounded the variable that passes through the final price to Pay Pal?

 

My gut sense says it won't work every time, but then again....I will try and let you know.

 

Charlie

Link to comment
Share on other sites

Just realised too that the rounding doesn't pass throught Paypal. This will make a great contribution once issues like these are thought out.

 

Keep up the good work Tomjul!

 

--Peter

CE PHOENIX SUPPORTER

Support the Project, go PRO and get access to certified add ons

Full-time I am a C-suite executive of a large retail company in Australia. In my spare time, I enjoying learning about web-design.

Download the latest version of CE Phoenix from gitHub here

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...