0

您能否帮助我解决如何将有源过滤器应用于产品集合?

例如:

$_category = Mage::getModel('catalog/category')->load($category_id);
$productsCollection = $_category->getProductCollection();

获取有源过滤器:

$_filters = Mage::getSingleton('catalog/layer')->getState()->getFilters();

如何使用 $_filters 过滤 $productsCollection 中的产品?

感谢您的关注!

4

2 回答 2

1

Magento 将过滤器应用于函数Mage_Catalog_Model_Resource_Layer_Filter_Attribute::applyFilterToCollection中的集合。按照其逻辑,您可以使用以下代码:

$_filters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
$productsCollection = $_category->getProductCollection();
foreach ($_filters as $filterItem) {
    $filter = $filterItem->getFilter(); /**Mage_Catalog_Model_Layer_Filter_Attribute**/
    $attribute  = $filter->getAttributeModel();
    $connection = Mage::getSingleton('core/resource')->getConnection('core_read');
    $tableAlias = $attribute->getAttributeCode() . '_idx';
    $conditions = array(
        "{$tableAlias}.entity_id = e.entity_id",
        $connection->quoteInto("{$tableAlias}.attribute_id = ?",
        $attribute->getAttributeId()),
        $connection->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()),
            $connection->quoteInto("{$tableAlias}.value = ?", $value)
        );

    $productsCollection->getSelect()->join(
        array($tableAlias => $filter->getResource()->getMainTable()),
        implode(' AND ', $conditions),
        array()
        );

}
于 2013-02-21T16:03:19.850 回答
1

选择类别时出现的错误是过滤器对象属于类型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()
            );
    }
于 2013-02-22T16:15:21.017 回答