Skip to main content

Views filter items by price range.

Today I needed to create a view the filtered items by a range of prices. The prices for these nodes were stored in a CCK number field. After searching for an easy 'plug and play' method of doing this I quickly began writing my own code to accomplish this task. To summarize, I accomplished this by using the following functions:

  • hook_form_alter()
  • hook_views_query_alter()

Step 1.

Begin by creating your view and adding your CCK number field as an exposed filter. You'll want to be sure that you use the 'is between' operator. You can leave the Min and Max fields blank.

Step 2.

Using hook_form_alter, alter the exposed filter form and change the current form:

[field_price_value] => Array
        (
            [#tree] => 1
            [min] => Array
                (
                    [#type] => textfield
                    [#title] => 
                    [#size] => 30
                    [#default_value] => 
                )

            [max] => Array
                (
                    [#type] => textfield
                    [#title] => And
                    [#size] => 30
                    [#default_value] => 
                )

        )

to become a 'select' field.

[field_price_value] => Array
        (
            [#tree] => 1
            [min] => Array
                (
                    [#type] => textfield
                    [#title] => 
                    [#size] => 30
                    [#default_value] => 
                )

            [max] => Array
                (
                    [#type] => textfield
                    [#title] => And
                    [#size] => 30
                    [#default_value] => 
                )

            [#type] => select
            [#options] => Array
                (
                    [] => All Prices
                    [1-50000] => Under $50,000
                    [50001-100000] => $50,000 to $100,000
                    [100001-150000] => $100,000 to $150,000
                    [150001] => Over $150,000
                )

            [#multiple] => 
            [#size] => 1
            [#default_value] => All
        )

Set your options array to be the range of prices you would like separated by dashes.

Step 3.

Next I used hook_views_query_alter() to alter the query of this view. My function looks like this:

  1. function MYMODULE_views_query_alter(&$view, &$query){
  2.   // Check your view ID here.  
  3.   if ($view->vid == 1) {
  4.       // Check to see if your exposed filter has a value.
  5.       if(isset($_GET['field_price_value'])) {
  6.         // If a value exists, turn it into an array using the dash as a separator.
  7.         $prices = explode('-', $_GET['field_price_value']);
  8.         switch ($prices[0]) {
  9.           // Check your beginning prices and 
  10.           case 1:
  11.           case 50001:
  12.           case 100001:            
  13.             // Remove the last two pricing elements.
  14.             array_pop($query->where[0]['args']);
  15.             array_pop($query->where[0]['args']);
  16.  
  17.             // Add the correct prices to the args.
  18.             array_push($query->where[0]['args'], $prices[0]);
  19.             array_push($query->where[0]['args'], $prices[1]);
  20.           break;
  21.           case 150001:
  22.             // Remove the last clauses and add our own.
  23.             array_pop($query->where[0]['clauses']);
  24.             array_pop($query->where[0]['clauses']);
  25.             array_push($query->where[0]['clauses'], 'node_data_field_price.field_price_value >= %f');
  26.  
  27.             // Remove the last two pricing elements.
  28.             array_pop($query->where[0]['args']);
  29.             array_pop($query->where[0]['args']);
  30.  
  31.             // Add the correct prices to the args.
  32.             array_push($query->where[0]['args'], $prices[0]);     
  33.           break;
  34.         }
  35.       }
  36.     }
  37. }

Basically, you end up removing the last two arguments from the queries where clause and then re-adding them with the array_push() function.

For the last item we remove the where clauses altogether and only add the greater than clause back to the query.

PLEASE NOTE:
The code above only works when the exposed filter is the LAST one in your list of filters.


Comments