Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Currency, CurrencySymbol, CurrencyField, £, $, GBP - EXPLAINED!


Reply


4 Posts   832 Views

Avatar
CHD

Community Member, 218 Posts

21 September 2012 at 12:20am

Edited: 21/09/2012 12:20am

So today I pulled my hair out for an hour trying to do somehting as simple as display £ signs in the CMS.
I've had this issue before with the 2.x versions of SS (not sure if its sorted in 3?) and usually you have to hack the Currency.php file which has

protected static $currencySymbol = '$';


why this is hardcoded in I'll never understand.

the problem is changing the $ to £ doesnt update the actual CMS fields, so you'll still see dollars all the time.
I tried updating CurrencyField.php as that too has dollar signs hardcoded in

function setValue($val) {
      $value = ($val) ? $val : 0.00;
      $this->value = '$' . number_format((double)preg_replace('/[^0-9.\-]/', '', $value), 2);
   }

So naturally I tried changing the $ to a £. which showed the "?" in the CMS as an unrecognised symbol.
Changing it to £ just displayed the curreny as £50.00
So I had to (In Dreamweaver) go to Modify>Page Properties>Title/Encoding and change to UTF-8.

and that fixed it.

I hate hackign core files, they'll only get overwritten at some point, but sometimes SS really seems to leave you no choice.

Anyway, hopefully this helps.

Avatar
CHD

Community Member, 218 Posts

21 September 2012 at 12:42am

Edited: 21/09/2012 12:43am

P.S - You'll also find the fields will now throw up JS validation errors, because the stupid thing is hardcoded to look for $ signs, if there are none, it's not allowed.

I checked regexpress for a GBP validator, but couldnt find one, so i've just ripped the JS validator function now as i'm simply too sick of this to carry on.

I'm 100% sure that what I've done is a load of unnecessary crap, so PLEASE if somebody knows a simpler way to handle non $ currency/money in a basic SS install without the eCommerce module, let us all know!

cheers

Avatar
Willr

Forum Moderator, 5513 Posts

22 September 2012 at 4:57pm

Patches welcome! Obviously a legacy thing, no ones needed to update all the currencies framework wide (though sooner or later someone will!) Every reference throughout needs to be updated to use that variable you pointed out rather than hard coded string. There already exists a Currency::setCurrencySymbol(); function to override it globally but there doesn't appear to be a getter which is perhaps why it's not used. Raise it as a ticket (if it isn't already) on open.silverstripe.org and if you can list all the road blocks you're running that will help it get fixed for the next version.

Avatar
merrick_sd

Community Member, 97 Posts

8 January 2014 at 3:21am

in my config.php i've set
Currency::setCurrencySymbol('£');

and on the front end this works
$Price.Nice

However in my admin area its show a $

public static $db = array(
'Price' => 'Currency');

public function getCMSFields() {
$fields = parent::getCMSFields();

$fields->addFieldToTab('Root.Main', new CurrencyField("Price", "Price"));
return $fields;

}