Display % discount off List Price

Posts: 1
Joined: 04/23/2008

Hi

This should be an easy one for someone who knows what they are doing! I'm looking to display the amount of discount offered off the List Price of a product on each products description e.g

List Price:£999
Our Price:£499
Discount:50%

Can anyone point me in the right direction?

Cheers

Martin

Posts: 50
Joined: 02/14/2008
Bug Finder

Hi Martin,
you could use http://drupal.org/project/computed_field

Add a computed field to your product type, in Computed Code you enter without the PHP tags:

<?php
if ($node->list_price != $node->sell_price && $node->list_price > '0.01') {
 
$node_field[0]['value'] = round((1 - ($node->sell_price / $node->list_price)) * 100, 0);
}
?>

in Display Format:

<?php
if ($node_field_item['value']) {
 
$display = t('Discount') . ': ' . $node_field_item['value'] . '%';
}
?>

Use with caution, it's a long time since I did something similar, so no guarantee, but it should work.

Posts: 3
Joined: 08/07/2007

Hi Al,
thank you for this workaround, it works for me.
If i change the line

<?php
$node_field
[0]['value'] = round((1 - ($node->sell_price / $node->list_price)) * 100, 0);
?>
to
<?php
$node_field
[0]['value'] = round((1 - ($node->sell_price / $node->list_price)) * 100, 2);
?>
i get a discount of "50.56 %" but i want a discount of "50,56 %" ?
I mean the comma in the discount...

In admin/store/settings/store/edit/format
my current format is: € 1.000,12 and
Number of decimal places: 2

Any idea to solve this?

PS. in my node-product.tpl.php i show this field with

<?php
print $node->field_discount[0]['value']
?>

Posts: 659
Joined: 11/05/2007
Bug FinderFAQ ModeratorGetting busy with the Ubercode.

<?php
 
print uc_currency_format($node->field_discount[0]['value']);
?>

--

<tr>.

Posts: 3
Joined: 08/07/2007

Hi TR,

thank you for your reply! I see it's the right way but now i get "Discount: € 50,50 %"
How can i format the field without the € symbol?
I need only 50,50 %. Have you any idea?

Posts: 659
Joined: 11/05/2007
Bug FinderFAQ ModeratorGetting busy with the Ubercode.

<?php
 
print uc_currency_format($node->field_discount[0]['value'], false);
?>

--

<tr>.

Posts: 3
Joined: 08/07/2007

Merci! It works nicely!

Thank you