Example #1
0
 /**
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * @param Morphes_Filters_Model_Filter_Price $model
  * @param array $value
  * @return Morphes_Filters_Resource_Filter_Price
  */
 public function applyToCollection($collection, $model, $value)
 {
     $collection->addPriceData($model->getCustomerGroupId(), $model->getWebsiteId());
     $select = $collection->getSelect();
     $response = $this->_dispatchPreparePriceEvent($model, $select);
     $table = $this->_getIndexTableAlias();
     $additional = join('', $response->getAdditionalCalculations());
     $fix = $this->_getConfigurablePriceFix();
     $rate = $model->getCurrencyRate();
     $precision = 2;
     //$filter->getDecimalDigits();
     if ($this->isUpperBoundInclusive()) {
         $priceExpr = new Zend_Db_Expr("ROUND(({$table}.min_price {$additional} {$fix}) * {$rate}, {$precision})");
     } else {
         $priceExpr = new Zend_Db_Expr("({$table}.min_price {$additional} {$fix}) * {$rate}");
     }
     $condition = '';
     foreach ($model->getMSelectedValues() as $selection) {
         if (strpos($selection, ',') !== false) {
             list($index, $range) = explode(',', $selection);
             $range = $this->getPriceRange($index, $range);
             if ($condition != '') {
                 $condition .= ' OR ';
             }
             $condition .= '((' . $priceExpr . ' >= ' . $range['from'] . ') ' . 'AND (' . $priceExpr . ($this->isUpperBoundInclusive() ? ' <= ' : ' < ') . $range['to'] . '))';
         }
     }
     if ($condition) {
         $select->distinct()->where("NOT ({$condition})");
     }
     return $this;
 }
 public function getFilterCounts($code, $cache = true)
 {
     $currentFilter = $this->_filters[$code];
     /* @var $currentFilterModel Mana_Filters_Interface_Filter */
     $currentFilterModel = $currentFilter['model'];
     if (!$cache || !isset($currentFilter['processedCounts'])) {
         if (!empty($currentFilter['isCounted'])) {
             $currentFilter['processedCounts'] = $currentFilterModel->processCounts($currentFilter['counts']);
         } else {
             $mainSelect = clone $this->_productCollection->getSelect();
             $collection = $this->createProductCollection();
             //$sql = $collection->getSelect()->__toString();
             foreach ($this->_filters as $filter) {
                 /* @var $filterModel Mana_Filters_Interface_Filter */
                 $filterModel = $filter['model'];
                 if ($filter['isApplied'] && $currentFilterModel->isFilterAppliedWhenCounting($filterModel)) {
                     $filterModel->applyToCollection($collection);
                 }
             }
             $counts = $currentFilterModel->countOnCollection($collection);
             $currentFilter['processedCounts'] = $currentFilterModel->processCounts($counts);
             $this->_copyParts($this->_productCollection->getSelect(), $mainSelect);
         }
         if ($cache) {
             $this->_filters[$code] = $currentFilter;
         }
     }
     return $currentFilter['processedCounts'];
 }
 /**
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * @param $categoryId
  */
 protected function _prepareProductCollection($collection, $categoryId)
 {
     $res = $collection->getResource();
     $db = $res->getReadConnection();
     $storeId = Mage::app()->getStore()->getId();
     $subSelect = $db->select()->from(array('subcat_index' => $res->getTable('catalog/category_product_index')), new Zend_Db_Expr("`subcat_index`.`product_id`"))->joinInner(array('subcat' => $res->getTable('catalog/category')), "`subcat`.`entity_id` = `subcat_index`.`category_id`", null)->where("`subcat_index`.`store_id`={$storeId} AND `subcat_index`.`visibility` IN(2, 4) AND `subcat`.`parent_id` = ?", $categoryId);
     $collection->getSelect()->where("`e`.`entity_id` IN ({$subSelect})");
 }
 /**
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * @param Mana_Filters_Model_Filter_Attribute $model
  * @param array $value
  * @return Mana_Filters_Resource_Filter_Attribute
  */
 public function applyToCollection($collection, $model, $value)
 {
     $attribute = $model->getAttributeModel();
     $connection = $this->_getReadAdapter();
     $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($value)) . ")");
     $conditions = join(' AND ', $conditions);
     $collection->getSelect()->distinct()->join(array($tableAlias => $this->getMainTable()), $conditions, array());
     return $this;
 }
 /**
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * @param Mana_Filters_Resource_Filter_Attribute $model
  * @param array $value
  * @return Mana_Filters_Resource_Filter_Attribute
  */
 public function applyToCollection($collection, $model, $value)
 {
     $attribute = $model->getAttributeModel();
     $connection = $this->_getReadAdapter();
     foreach ($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}");
         $conditions = join(' AND ', $conditions);
         $collection->getSelect()->distinct()->join(array($tableAlias => $this->getMainTable()), $conditions, array());
     }
     return $this;
 }
