18

I have a product content type that has 2 price fields.

I want to create a view that shows all products where price-A is less than price-B.

I was hoping I could just add a filter and select price-A then choose operate less than and set the value as price-B, however you can't do that.

I have looked into using a views hook, mainly hook_views_query_alter but haven't had any luck.

I've also tried using the Views PHP module and been unable to get it to work.

Does anyone know the best way to achieve this?

5 Answers 5

12

What you're looking for is the filter criteria Global: Fields comparison:)

2
4

Welcome to the Drupal Answers Stack Exchange ! There is no straight forward way to achieve this in Drupal views as of now, and as i know.

You can do this using Views PHP module. Enable this module and add a filter PHP filter which you can select from views filter Global Category once you install above module.

Add php code some thing like below in the PHP code section of filter.

$node = node_load($data->nid);

$field_a = field_get_items('node', $node, 'field_price_a');
$price_a = field_view_value('node', $node, 'field_price_a', $field_a[0]);

$field_b = field_get_items('node', $node, 'field_price_b');
$price_b = field_view_value('node', $node, 'field_price_b', $field_b[0]);

if ($price_a >= $price_b) {
  return TRUE;
}

Change the field_price_a with your Price A field name and field_price_b with your Price B field name.

I tested and it worked for me ! Hope you can crack it too :-)

2
  • Thanks for the suggestion. I'm still unable to get it working with this bit of code and have used the fields that show up when doing dsm($data); I think I just might add in a checkbox as well and filter if checkbox is checked. Thanks for your help.
    – Fadzy
    Commented Sep 19, 2012 at 6:00
  • Meanwhile, it is possible with views, without any custom coding oder any contributed module needed.
    – Elin Y.
    Commented Mar 22, 2017 at 16:19
0

This functionality is now included in Views starting with version 7.x-3.8. See https://www.drupal.org/node/699252#comment-7513087 for more.

-1

http://drupal.org/project/views_dependent_filters is pretty useful...

The add-button of "Filter Criteria" has new submenues. Selfexplaning once you try it...

EDIT: sorry, but not for > or <, but for filtergroups which can be seperated by OR

-2

If price is a field in your content type, in Drupal 7 views you can automatically, and easily choose a field com-parer. I guess this should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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