Magento 1.9 – Using Quantity as Filter in Layered Navigation

filterlayered-navigationmagento-1.9

How to make product Quantity wise filter in layered navigation.

See image

enter image description here

Best Answer

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

enter image description here

Related Topic