选择类别时出现的错误是过滤器对象属于类型Mage_Catalog_Model_Layer_Filter_Category
并且没有属性attribute_model
。您必须在过滤器循环中添加检查以防止出现这种情况:
foreach($_filters as $filterItem)
{
$filter = $filterItem->getFilter();
if (!($filter instanceof Mage_Catalog_Model_Layer_Filter_Attribute)) {
continue;
}
多个选择过滤器值的问题也可以通过两种方式解决。如果您希望以 OR 方式应用过滤器值,请执行以下操作:
$connection->quoteInto("{$tableAlias}.value = in(?)", explode('_', Mage::app()->getRequest()->getParam($attribute->getAttributeCode())))
如果必须以 AND 方式应用过滤器值,则该过程有点复杂。基本上,您必须对同一个表执行多个连接,并且必须为每个连接提供唯一的表别名:
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$tableAlias = $attribute->getAttributeCode() . '_idx';
$values = explode('_', Mage::app()->getRequest()->getParam($attribute->getAttributeCode()));
foreach ($values as $value) {
$tableAliasModified = $tableAlias.'_'.$value;
$conditions = array(
"{$tableAliasModified}.entity_id = e.entity_id",
$connection->quoteInto("{$tableAliasModified}.attribute_id = ?",
$attribute->getAttributeId()),
$connection->quoteInto("{$tableAliasModified}.store_id = ?", Mage::app()->getStore()->getStoreId()),
$connection->quoteInto("{$tableAliasModified}.value = ?", $value)
);
$productsCollection->getSelect()->join(
array($tableAliasModified => Mage::getResourceModel('catalog/layer_filter_attribute')->getMainTable()),
implode(' AND ', $conditions),
array()
);
}