Example #6
0
 /**
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * @param Mana_Filters_Model_Filter_Attribute $model
  * @return Mana_Filters_Resource_Filter_Decimal
  */
 public function countOnCollection($collection, $model)
 {
     $select = $collection->getSelect();
     $select->reset(Zend_Db_Select::COLUMNS);
     $select->reset(Zend_Db_Select::ORDER);
     $select->reset(Zend_Db_Select::LIMIT_COUNT);
     $select->reset(Zend_Db_Select::LIMIT_OFFSET);
     $attributeId = $model->getAttributeModel()->getId();
     $storeId = $collection->getStoreId();
     $select->join(array('decimal_index' => $this->getMainTable()), 'e.entity_id = decimal_index.entity_id' . ' AND ' . $this->_getReadAdapter()->quoteInto('decimal_index.attribute_id = ?', $attributeId) . ' AND ' . $this->_getReadAdapter()->quoteInto('decimal_index.store_id = ?', $storeId), array());
     $adapter = $this->_getReadAdapter();
     $countExpr = new Zend_Db_Expr("COUNT(*)");
     $rangeExpr = new Zend_Db_Expr("FLOOR(decimal_index.value / {$model->getRange()}) + 1");
     $select->columns(array('range' => $rangeExpr, 'count' => $countExpr));
     $select->group('range');
     return $adapter->fetchPairs($select);
 }
Example #7
0
 /**
  * @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;
 }
Example #8
0
 public function getAllOptimizedAttributeFilterCounts()
 {
     if (!$this->_allOptimizedAttributeFilterCounts) {
         $attributeIds = array();
         $firstModel = null;
         /* @var $firstModel Mana_Filters_Model_Filter_Attribute */
         foreach ($this->_filters as $filter) {
             $model = $filter['model'];
             if ($this->isOptimizedAttributeFilter($model)) {
                 if (!$firstModel) {
                     $firstModel = $model;
                 }
                 /* @var $model Mana_Filters_Model_Filter_Attribute */
                 $id = $model->getAttributeModel()->getId();
                 $attributeIds[$id] = $id;
             }
         }
         $mainSelect = clone $this->_productCollection->getSelect();
         $collection = $this->createProductCollection();
         foreach ($this->_filters as $filter) {
             if ($filter['isApplied']) {
                 /* @var $filterModel Mana_Filters_Interface_Filter */
                 $filterModel = $filter['model'];
                 $applyFilter = true;
                 foreach ($this->_filters as $optimizedFilter) {
                     /* @var $model Mana_Filters_Model_Filter_Attribute */
                     $model = $optimizedFilter['model'];
                     if ($this->isOptimizedAttributeFilter($model) && !$model->isFilterAppliedWhenCounting($filterModel)) {
                         $applyFilter = false;
                         break;
                     }
                 }
                 if ($applyFilter) {
                     $filterModel->applyToCollection($collection);
                 }
             }
         }
         $this->_allOptimizedAttributeFilterCounts = $firstModel ? $firstModel->optimizedCountOnCollection($collection, $attributeIds) : array();
         $this->_copyParts($this->_productCollection->getSelect(), $mainSelect);
     }
     return $this->_allOptimizedAttributeFilterCounts;
 }
Example #9
0
 /**
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * @param Morphes_Filters_Model_Filter_Decimal $model
  * @param array $value
  * @return Morphes_Filters_Resource_Filter_Decimal
  */
 public function applyToCollection($collection, $model, $value)
 {
     $attribute = $model->getAttributeModel();
     $connection = $this->_getReadAdapter();
     $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()));
     $condition = '';
     foreach ($value as $selection) {
         if (strpos($selection, ',') !== false) {
             list($index, $range) = explode(',', $selection);
             $range = $this->getRange($index, $range);
             if ($condition != '') {
                 $condition .= ' OR ';
             }
             $condition .= '((' . "{$tableAlias}.value" . ' >= ' . $range['from'] . ') ' . 'AND (' . "{$tableAlias}.value" . ($this->isUpperBoundInclusive() ? ' <= ' : ' < ') . $range['to'] . '))';
         }
     }
     if ($condition) {
         $collection->getSelect()->join(array($tableAlias => $this->getMainTable()), join(' AND ', $conditions), array())->distinct()->where("NOT ({$condition})");
     }
     return $this;
 }
