Jump to content

Guide to properly add a new currency formatting option


Recommended Posts

So, although there is a small reference in the PrestaShop Wiki (PrestaShop Wiki | Currencies), I wanted to add a totally new currency formatting option while preserving the old ones in the Administration panel. Since I couldn't find something for the 1.4 RC6 version, I did it on my own and thought I could share the way with anyone interested.

For this example we'll be adding a format that uses Euro as a currency sign, no thousand separator and a dot as a decimal separator, thus making prices look like this: 2400.45 € or in other words, following this format: 0000.00X. Oh, I'll also assume you're familiar with editing PHP and JavaScript files.

We'll be editing three files:
(all these assume you've installed the default 1.4 RC6 version)

1. AdminCurrencies.php located in prestashop/admin/tabs/
2. Tools.php located in prestashop/classes/
3. tools.js using the default theme, located in prestashop/themes/prestashop/js/

On to the guide then...


Step 1:
- Open up AdminCurrencies.php and find the block of code looking like this (around line 190):

$currency_formats = array(
                   1 => 'X0,000.00 ('.$this->l('as with dollars').')',
                   2 => '0 000,00X ('.$this->l('as with euros').')',
                   3 => 'X0.000,00',
                   4 => '0,000.00X',
               );




This is simply an array for the select list in the Back Office in order to set the $currency_formats variable. We'll add the new currency option to the array here, making the updated code look like this:

$currency_formats = array(
                   1 => 'X0,000.00 ('.$this->l('as with dollars').')',
                   2 => '0 000,00X ('.$this->l('as with euros').')',
                   3 => 'X0.000,00',
                   4 => '0,000.00X',
                   5 => '0000.00X (New format)'
               );




Done. Save and close.


Step 2:
- Open up Tools.php and find the block of code looking like this (inside the displayPrice function, around line 325):

switch ($c_format)
        {
             /* X 0,000.00 */
             case 1:
               $ret = $c_char.$blank.number_format($price, $c_decimals, '.', ',');
               break;
           /* 0 000,00 X*/
           case 2:
               $ret = number_format($price, $c_decimals, ',', ' ').$blank.$c_char;
               break;
etc...



At the end of the last case statement after the break and before the closing bracket, add a new case making the updated code look like this:

switch ($c_format)
        {
             /* X 0,000.00 */
             case 1:
               $ret = $c_char.$blank.number_format($price, $c_decimals, '.', ',');
               break;
           /* 0 000,00 X*/
           case 2:
               $ret = number_format($price, $c_decimals, ',', ' ').$blank.$c_char;
               break;
           /* X 0.000,00 */
           case 3:
               $ret = $c_char.$blank.number_format($price, $c_decimals, ',', '.');
               break;
           /* 0,000.00 X */
           case 4:
               $ret = number_format($price, $c_decimals, '.', ',').$blank.$c_char;
               break;
           /* 0000.00 X */
           case 5:
               $ret = number_format($price, $c_decimals, '.', '').$blank.$c_char;
               break;
       }



Done here as well. Save and close.


Step 3:
- Open up tools.js and find the block of code looking like this (inside the formatCurrency function, around line 90):

    
   if (currencyFormat == 1)
       return currencySign + blank + formatNumber(price, priceDisplayPrecision, ',', '.');
   if (currencyFormat == 2)
       return (formatNumber(price, priceDisplayPrecision, ' ', ',') + blank + currencySign);
etc...



After the last if statement, let's add another one, making the final code look like this:

if (currencyFormat == 1)
       return currencySign + blank + formatNumber(price, priceDisplayPrecision, ',', '.');
   if (currencyFormat == 2)
       return (formatNumber(price, priceDisplayPrecision, ' ', ',') + blank + currencySign);
   if (currencyFormat == 3)
       return (currencySign + blank + formatNumber(price, priceDisplayPrecision, '.', ','));
   if (currencyFormat == 4)
       return (formatNumber(price, priceDisplayPrecision, ',', '.') + blank + currencySign);
   if (currencyFormat == 5)
       return (formatNumber(price, priceDisplayPrecision, '', '.') + blank + currencySign);
   return price;
}



All done. Save and close.

That's pretty much it. Now, in your Back Office at: Payment -> Currencies -> Actions-Edit and in the new window under Formatting, you'll see the new option you just added.

Although I've tested this out on most pages and modules, chances are I'm missing something out. So, I apologize in advance. Anyway, hope it's been helpful :)

Link to comment
Share on other sites

  • 1 year later...
  • 4 months later...

not sure if this applied to you but I a changed another function in tools.js

formatedNumberToFloat

 

If you check tools.js youll see that here there is code relating to the different formats, so at least to avoid any later error/bug it is worth coding your new format here too.

 

cheers

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...