如何使用活动筛选器筛选产品集合


How to filter products collection using active filters?

您能帮我提供一个如何将活动过滤器应用于产品集合的解决方案吗?

例如:

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

正在获取活动筛选器:

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

如何使用$_filters筛选$productsCollection中的产品?

感谢您的关注!

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()
        );
}

选择类别时出现的错误是筛选器对象的类型为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()
            );
    }