How to filter product list by custom attribute on category page of Magento?

南楼画角 提交于 2019-12-08 06:22:09

问题


I made 3 tabs on category page, which are All Products, Online Products and Store Only Products.

All products display all products under this category.

I use

{{block type="catalog/product_list" template="catalog/product/list.phtml"}} 

to do the job.

Online Products will display custom attribute (product_type) equals "Online Product"

Store Only Products will display custom attribute (product_type) equals "Store Only Product"

How can I create a block to display Online Products and Store Only Products under this category?


回答1:


The easiest way is to make a duplicate of the List block and add the filter you need.

You May need to modify your attribute in the Magento admin area to "show in product listing" too.

for example:

{{block type="catalog/product_list" template="catalog/product/list.phtml"}}

this will use the List block to filter the collection for you, lets make a copy:

app/code/core/Mage/Catalog/Block/Product/List.php

to

app/code/local/Mage/Catalog/Block/Product/Mylist.php

Now lets modify the Block to use our custom attribute, something like this should work (not tested)

Mylist.php

class Mage_Catalog_Block_Product_Mylist extends Mage_Catalog_Block_Product_List
{
    /**
     * Retrieve loaded category collection
     *
     * @return Mage_Eav_Model_Entity_Collection_Abstract
     */
    protected function _getProductCollection()
    {
        $collection = parent::_getProductCollection();

        $collection->addAttributeToSelect('my_attribute')
            ->addAttributeToFilter('my_attribute', array('eq' => '000001'))
        ;

        return $collection;
    }
}

Now you simple use your new block:

{{block type="catalog/product_mylist" template="catalog/product/list.phtml"}}



回答2:


Never, but NEVER, create or modify a Magento CORE class. The answer is to implement your module, override the List block class and customize the method _getProductCollection.



来源:https://stackoverflow.com/questions/16441148/how-to-filter-product-list-by-custom-attribute-on-category-page-of-magento

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!