如何使 magento 过滤器像类别一样工作
how to make magento filter works like category
我想让 magento 中的类别使用 attributes/filter。
假设我有一个属性 "CupAttr",它不用于分层导航。然后我创建了一个名为 CupCat 的类别,它使用 "CupAttr" 提取产品以在 CupCat 类别中显示它们。
这可能吗?我想这样做的原因是我想尽量减少对分类产品的维护。
谢谢
已编辑:
Amit 的解决方案非常有效,但这带来了另一个问题。列表中显示的产品与可以从分层导航中过滤的产品不同。
我实际上需要 select 任何类别的所有产品(因为我不会将任何产品添加到任何类别,它们都是空白的),然后我开始按属性过滤该特定类别的产品.
再次感谢。
在这种情况下,您可以使用 magento event/observer.
在事件 catalog_block_product_list_collection
上挂一个观察者。
然后使用addAttributeToFilter('CupAttrAttibiteCode')
;按 CupAttr 筛选集合。
config.xml代码:
<?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>
观察者代码位置:
创建目录结构 - app/code/local/Xyz/Catalog/Model/Observer.php
第一个 "CupAttr",用于产品列表,用于使用此属性进行过滤
<?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;
}
}
我想让 magento 中的类别使用 attributes/filter。
假设我有一个属性 "CupAttr",它不用于分层导航。然后我创建了一个名为 CupCat 的类别,它使用 "CupAttr" 提取产品以在 CupCat 类别中显示它们。
这可能吗?我想这样做的原因是我想尽量减少对分类产品的维护。
谢谢
已编辑:
Amit 的解决方案非常有效,但这带来了另一个问题。列表中显示的产品与可以从分层导航中过滤的产品不同。
我实际上需要 select 任何类别的所有产品(因为我不会将任何产品添加到任何类别,它们都是空白的),然后我开始按属性过滤该特定类别的产品.
再次感谢。
在这种情况下,您可以使用 magento event/observer.
在事件 catalog_block_product_list_collection
上挂一个观察者。
然后使用addAttributeToFilter('CupAttrAttibiteCode')
;按 CupAttr 筛选集合。
config.xml代码:
<?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>
观察者代码位置:
创建目录结构 - app/code/local/Xyz/Catalog/Model/Observer.php
第一个 "CupAttr",用于产品列表,用于使用此属性进行过滤
<?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;
}
}