1

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.

0

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);
  • hey Thanks for the reply. But its dynamic string means multiple parameters there. So I want dynamic solution for the same. – Hiren Shah Jun 29 '16 at 5:07
  • Please check updated answer, hope it will help you. Thanks! – Narayan S Jun 29 '16 at 6:38
  • Hey thanks for quick replay and its work. But condition attribute type is dropdown and magento store option id in database for dropdown. Can you please help me with dropdown attribute filtration? – Hiren Shah Jun 29 '16 at 7:39
  • You need to pass option_id of condition attribute to filter. Use same "addAttributeToFilter" it will work. – Narayan S Jun 29 '16 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.