Tags/topics: 
1
Answers
Vote up!
0
Vote down!

How do I show per-user discounts? For example, "ON SALE!"

I need a simple call or an idea of what to look for to see if a product has been discounted so that I can show an "ON SALE" image on the display node.

Any ideas?

Asked by: criley
on July 17, 2012

1 Answer

Vote up!
1
Vote down!

I recommend you use http://drupal.org/project/computed_field and a "computed field" to your product type.

I just did this on Kickstart 2 and had much success with this bit of php code:

<?php
$price
= $entity->commerce_price['und'][0];
$calculated_price = commerce_product_calculate_sell_price($entity);
if (
$calculated_price["amount"]<$price["amount"]) {
 
$entity_field[0]['value'] = "On Sale!";
} else {
 
$entity_field[0]['value'] = "Normal Price";
}
?>

You will need to add this computed field to a product type, not a content type. Additionally, you will need to make this new field "visible" (not hidden) in the "Manage Display" of the same product type.

You should test to see if the value of that field is cached in any way, because the price is recalculated per-user and we wouldn't want to say to an anonymous user that they have a price on sale if it wasn't.

Additionally, there is a great module http://drupal.org/project/commerce_extra_price_formatters that gives you a lot of control over how you show the person's savings without resorting to a bit of php like I did above.

Josh Miller
Answer by: Josh Miller
Posted: Sep 5, 2012