简体   繁体   中英

How to filter products collection using active filters?

Could you please help me with a solution as to how I can apply active filters to products collection?

For example:

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

Fetching active filters:

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

How can I filter products in $productsCollection using $_filters ?

Thank you for your attention!

Magento applies filters to a collection in function Mage_Catalog_Model_Resource_Layer_Filter_Attribute::applyFilterToCollection . Following its logic you could use this code:

$_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()
        );

}

The error you get when selecting a category is that the filter object is of type Mage_Catalog_Model_Layer_Filter_Category and has no property attribute_model . You must add a check to the filter loop to prevent this:

foreach($_filters as $filterItem)
{
    $filter = $filterItem->getFilter();
    if (!($filter instanceof Mage_Catalog_Model_Layer_Filter_Attribute)) {
        continue;
    }

The problem with multiple select filter values can also be solved two ways. If you want the filter values applied in a OR fashion, do this:

   $connection->quoteInto("{$tableAlias}.value = in(?)", explode('_', Mage::app()->getRequest()->getParam($attribute->getAttributeCode())))

If the filter values must be applied in an AND fashion, the procedure is a bit more complex. Basically you must perform multiple joins to the same table and have to provide a unique table alias for each join:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM