/** * Modifies product collection select sql to include only those products which conforms this filter's conditions * @param Mana_Filters_Model_Filter_Attribute $filter * @param string $value * This method is overridden by copying (method body was pasted from parent class and modified as needed). All * changes are marked with comments. * @see Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Attribute::applyFilterToCollection() */ public function applyFilterToCollection($filter, $value) { $collection = $filter->getLayer()->getProductCollection(); // MANA BEGIN: prevent product to appear twice if it conforms joined codition 2 times (e.g. if product // has two values assigned for an attribute and both are filtered). $collection->getSelect()->distinct(true); // MANA END $attribute = $filter->getAttributeModel(); $connection = $this->_getReadAdapter(); switch ($filter->getFilterOptions()->getOperation()) { case '': $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()), "{$tableAlias}.value in (" . implode(',', array_filter(explode('_', $value))) . ")"); $collection->getSelect()->distinct()->join(array($tableAlias => $this->getMainTable()), join(' AND ', $conditions), array()); break; case 'and': foreach (explode('_', $value) as $i => $singleValue) { $tableAlias = $attribute->getAttributeCode() . '_idx' . $i; $conditions = array("{$tableAlias}.entity_id = e.entity_id", $connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()), $connection->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()), "{$tableAlias}.value = {$singleValue}"); $collection->getSelect()->distinct()->join(array($tableAlias => $this->getMainTable()), join(' AND ', $conditions), array()); } break; default: throw new Exception('Not implemented'); } return $this; }
/** * @param Mana_Filters_Model_Filter_Attribute $filter * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection * @return Varien_Db_Select */ public function countItems($filter, $collection) { $select = $collection->getSelect(); $select->reset(Zend_Db_Select::COLUMNS); $select->reset(Zend_Db_Select::ORDER); $select->reset(Zend_Db_Select::GROUP); $select->reset(Zend_Db_Select::LIMIT_COUNT); $select->reset(Zend_Db_Select::LIMIT_OFFSET); $db = $this->_getReadAdapter(); $attribute = $filter->getAttributeModel(); $selectedOptionIds = $filter->getMSelectedValues(); $isSelectedExpr = count($selectedOptionIds) ? "`eav`.`value` IN (" . implode(', ', $selectedOptionIds) . ")" : "NULL"; $fields = array('sort_order' => new Zend_Db_Expr("`o`.`sort_order`"), 'value' => new Zend_Db_Expr("`eav`.`value`"), 'label' => new Zend_Db_Expr("COALESCE(`vs`.`value`, `vg`.`value`)"), 'm_selected' => new Zend_Db_Expr($isSelectedExpr), 'm_show_selected' => new Zend_Db_Expr($filter->getFilterOptions()->getIsReverse() ? "NOT ({$isSelectedExpr})" : $isSelectedExpr)); $select->joinInner(array('eav' => $this->getTable('catalog/product_index_eav')), "`eav`.`entity_id` = `e`.`entity_id` AND\r\n {$db->quoteInto("`eav`.`attribute_id` = ?", $attribute->getAttributeId())} AND\r\n {$db->quoteInto("`eav`.`store_id` = ?", $filter->getStoreId())}", array('count' => "COUNT(DISTINCT `eav`.`entity_id`)"))->joinInner(array('o' => $this->getTable('eav/attribute_option')), "`o`.`option_id` = `eav`.`value`", null)->joinInner(array('vg' => $this->getTable('eav/attribute_option_value')), $db->quoteInto("`vg`.`option_id` = `eav`.`value` AND `vg`.`store_id` = ?", 0), null)->joinLeft(array('vs' => $this->getTable('eav/attribute_option_value')), $db->quoteInto("`vs`.`option_id` = `eav`.`value` AND `vs`.`store_id` = ?", $filter->getStoreId()), null)->columns($fields)->group($fields); //$sql = $select->__toString(); return $select; }