2

I have two different headers in my Magento one for retail shop and second is for commercial shop in same store view. In both header Product category will be same.

  • Condition #1 :- In retail shop when we click on "Products" category then it will be list all products.
  • Condition #2 :- when I will change click on commercial shop header then it will be show only commercial products I have assigned custom attribute for it. Is this possible ?

I found code for loaded product collection in this file \app\code\core\Mage\Catalog\Block\Product\List.php can we add attribute filter in this function ?

protected function _getProductCollection()
    {
        if (is_null($this->_productCollection)) {
            $layer = $this->getLayer();
            /* @var $layer Mage_Catalog_Model_Layer */
            if ($this->getShowRootCategory()) {
                $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
            }

            // if this is a product view page
            if (Mage::registry('product')) {

                // get collection of categories this product is associated with
                $categories = Mage::registry('product')->getCategoryCollection()
                    ->setPage(1, 1)
                    ->load();

                // if the product is associated with any category
                if ($categories->count()) {
                    // show products from this category
                    $this->setCategoryId(current($categories->getIterator()));
                }
            }

            $origCategory = null;
            if ($this->getCategoryId()) {
                $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
                if ($category->getId()) {
                    $origCategory = $layer->getCurrentCategory();
                    $layer->setCurrentCategory($category);
                    $this->addModelTags($category);
                }
            }
            $this->_productCollection = $layer->getProductCollection();

            $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

            if ($origCategory) {
                $layer->setCurrentCategory($origCategory);
            }
        }

        return $this->_productCollection;
    }
  • You can also refer the link where Sorting of product by attributes. inchoo.net/magento/magento-products/… Help of above example you can add you own attribute. – Somnath Jul 20 '15 at 9:39
  • Can you find the way to filter product collection on layered navigation and category page using attribute,if you find than please post it... – ND17 Jan 28 '16 at 10:46
  • @ND17 have you tried below mentioned Piyush's code ? – Arunendra Jan 28 '16 at 11:01
  • yes but its only filter in category page not in layered navigation and toolbar is also not given correct item – ND17 Jan 28 '16 at 11:03
  • have you tried it in toolbar.php public function setCollection($collection)() ?? – Arunendra Jan 28 '16 at 11:20
4

For your second Condition you can Filter Your Product Collection Like This :-

$products=Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToFilter('your_custom_attribute_code', 'your_custom_attribute_value');

You Can Add this filter at File list.php

app\code\core\Mage\Catalog\Block\Product\List.php Find The setCollection method

and under $this->_productCollection = $collection;

add this code

$collection->addAttributeToFilter('your_custom_attribute_code', 'your_custom_attribute_value');

  • 1
    I think it's addAttributeToFilter for product collections, not addFieldToFilter. – Marius Jul 20 '15 at 6:03
  • @Piyush I know this type filter but i need it done in product list so it will also apply on layer navigation see my updated question. – Arunendra Jul 20 '15 at 6:13
  • @Arunendra you can apply this filter on setCollection method of block file. – Piyush Jul 20 '15 at 6:20
  • @Piyush you mean in list.phtm can you show me query for it. – Arunendra Jul 20 '15 at 6:25
  • @Arunendra I have updated the answer. – Piyush Jul 20 '15 at 6:31

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.