Magento 1.9.2 – Apply Layer Filter to Custom Product Collection

collection;layered-navigationmagento-1.9product-collection

I want to apply layer navigation filter in custom product collection.

Ex:- http://domain.com/seller/collection/test/?condition=new&brand=xyz

Now I have collection of products of particular seller as per below code.

  $layer = Mage::getModel('catalog/layer');
  $category = Mage::getModel('catalog/category')->load(Mage::app()-  >getStore()->getRootCategoryId());
  $layer->setCurrentCategory($category);
  $collection = $layer->getProductCollection();
  $collection->addAttributeToSelect('*');

  $collection->addAttributeToFilter('entity_id', array('in' => '339,340,345,356'));
  $collection->addAttributeToFilter('visibility', array('in' => array(4)));

  $this->setCollection($collection);

Now before set collection ($this->setCollection($collection)), I want to apply layered filter to collection as per query string.

Means apply condition and brand filter.

Please help me as soon as possible. I have waste almost one day in this customization.

Or give me some alternative solution.

Best Answer

Try using below code,

  $params = $this->getRequest()->getParams();


  $layer = Mage::getModel('catalog/layer');
  $category = Mage::getModel('catalog/category')->load(Mage::app()-  >getStore()->getRootCategoryId());
  $layer->setCurrentCategory($category);
  $collection = $layer->getProductCollection();
  $collection->addAttributeToSelect('*');

  $collection->addAttributeToFilter('entity_id', array('in' => '339,340,345,356'));
  $collection->addAttributeToFilter('visibility', array('in' => array(4)));

  if(count($params)){
      // Make sure array key is attribute code
      foreach($params as $key => $value) {
          $collection->addAttributeToFilter($key, array('eq' => $value));
      }
  }


  $this->setCollection($collection);
Related Topic