triston
triston

Reputation: 493

how to make magento filter works like category

I would like to have the categories in magento made use of the attributes/filter.

Let say I have an attribute "CupAttr" which is a NOT used in layered navigation. Then I create a category called CupCat, and it uses the "CupAttr" to pull products to display them within the CupCat category.

is that possible? The reason why i want to do this is I want to minimize the maintenance of categorizing products.

Thanks

EDITED:

Amit's solution works perfectly, but that bring in another issue. the products showing in the list is different from the products can be filtered from the layered navigation.

I actually need to select all products for any category (because i won't add any products to any category, they are all blank), then i start filter the products for that specific category by attribute.

thanks again.

Upvotes: 1

Views: 1076

Answers (1)

Amit Bera
Amit Bera

Reputation: 7611

In this case,you can use magento event/observer.

Hook an observer on event catalog_block_product_list_collection.

Then using addAttributeToFilter('CupAttrAttibiteCode'); filter the collection by CupAttr.

config.xml code:

<?xml version="1.0"?>
    <config>
      <global>
        <models>
            <xyzcatalog>
                 <class>Xyz_Catalog_Model</class>
            </xyzcatalog>
        </models>
        <events>
          <catalog_block_product_list_collection> <!-- event -->
            <observers>
              <xyz_catalog_block_product_list_collection>
                <type>singleton</type>
                <class>Xyz_Catalog_Model_Observer</class>
                <method>apply_CupAttr_filter</method>
              </xyz_catalog_block_product_list_collection>
            </observers>
          </catalog_block_product_list_collection>     
        </events>
      </global>
    </config>

Observer code location:

Create the directory structure - app/code/local/Xyz/Catalog/Model/Observer.php

First "CupAttr" which is used in prouct listing for use this attribute to filtering

<?php
class Xyz_Catalog_Model__Observer
{
    public function __construct()
    {
    }
    public function apply_CupAttr_filter($observer){

        //apply filter when category is CupCat
        if(Mage::registry('current_category') &&(Mage::registry('current_category')->getId()=='CupCatCatrgoryId') ):

        $collection=$observer->getEvent()->getCollection();
        $collection->addAttributeToFilter('CupAttrAttibiteCode','FilterExpression');
        endif;
        return $this;
    }
}

Upvotes: 3

Related Questions