Example #10
0
 protected function _getParentSelect()
 {
     $collection = new Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection();
     return $collection->getSelect();
 }
Example #11
0
 /**
  * Filter product collection
  *
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * @return Mage_Catalog_Model_Layer
  */
 public function prepareProductCollection($collection)
 {
     $rule = $this->getRule();
     if ($rule->getData('show_outofstock') == 2) {
         $collection->joinField('stock_status', 'cataloginventory/stock_status', 'stock_status', 'product_id=entity_id', array('stock_status' => Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK, 'website_id' => Mage::app()->getWebsite()->getWebsiteId()));
     }
     $collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())->addMinimalPrice()->addFinalPrice()->addTaxPercents();
     /*Filter min, max price*/
     $this->currentRate = Mage::app()->getStore()->getCurrentCurrencyRate();
     $max = $this->getMaxPriceFilter();
     $min = $this->getMinPriceFilter();
     if ($min && $max) {
         $collection->getSelect()->where(' final_price >= "' . $min . '" AND final_price <= "' . $max . '" ');
     }
     /*End Filter min, max price*/
     Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
     Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
     //$this->_productCollections = $collection;
     return $this;
 }
Example #12
0
 /**
  * Add WHERE statement for select only not mapped attributes with mapped value(s)
  *
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * @param $attributeSetId
  * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
  */
 public function addFilterOnlyMappedAttributes($collection, $attributeSetId)
 {
     $select = $this->_getMappedAttributes()->reset(Zend_Db_Select::COLUMNS)->columns(array('dummy_field' => new Zend_Db_Expr('1')))->where('main_table.attribute_id = ar.attribute_id')->where('tr.attribute_set_id = ?', $attributeSetId);
     $collection->getSelect()->where(sprintf('NOT EXISTS(%s)', $select->assemble()));
     return $collection;
 }
