如何使用有源过滤器过滤产品集合?

时间:2013-02-21 15:28:15

标签: php magento

您能否帮我解决一下如何将有源滤波器应用于产品系列?

例如:

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

获取有效过滤器:

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

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

感谢您的关注!

2 个答案:

答案 0 :(得分: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()
        );

}

答案 1 :(得分: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()
            );
    }