I have a Commerce product and there are two price fields. (Original price and Sale price). I would like to display original price as line-through text when sale price is available. Therefore I would like to add a custom class to a field based on another field value(boolean), I looked at this function, hook_entity_view_alter, but I don't know whether it will be good.

Anyone has idea how do I solve this?


I've never used the commerce module but that sounds possible, you need to create a module for the hook to be triggered from.

Then, with the devel module installed use dpm($build, $type) within the hook to see the correct names (I've put in dummy values in CAPS) and do something like:

function MYMODULE_entity_view_alter(&$build, $type){
  if ($type =='product') {
    if (isset($build['SALE_PRICE']['VALUE'])) {
                $build['PRICE']['attributes']['class'] = array('on-sale');

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.