0

I'm trying to calculate the price difference between old price and new price, and if the price has dropped to display by how much, both Nominally and in Percent.

So, basically these are 2 simple formulas that should look like this:

NOMINAL PRICE DROP = field_price_original - field_price_new

PERCENTAGE PRICE DROP = ( (field_price_new - field_price_original) / field_price_original ) * 100

I tried setting this up through Rules (unsuccessfully), but it seems that doing it through Computed Field will not be so heavy on the server. Using Computing Field I have the following formula:

$entity_field[0]['value'] = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_price_original'))) - array_pop(array_pop(field_get_items($entity_type, $entity, 'field_price_new')));

The number is set as float, 10.2. But for some reason it doesn't work.

Can someone please help and provide the correct syntax for Computed Field?

1

As per the documentation of computation field module to calculate the difference of two fields you need to use the PEAR:

$path = '/pathToPearsParentDirectory/pear/PEAR';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

require_once 'PEAR.php'; 
include_once "Text/Diff.php"; 
include_once "Text/Diff/Renderer.php"; 
include_once "Text/Diff/Renderer/inline.php";

$diff = &new Text_Diff('auto', array(array($node->field_nameOfOneFieldToCompare[0]['value']), array($node->field_nameOfOtherFieldToCompare[0]['value']))); $renderer
= &new Text_Diff_Renderer_inline();

$node_field[0]['value'] = $renderer->render($diff);

More info about it available here http://arencambre.com/blog/2009/05/17/how-i-got-field-diffs-working-with-drupal-pear-text_diff-and-dreamhost

  • @drupalina Has the above solution worked for you, or if you found some better solution than please share. Thanks!! – Ankit Agrawal Oct 1 '13 at 20:56
-1
$entity_field[0]['value'] = $entity->field_price[LANGUAGE_NONE][0]['value'] - $entity->field_cost[LANGUAGE_NONE][0]['value'];
  • If it's an answer to the question please provide some description. – Krishna Mohan Nov 6 '14 at 4:46
  • Also, please avoid encouraging people to use the field arrays directly like that. field_get_items, as used by the OP, is helpful here. – Letharion Nov 6 '14 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.