Example #13
0
 /**
  * prepare sql for attribute compare
  *
  * @param  object $attribute  - attribute class with data
  * @param  array  $joinedAttributes  - attribute which was joined into query
  * @param  Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection - product collection
  * @param  string $operator   - operator for compare
  * @param  string $value       - value for compare
  * @return string $where      - sql query
  */
 public function prepareSqlForAtt($attribute, $joinedAttributes, $collection, $operator, $value)
 {
     $nOperator = array('==', '!{}', '{}', '!()', '()');
     $mOperator = array('=', 'NOT LIKE', 'LIKE', 'NOT IN', 'IN');
     //category operator
     $cnOperator = array('==', '!=', '<=', '>=', '=', '>', '<', '!{}', '{}', '!()', '()', 'NOT LIKE', 'LIKE');
     $cmOperator = array('IN', 'NOT IN', 'NOT IN', 'NOT IN', 'IN', 'NOT IN', 'NO IN', 'NOT IN', 'IN', 'NOT IN', 'IN', 'NOT IN', 'IN');
     $operator = str_replace($nOperator, $mOperator, $operator);
     if ($attribute == 'category_ids') {
         $operator = str_replace($cnOperator, $cmOperator, $operator);
     }
     if (($operator == 'LIKE' || $operator == 'NOT LIKE') && is_array($value)) {
         if ($operator == 'LIKE') {
             $operator = 'FIND_IN_SET';
         } else {
             $operator = '!FIND_IN_SET';
         }
     }
     /* Quote rule value depending operator type */
     switch ($operator) {
         case 'LIKE':
         case 'NOT LIKE':
             $value = $collection->getConnection()->quote("%{$value}%");
             break;
         case 'IN':
         case 'NOT IN':
             if (!is_array($value)) {
                 $value = array_filter(array_map('trim', explode(',', $value)));
             }
             $value = '(' . $collection->getConnection()->quote($value) . ')';
             break;
         case 'FIND_IN_SET':
         case '!FIND_IN_SET':
             $_resultSql = '(' . $collection->getConnection()->quote($value[0]) . ', att_table_' . $attribute . '.' . 'value)';
             unset($value[0]);
             foreach ($value as $_conditionValue) {
                 $_resultSql .= ' OR ' . $operator . '(' . $collection->getConnection()->quote($_conditionValue) . ', att_table_' . $attribute . '.' . 'value)';
             }
             $value = $_resultSql;
             break;
         default:
             if (is_array($value)) {
                 $value = implode(',', $value);
             }
             $value = $collection->getConnection()->quote($value);
     }
     if (!in_array($attribute, $this->_joinedAttributes)) {
         array_push($this->_joinedAttributes, $attribute);
         $att = Mage::getModel('catalog/product')->getResource()->getAttribute($attribute);
         if (!$att && $attribute != 'category_ids') {
             return null;
         }
         switch ($attribute) {
             case 'sku':
                 $collection->getSelect()->join(array('att_table_' . $attribute => $att->getBackend()->getTable()), 'att_table_' . $attribute . '.entity_id = e.entity_id', array('sku'));
                 break;
             case 'category_ids':
                 $collection->getSelect()->joinLeft(array('att_table_' . $attribute => $collection->getTable('catalog/category_product')), 'att_table_' . $attribute . '.product_id = e.entity_id', array('category_id'));
                 break;
             case 'attribute_set_id':
                 $collection->getSelect()->joinLeft(array('att_table_' . $attribute => $att->getBackend()->getTable()), 'att_table_' . $attribute . '.entity_id = e.entity_id', array('attribute_set_id'));
                 break;
             case 'price':
                 $collection->addPriceData();
                 break;
             default:
                 $collection->getSelect()->joinLeft(array('att_table_' . $attribute => $att->getBackend()->getTable()), 'att_table_' . $attribute . '.entity_id = e.entity_id AND att_table_' . $attribute . '.attribute_id = ' . $att->getId(), array('value'));
         }
     }
     switch ($attribute) {
         case 'sku':
             $where = '(att_table_' . $attribute . '.' . 'sku' . ' ' . $operator . ' ' . $value . ')';
             break;
         case 'category_ids':
             $where = '(IFNULL(att_table_' . $attribute . '.' . 'category_id,\'\')' . ' ' . $operator . ' ' . $value . ' AND e.entity_id ' . $operator . '(SELECT `product_id` FROM `' . $collection->getTable('catalog/category_product') . '` WHERE `category_id` IN' . $value . ')' . '    )';
             break;
         case 'price':
             $where = '(price_index.final_price ' . $operator . ' ' . $value . ')' . ' AND (price_index.min_price ' . $operator . ' ' . $value . ')';
             break;
         case 'attribute_set_id':
             $where = '(IFNULL(att_table_' . $attribute . '.' . 'attribute_set_id,\'\')' . ' ' . $operator . ' ' . $value . ')';
             break;
         default:
             $where = '(IFNULL(att_table_' . $attribute . '.' . 'value,\'\')' . ' ' . $operator . ' ' . $value . ')';
             if ($operator == 'FIND_IN_SET' || $operator == '!FIND_IN_SET') {
                 $where = '(' . $operator . ' ' . $value . ')';
             }
     }
     return $where;
 }
Example #14
0
 /**
  * Add price index filter
  * 
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * 
  * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
  */
 public function addPriceIndexFilter($collection)
 {
     $select = $collection->getSelect();
     $fromPart = $select->getPart(Zend_Db_Select::FROM);
     if (isset($fromPart['price_index'])) {
         $joinCond = $fromPart['price_index']['joinCondition'];
         if (strpos($joinCond, 'price_index.store_id') === false) {
             $helper = $this->getStorePricingHelper();
             $connection = $collection->getConnection();
             if (!$collection->getFlag('store_id')) {
                 $storeId = $helper->getCurrentStoreId();
             } else {
                 $storeId = $collection->getFlag('store_id');
             }
             $storeId = $connection->quote($storeId);
             if ($storeId) {
                 $joinCond .= " AND (price_index.store_id = {$storeId})";
             } else {
                 $joinCond .= " AND (price_index.store_id = 0)";
             }
             $fromPart['price_index']['joinCondition'] = $joinCond;
             $select->setPart(Zend_Db_Select::FROM, $fromPart);
         }
     }
     return $collection;
 }
