I am using your module and it is great.
Please tell me how can I add price filter to my Ubercart catalog with this module?

Thanks.

Comments

3cwebdev’s picture

If you were using the 2.x version I could guide as to how I accomplished creating a price range filter (requires hacking the module).

Spider84’s picture

Yes I am using 2.0 version. I have made a mistake when creating this issue. Can you share this code for me? I will be very gratefull to you, I need this feature very much.

Thank you.

3cwebdev’s picture

Version: 6.x-1.x-dev » 6.x-2.x-dev

The concept that I used to make price range filters on my site is below. Please adapt it to suit your site.

In uc_advanced_catalog.filters.inc add the following code to uc_advanced_catalog_catalog_filters() before return $fields;. This adds the filter to the display. You can adjust the pricing accordingly. Part of this code finds the lowest and highest prices in the current category and removes price ranges that are under and over these so that some unneeded ranges won't be displayed.

    $options= array(
      'all' => t('All'),
      '$1-$199' => 'less than $199',
      '$200-$299' => '$200-$299',
      '$300-$499' => '$300-$499',
      '$500-$899' => '$500-$899',
      '$900-$1299' => '$900-$1299',
      '$1300-$1699' => '$1300-$1699',
      '$1700-$2499' => '$1700-$2499',
      '$2500-$3500' => 'more than $2500',
    );    

    // Find lowest and highest prices in view
    $sql = "
    SELECT MAX(sell_price)
    FROM {uc_products} AS uc_products
    INNER JOIN {node} AS n ON n.nid = uc_products.nid
    INNER JOIN {term_node} AS term_node ON n.vid = term_node.vid
    WHERE term_node.tid = %d
    ";
    
    $high = db_result(db_query($sql, arg(1)));    
    
    $sql = "
    SELECT MIN(sell_price)
    FROM {uc_products} AS uc_products
    INNER JOIN {node} AS n ON n.nid = uc_products.vid
    INNER JOIN {term_node} AS term_node ON n.vid = term_node.vid
    WHERE term_node.tid = %d AND n.status = 1
    ";
    
    $low = db_result(db_query($sql, arg(1)));    

    // Strip $ sign and dashes to make array containing low and high filter limits
    foreach ($options as $option){
      if ($option == 'All') continue;
    
      $range = str_replace('$','',$option);
      if ($range == 'less than 199'){      
        $p_range[] = array(0,199);       
        
      }elseif ($range == 'more than 2500'){
        $p_range[] = array(2500,10000);
      }else{
        $p_range[] = explode('-',$range);
      }      
    }
    
    $count = 0;
    foreach ($options AS $key => $option){        
    
    if ($option == 'All') continue;           
      if ($low>$p_range[$count][1]){        
        unset($options[$key]);     
         $count++;        
      }       
    }

    $count = 0;
    foreach ($options AS $key => $option){    
    if ($option == 'All') continue;      
      if ($high<$p_range[$count][0]){        
        unset($options[$key]);     
        $count++;        
      }       
    }
   
    $fields[] = array(
      'name' => 'price_range',
      'edit' => array(
        '#title' => 'Price Range',
        'default' => array(
          '#type' => 'select',
          '#options' => array('all' => 'All'),
        ),
        '#description' => t('Price Range'),
      ),
      'view' => array(
        '#type' => 'select',
        '#prefix' => 'Price',
        '#options' => $options,
      ),
      '#weight' => 9,
      '#default_state' => FALSE,
    );

Then in uc_advanced_catalog.pages.inc, and the following code to uc_advanced_catalog_products() right after this snippet

      else {
      // table storage
        $inner .= "  INNER JOIN {content_$name} AS $name ON n.vid = $name.vid \n";
        $and .= " AND ". $name .".". $name ."_value = '%s' \n";
        $sql_args[] = $value;        
      }
    }
<
    // Price Filter    
    if (ereg('price_range', $key) && $defaults[$key] && $defaults[$key] <> 'all') {
      $range = str_replace('$','',$defaults[$key]);
      
      $and .= " AND uc_products.sell_price >= %d AND uc_products.sell_price <= %d \n";
      
      if ($range == 'less than 199'){
        $range[0] = 0;
        $range[1] = 199;
      }elseif ($range == 'more than 2500'){
        $range[0] = 2500;
        $range[1] = 10000;
      }else{
        $range = explode('-',$range);
      }
      
      $sql_args[] = $range[0];
      $sql_args[] = $range[1];            
    }
  }

This part adds the query to provide that actual price filter functionality.

I hope this makes sense. I couldn't make a patch because I have many other hacks in the module as well.

3cwebdev’s picture

The following snipped should take care of empty catalog pages the occur when set set filters and then go to a different category page.

In uc_advanced_catalog.pages.inc, in function uc_advanced_catalog_browse() after the line that says:

$defaults = _uc_advanced_catalog_default();
  // If this is a new catalog page then reset the filters
  if ($_SESSION['uc_advanced_catalog']['tid'] != $tid){  
    $_SESSION['uc_advanced_catalog']['tid'] = $tid;
    
    foreach ($defaults AS $key => &$default){    
        if ($key != 'limit' && $key != 'mode' && $key != 'order' && $key != 'sort')
      {
         $default = 'all';
      }      
    }
  }
mogtofu33’s picture

Priority: Normal » Minor
Status: Active » Needs review

Hi,

Perhaps you could simply create a vocabulary to handle price range. Taxonomy will need to be selected on each product edit form, but it will make it very easy to manage.

Regards.
Mog.

MakeOnlineShop’s picture

Issue summary: View changes

Hello,

Do you know any other way to show only some items which price is below 19.90 without hacking the module ?

Thank you.