Filter Products with layered navigation using AND condition
-
16-04-2021 - |
Вопрос
i need to change the layered navigation feature a little bit, currently it filter product attributes on "OR" condition like Product 1 Red, blue, pink
Product 2 Red
Product 3 Blue
On selecting filter options “red” and “blue” in the layered nav, the product collection includes Product 1, Product 2 and Product 3. I just want it to return Product 1. I tried to it by modifying
->where($alias.'.value IN (?)', $ids);
to
foreach ($ids as $key => $value) {
$collection->getSelect()->where($alias.'.value = ?', $value);
}
but with this when i select two option, it doesn't return a value.
Решение
In order to manage to get my task done. I overridden the applyMultipleValuesFilter($ids)
to something like this
protected function applyMultipleValuesFilter($ids)
{
$attribute = $this->getAttributeModel();
$table = Mage::getSingleton('core/resource')->getTableName('catalogindex/eav');
$alias = 'attr_index_'.$attribute->getId();
$hvngquery = ' ';
$i= count($ids)-1;
foreach ($ids as $key => $value)
{
$hvngquery .= '(SUM(`'.$alias.'`.`value`= "'.$value.'") > 0)';
if ($i>0)
{
$hvngquery .= ' AND ';
}
$i = $i-1;
}
$collection = $this->getLayer()->getProductCollection();
$collection->getSelect()->join(
array($alias => $table),
$alias.'.entity_id=e.entity_id',
array()
)
->where($alias.'.store_id = ?', Mage::app()->getStore()->getId())
->where($alias.'.attribute_id = '. $attribute->getId())
->group('e.entity_id')
->having($hvngquery);
return $this;
}
This then generates an unknown field error in query (if you run the query directly in phpmyadmin it works but error occurs only in magento) for this i had to modify _getBaseCollectionSql()
to add having clause to collection