Example #15
0
 /**
  * prepare sql for attribute compare
  *
  * @param  object $attribute  - attribute class with data
  * @param  array  $joinedAttributes  - attribute which was joined into query
  * @param  Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection - product collection
  * @param  string $operator   - operator for compare
  * @param  string $value       - value for compare
  * @return string $where      - sql query
  */
 public function prepareSqlForAtt($attribute, $joinedAttributes, $collection, $operator, $value)
 {
     $nOperator = array('==', '!{}', '{}', '!()', '()');
     $mOperator = array('=', 'NOT LIKE', 'LIKE', 'NOT IN', 'IN');
     //category operator
     //$cnOperator = array('==', '!=', '=', '>=', '<=', '>', '<', '!{}', '{}', '!()', '()', 'NOT LIKE', 'LIKE');
     $cnOperator = array('==', '!=', '<=', '>=', '=', '>', '<', '!{}', '{}', '!()', '()', 'NOT LIKE', 'LIKE');
     //$cmOperator = array('=','!=','!=','!=','!=','!=','NOT IN','IN','NOT IN','IN');
     //$cmOperator = array('IN', 'NOT IN', 'IN', 'NOT IN', 'NOT IN', 'NOT IN', 'NO IN', 'NOT IN', 'IN', 'NOT IN', 'IN', 'NOT IN', 'IN');
     $cmOperator = array('IN', 'NOT IN', 'NOT IN', 'NOT IN', 'IN', 'NOT IN', 'NO IN', 'NOT IN', 'IN', 'NOT IN', 'IN', 'NOT IN', 'IN');
     $operator = $attribute == 'category_ids' ? str_replace($cnOperator, $cmOperator, $operator) : str_replace($nOperator, $mOperator, $operator);
     /* Quote rule value depending operator type */
     switch ($operator) {
         case 'LIKE':
         case 'NOT LIKE':
             $value = $collection->getConnection()->quote("%{$value}%");
             break;
         case 'IN':
         case 'NOT IN':
             $value = '(' . $collection->getConnection()->quote(explode(',', $value)) . ')';
             break;
         default:
             $value = $collection->getConnection()->quote($value);
     }
     /* */
     if (!in_array($attribute, $this->_joinedAttributes)) {
         array_push($this->_joinedAttributes, $attribute);
         //$this->_joinedAttributes = $joinedAttributes;
         $att = Mage::getModel('catalog/product')->getResource()->getAttribute($attribute);
         if (!$att && $attribute != 'category_ids') {
             return null;
         }
         switch ($attribute) {
             case 'sku':
                 $collection->getSelect()->join(array('att_table_' . $attribute => $att->getBackend()->getTable()), 'att_table_' . $attribute . '.entity_id = e.entity_id', array('sku'));
                 break;
             case 'category_ids':
                 $collection->getSelect()->joinLeft(array('att_table_' . $attribute => $collection->getTable('catalog/category_product')), 'att_table_' . $attribute . '.product_id = e.entity_id', array('category_id'));
                 break;
             case 'attribute_set_id':
                 $collection->getSelect()->joinLeft(array('att_table_' . $attribute => $att->getBackend()->getTable()), 'att_table_' . $attribute . '.entity_id = e.entity_id', array('attribute_set_id'));
                 break;
             case 'price':
                 $collection->addPriceData();
                 break;
             default:
                 $collection->getSelect()->joinLeft(array('att_table_' . $attribute => $att->getBackend()->getTable()), 'att_table_' . $attribute . '.entity_id = e.entity_id AND att_table_' . $attribute . '.attribute_id = ' . $att->getId(), array('value'));
         }
     }
     switch ($attribute) {
         case 'sku':
             $where = '(att_table_' . $attribute . '.' . 'sku' . ' ' . $operator . ' ' . $value . ')';
             break;
         case 'category_ids':
             $where = '(IFNULL(att_table_' . $attribute . '.' . 'category_id,\'\')' . ' ' . $operator . ' ' . $value . ' AND e.entity_id ' . $operator . '(SELECT `product_id` FROM `' . $collection->getTable('catalog/category_product') . '` WHERE `category_id` IN' . $value . ')' . '    )';
             break;
         case 'price':
             $where = $collection->getConnection()->quoteInto('(price_index.final_price ' . $operator . '?)', $value);
             break;
         case 'attribute_set_id':
             $where = '(IFNULL(att_table_' . $attribute . '.' . 'attribute_set_id,\'\')' . ' ' . $operator . ' ' . $value . ')';
             break;
         default:
             $where = '(IFNULL(att_table_' . $attribute . '.' . 'value,\'\')' . ' ' . $operator . ' ' . $value . ')';
     }
     return $where;
 }
Example #16
0
 /**
  * Add only is in stock products filter to product collection
  *
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * @return Mage_CatalogInventory_Model_Stock_Status
  */
 public function addIsInStockFilterToCollection($collection)
 {
     $websiteId = Mage::app()->getStore($collection->getStoreId())->getWebsiteId();
     $collection->getSelect()->join(array('stock_status_index' => $this->getMainTable()), 'e.entity_id = stock_status_index.product_id AND stock_status_index.website_id = ' . $websiteId . ' AND stock_status_index.stock_id = ' . Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID, array())->where('stock_status_index.stock_status=?', Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK);
     return $this;
 }
