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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[Solved] .Nice not showing on a function returning an integer / currency...


Go to End


5 Posts   2065 Views

Avatar
SnowBoarder82

Community Member, 57 Posts

13 November 2014 at 1:43pm

Hey Guys,
I think i'm missing something obvious here so was hoping someone could help set me right.

I have two currency fields "Price" and "OriginalPrice" set on a Product Page / Dataobject.

When I use $Price.Nice or $OriginalPrice.Nice in a SS template I get the desired result of these being displayed correctly - ie: $85.00.

I want to display the result of the difference between the two, or subtracting one from the other. To do this I created the following function (feel free to suggest a better way if applicable):

function Savings(){
$savings = $this->OriginalPrice - $this->Price;
return $savings;
}

This returns the correct result and shows on the template if I use $Savings, however if I use $Savings.Nice - nothing shows.....

Any help would definitely be appreciated.

Thanks

Avatar
tim-lar

Community Member, 3 Posts

13 November 2014 at 4:43pm

Hey SnowBoarder82,

This works for me in the DataObject (note the change of the function from Savings() to getSavings()):

private static $casting = array(
    "Savings"           =>      "Currency"    
);
  
function getSavings(){ 
    $savings = ($this->OriginalPrice - $this->Price);  // assuming that OriginalPrice and Price return numeric values
    return $savings; 
}

Continue to use

<p>{$Savings.Nice}</p>

in the front-end.

If it's not working, try the usual dev/build / flush combo.

Hope that helps!

Avatar
SnowBoarder82

Community Member, 57 Posts

13 November 2014 at 4:59pm

Thanks tim-lar,

I've got that working correctly now with your help - Cheers!
Quick note: Interestingly I wasn't able to run a dev/build without changing the "private static" declaration to "public static"... with that change it processed and runs fine.

Thanks again,

Avatar
tim-lar

Community Member, 3 Posts

13 November 2014 at 5:05pm

Good to hear!

The public vs. private static issue - I tested it on v 3.1.5, I'm guessing you're on v 3.0.x?

Avatar
SnowBoarder82

Community Member, 57 Posts

13 November 2014 at 5:12pm

Got ya, this is actually doing some work on an even older site - that'd explain it. Thanks again.