Jump to content

[solved] Use BlockLayered class methods and/or DB tables for price range button?


stratboy

Recommended Posts

Hi, I've got to build a standalone menu button with submenu that contains links to price ranges.

 

I activated the blocklayered module (not for this task, only for regular left-column filters). So the relative db tables are in place and populated.

 

I want to make a controller specific for price ranges. So I've got to do the right query and maybe set up the same url vars as the blocklayered module so they wil not conflict.

 

Would it be too crazy to import blocklayered or blocklayered-ajax in my controller and use part of their functionality? Maybe not good because of object duplication or other issues?

 

Or maybe, would it be a bad idea to use the blocklayered tables (for example layered_price_index) to help me get filtered products? I'm wandering if it would be a better solution than re-doing all by myself, or if instead it's not good for some reason.

 

Any idea?

Edited by stratboy (see edit history)
Link to comment
Share on other sites

Hi, for now, the answer is YES I can. Or at leat almost yes. I do not import the blocklayered class, but I use the layered_price_index table, that has already all the min and max things correctly calculated, indexed and up to date (obviously, that means that I installed the module). My current code to build the submenu is this:

 

 $sql = '
  SELECT
MIN(price.price_min) min,
MAX(price.price_max) max
  FROM '._DB_PREFIX_.'layered_price_index price';

 $price_boudaries = $db->executeS($sql);
 $overall_min = $price_boudaries[0]['min'];
 $overall_max = $price_boudaries[0]['max'];

 $ranges = array(
  array('min' => $overall_min, 'max' => 100)
  ,array('min' => 100, 'max' => 300)
  ,array('min' => 300, 'max' => 500)
  ,array('min' => 500, 'max' => $overall_max)
 );

 $parent_category = 11;

 $price_sub = '<ul>';
 $currency = $this->context->currency->sign;

 $ranges_count = count($ranges);

 for($i=0; $i<$ranges_count; $i++){

  $item = $ranges[$i];

  $min_price = $item['min'];
  $max_price = $item['max'];

  $price_sub .= (($i==0) ? '<li class="first">' : (($i == $ranges_count-1) ? '<li class="last">' : '<li>')) .
'<a href="'.$base_url.'index.php?id_category='.$parent_category.'&controller=category#/price-'.$min_price.'-'.$max_price.'">' .
'Da ' . $min_price . $currency . ' a ' . $max_price . $currency .
'</a></li>';
 }//end for

 $price_sub .= '</ul>';

 

It works. I use the sql query on the layered_price_index table only to get min and max boundaries (just to write more accurate ranges). One could easili write a small function to automatically get 3 or 4 (or n) price ranges out of that table, just as the module does. Anyway, for now I can manually write them.

 

As you can see, the link will be something like this:

 

http://www.mysite.co...#/price-502-720

 

No matter what values you choose in price-min-max, the controller (or blocklayered the module) will properly round and reformat them, and you'll get the right product list. Also, in the resulting category page, if you have blocklayered module in place and you use some other filter, it will work well taking in account the already setted price filter. So all fine.

 

I wanted to share.

 

Bye!

Edited by stratboy (see edit history)
Link to comment
Share on other sites

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...