Example #17
0
 /**
  * Make collection not to load products that are in specified quote
  *
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * @param int $quoteId
  */
 public function addExcludeProductFilter($collection, $quoteId)
 {
     $collection->getSelect()->where(new Zend_Db_Expr(sprintf('e.entity_id NOT IN (SELECT product_id FROM %s WHERE quote_id=%d)', $this->getTable('sales/quote_item'), $quoteId)));
 }
Example #18
0
 /**
  * Add index select in product collection
  *
  * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  * @return Enterprise_CatalogPermissions_Model_Mysql4_Permission_Index
  */
 public function addIndexToProductCollection($collection, $customerGroupId)
 {
     $parts = $collection->getSelect()->getPart(Zend_Db_Select::FROM);
     $conditions = array();
     if (isset($parts['cat_index']) && $parts['cat_index']['tableName'] == $this->getTable('catalog/category_product_index')) {
         $conditions[] = 'permission_index_product.category_id = cat_index.category_id';
         $conditions[] = 'permission_index_product.product_id = cat_index.product_id';
         $conditions[] = 'permission_index_product.store_id = cat_index.store_id';
     } else {
         $conditions[] = 'permission_index_product.category_id IS NULL';
         $conditions[] = 'permission_index_product.product_id = e.entity_id';
         $conditions[] = 'permission_index_product.store_id=' . intval($collection->getStoreId());
     }
     $conditions[] = 'permission_index_product.customer_group_id=' . intval($customerGroupId);
     $condition = join(' AND ', $conditions);
     if (isset($parts['permission_index_product'])) {
         $parts['permission_index_product']['joinCondition'] = $condition;
         $collection->getSelect()->setPart(Zend_Db_Select::FROM, $parts);
     } else {
         $collection->getSelect()->joinLeft(array('permission_index_product' => $this->getTable('permission_index_product')), $condition, array('grant_catalog_category_view', 'grant_catalog_product_price', 'grant_checkout_items'));
         if (!Mage::helper('enterprise_catalogpermissions')->isAllowedCategoryView()) {
             $collection->getSelect()->where('permission_index_product.grant_catalog_category_view = ' . Enterprise_CatalogPermissions_Model_Permission::PERMISSION_ALLOW);
         } else {
             $collection->getSelect()->where('permission_index_product.grant_catalog_category_view != ' . Enterprise_CatalogPermissions_Model_Permission::PERMISSION_DENY . '
                          OR permission_index_product.grant_catalog_category_view IS NULL');
         }
         /*
          * Checking if passed collection has link model attached
          */
         if (method_exists($collection, 'getLinkModel')) {
             $linkTypeId = $collection->getLinkModel()->getLinkTypeId();
             $linkTypeIds = array(Mage_Catalog_Model_Product_Link::LINK_TYPE_CROSSSELL, Mage_Catalog_Model_Product_Link::LINK_TYPE_UPSELL);
             /*
              * If collection has appropriate link type (cross-sell or up-sell) we need to
              * limit products by permissions (display price and add to cart)
              */
             if (in_array($linkTypeId, $linkTypeIds)) {
                 if (!Mage::helper('enterprise_catalogpermissions')->isAllowedProductPrice()) {
                     $collection->getSelect()->where('permission_index_product.grant_catalog_product_price = ' . Enterprise_CatalogPermissions_Model_Permission::PERMISSION_ALLOW);
                 } else {
                     $collection->getSelect()->where('permission_index_product.grant_catalog_product_price != ' . Enterprise_CatalogPermissions_Model_Permission::PERMISSION_DENY . '
                                  OR permission_index_product.grant_catalog_product_price IS NULL');
                 }
                 if (!Mage::helper('enterprise_catalogpermissions')->isAllowedCheckoutItems()) {
                     $collection->getSelect()->where('permission_index_product.grant_checkout_items = ' . Enterprise_CatalogPermissions_Model_Permission::PERMISSION_ALLOW);
                 } else {
                     $collection->getSelect()->where('permission_index_product.grant_checkout_items != ' . Enterprise_CatalogPermissions_Model_Permission::PERMISSION_DENY . '
                                  OR permission_index_product.grant_checkout_items IS NULL');
                 }
             }
         }
     }
     return $this;
 }