How to make product Quantity wise filter in layered navigation.
See image
First step create block
<?php
class Spacename_Moduelname_Block_Layer_Filter_Quantity
extends Mage_Catalog_Block_Layer_Filter_Abstract
{
public function __construct()
{
parent::__construct();
$this->setTemplate('modeulane/filter/quantity.phtml');
$this->_filterModelName = 'modeulane/layer_filter_quantity';
}
public function getUrlCheck($url,$item)
{
return $url;
}
}
?>
second step create Model
<?php
class Spacename_Modulename_Model_Layer_Filter_Quantity
extends Mage_Catalog_Model_Layer_Filter_Abstract
{
/**
* Class constructor
*/
public function __construct()
{
parent::__construct();
$this->_requestVar = 'qty';
}
/**
* Apply Rating filter to layer
*
* @param Zend_Controller_Request_Abstract $request
* @param Mage_Core_Block_Abstract $filterBlock
* @return FME_Advancedfilter_Model_Layer_Filter_Rating
*/
public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
{
$filter = $request->getParam($this->getRequestVar());
if (!$filter) {
return $this;
}
$select = $this->getLayer()->getProductCollection();
/* @var $select Zend_Db_Select */
$pid1=$select->getAllIds();
$secondvalue=$filter-50;
$products = Mage::getModel('catalog/product')->getCollection()
->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left'
)->addAttributeToFilter('entity_id', array('in' => $pid1))
->addAttributeToFilter('qty', array('lteq' => $filter))->addAttributeToFilter('qty', array('gt' => $secondvalue))->load();
$proids=$products->getAllIds();
$select->addAttributeToFilter('entity_id', array('in' => $proids));
$stateLabel = Mage::helper('catalog')->__('Quantity');
$state = $this->_createItem(
$stateLabel, $filter
)->setVar($this->_requestVar);
/* @var $state Mage_Catalog_Model_Layer_Filter_Item */
$this->getLayer()->getState()->addFilter($state);
return $this;
}
/**
* Get filter name
*
* @return string
*/
public function getName()
{
return Mage::helper('catalog')->__('Qunatity');
}
/**
* Get data array for building rating filter items
*
* @return array
*/
protected function _getItemsData()
{
$data = array();
if($this->_getCount('0-50'))
{
$data[] = array(
'label' => '1-50',
'value' => '50',
'count' => $this->_getCount('1-50'),
);
}
if($this->_getCount('50-100'))
{
$data[] = array(
'label' => '50-100',
'value' => '100',
'count' => $this->_getCount('100-150'),
);
}
if($this->_getCount('100-150'))
{
$data[] = array(
'label' => '100-150',
'value' => '150',
'count' => $this->_getCount('100-150'),
);
}
return $data;
}
/**
*
* Count the number of product in rating filter
*
*/
protected function _getCount($var)
{
// Clone the select
$select = clone $this->getLayer()->getProductCollection();
$pid1=$select->getAllIds();
$secondvalue=explode('-',$var);
$products = Mage::getModel('catalog/product')->getCollection()
->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left'
)->addAttributeToFilter('entity_id', array('in' => $pid1))
->addAttributeToFilter('qty', array('lteq' => $secondvalue[1]))->addAttributeToFilter('qty', array('gt' => $secondvalue[0]))->load();
return count($products);
}
}
Third step rewrite Mage_Catalog_Block_Layer_View
`
<?php
class Namespace_Modulename_Block_Layer_View extends Mage_Catalog_Block_Layer_View
{
protected $_quantityBlockName;
protected function _initBlocks()
{
parent::_initBlocks();
$this->_quantityBlockName= 'modulename/layer_filter_quantity';
}
protected function _prepareLayout()
{
$this_helper=Mage::helper('catalog');
$qunatityBlock = $this->getLayout()->createBlock($this->_quantityBlockName)
->setLayer($this->getLayer())
->init();
$this->setChild('quantity_filter', $qunatityBlock);
return parent::_prepareLayout();
}
public function getFilters()
{
$filters = array();
if ($quantityFilter = $this->_getQuantityFilter()) {
$filters[] = $quantityFilter;
}
$filterableAttributes = $this->_getFilterableAttributes();
foreach ($filterableAttributes as $attribute) {
$filters[] = $this->getChild($attribute->getAttributeCode() . '_filter');
}
return $filters;
}
protected function _getQuantityFilter()
{
return $this->getChild('quantity_filter');
}
}
at the end quantity.phtml
<ol class="filter">
<?php
foreach ($this->getItems() as $_item):
?>
<li>
<a href=" <?php echo $_item->getUrl() ?>" id="<?php echo $_item->getUrl(); ?>" class="filters" >
<?php echo $_item->getLabel() ?>
<span class="count">(<?php echo $_item->getCount(); ?>)</span></a>
</li>
<?php endforeach ?>
</ol>
Output
Solution 1:
Go to Catalog/product/left.phtml
edit this file and add your qty filter
then rewrite this class app/code/core/Mage/Catalog/Block/Product/List.php
and in this class rewrite this method_getProductCollection()
and in this class put your condition based on parameter and your job is done.
Hope this will help you.
I have develop extension for this functionality . It uses attribute called "quantity". It will update on all quantity related observer call like bellow code
<events>
<cataloginventory_stock_item_save_after>
<observers>
<unleaded_quantityfilter>
<type>singleton</type>
<class>quantityfilter/observer</class>
<method>saveStockItemAfter</method>
</unleaded_quantityfilter>
</observers>
</cataloginventory_stock_item_save_after>
<checkout_submit_all_after>
<observers>
<unleaded_quantityfilter>
<class>quantityfilter/observer</class>
<method>checkoutAllSubmitAfter</method>
</unleaded_quantityfilter>
</observers>
</checkout_submit_all_after>
<cataloginventory_stock_revert_products_sale>
<observers>
<unleaded_quantityfilter>
<class>quantityfilter/observer</class>
<method>stockRevertProductsSale</method>
</unleaded_quantityfilter>
</observers>
</cataloginventory_stock_revert_products_sale>
<catalog_product_import_finish_before>
<observers>
<unleaded_quantityfilter>
<class>quantityfilter/observer</class>
<method>catalogProductImportFinishBefore</method>
</unleaded_quantityfilter>
</observers>
</catalog_product_import_finish_before>
<sales_order_item_cancel>
<observers>
<inventory><type>disabled</type></inventory>
<unleaded_quantityfilter>
<class>quantityfilter/observer</class>
<method>cancelOrderItem</method>
</unleaded_quantityfilter>
</observers>
</sales_order_item_cancel>
</events>