如何通过Magento类别页面上的自定义属性过滤产品列表? [英] How to filter product list by custom attribute on category page of Magento?
问题描述
我在类别页面上做了3个标签,分别是所有产品",在线产品"和仅存储产品".
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.
我使用
{{block type="catalog/product_list" template="catalog/product/list.phtml"}}
去做.
在线产品将显示等于在线产品"的自定义属性(product_type)
Online Products will display custom attribute (product_type) equals "Online Product"
仅存储产品"将显示等于仅存储产品"的自定义属性(product_type)
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?
推荐答案
最简单的方法是复制List块并添加所需的过滤器.
The easiest way is to make a duplicate of the List block and add the filter you need.
您可能还需要将Magento管理区域中的属性修改为在产品列表中显示".
例如:
{{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
到
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
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"}}
这篇关于如何通过Magento类别页面上的自定义属性过滤产品列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!