protected function getProductCollection() { $typeFilter = $this->getData('type_filter'); $storeId = Mage::app()->getStore()->getId(); if ($typeFilter == 'bestseller') { $products = Mage::getResourceModel('reports/product_collection')->addOrderedQty()->setStoreId($storeId)->addStoreFilter($storeId)->setOrder('ordered_qty', 'desc'); } else { if ($typeFilter == 'halo_featured') { $products = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('halo_featured', 1); } else { if ($typeFilter == 'most_viewed') { $products = Mage::getResourceModel('reports/product_collection')->addOrderedQty()->setStoreId($storeId)->addStoreFilter($storeId)->addViewsCount(); } else { if ($typeFilter == 'new_products') { $todayStartOfDayDate = Mage::app()->getLocale()->date()->setTime('00:00:00')->toString(Varien_Date::DATETIME_INTERNAL_FORMAT); $todayEndOfDayDate = Mage::app()->getLocale()->date()->setTime('23:59:59')->toString(Varien_Date::DATETIME_INTERNAL_FORMAT); $products = Mage::getResourceModel('catalog/product_collection'); $products->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()); $products->addStoreFilter()->addAttributeToFilter('news_from_date', array('or' => array(0 => array('date' => true, 'to' => $todayEndOfDayDate), 1 => array('is' => new Zend_Db_Expr('null')))), 'left')->addAttributeToFilter('news_to_date', array('or' => array(0 => array('date' => true, 'from' => $todayStartOfDayDate), 1 => array('is' => new Zend_Db_Expr('null')))), 'left')->addAttributeToFilter(array(array('attribute' => 'news_from_date', 'is' => new Zend_Db_Expr('not null')), array('attribute' => 'news_to_date', 'is' => new Zend_Db_Expr('not null'))))->addAttributeToSort('news_from_date', 'desc'); } else { if ($typeFilter == 'sale_off') { $todayStartOfDayDate = Mage::app()->getLocale()->date()->setTime('00:00:00')->toString(Varien_Date::DATETIME_INTERNAL_FORMAT); $todayEndOfDayDate = Mage::app()->getLocale()->date()->setTime('23:59:59')->toString(Varien_Date::DATETIME_INTERNAL_FORMAT); $products = Mage::getResourceModel('catalog/product_collection'); $products->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()); $products->addStoreFilter()->addAttributeToFilter('special_price', array('gt' => 0))->addAttributeToFilter('special_from_date', array('or' => array(0 => array('date' => true, 'to' => $todayEndOfDayDate), 1 => array('is' => new Zend_Db_Expr('null')))), 'left')->addAttributeToFilter('special_to_date', array('or' => array(0 => array('date' => true, 'from' => $todayStartOfDayDate), 1 => array('is' => new Zend_Db_Expr('null')))), 'left')->addAttributeToFilter(array(array('attribute' => 'special_from_date', 'is' => new Zend_Db_Expr('not null')), array('attribute' => 'special_to_date', 'is' => new Zend_Db_Expr('not null')))); } else { $products = Mage::getModel('catalog/product')->getCollection(); } } } } } if ($category = $this->getData('category')) { $products->addCategoryFilter(Mage::getModel('catalog/category')->load(str_replace('category/', '', $category))); } $this->_addProductAttributesAndPrices($products); Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products); /* For category page */ if (Mage::registry('current_category')) { $products->addUrlRewrite(Mage::registry('current_category')->getId()); } $pageSize = $this->getData('limit_count'); if (isset($pageSize)) { $products->setPageSize($pageSize); } $products->setCurPage($this->getRequest()->getParam('p', 1)); $this->setCollection($products); $this->setSize(count($this->_productCollection->getData())); return $this->_productCollection; }
protected function getProductCollection() { $products = Mage::getModel('catalog/product')->getCollection(); $config1 = $this->getData('category'); if ($config1) { $cat = Mage::getModel('catalog/category')->load(str_replace('category/', '', $config1)); $products->addCategoryFilter($cat); } $attrSetName = $this->getData('attribute_set'); if ($attrSetName) { $attributeSetId = Mage::getModel('eav/entity_attribute_set')->load($attrSetName, 'attribute_set_name')->getAttributeSetId(); $products->addAttributeToFilter('attribute_set_id', $attributeSetId); } $products->addAttributeToSelect('*'); $products->addMinimalPrice()->addFinalPrice()->addTaxPercents(); Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products); $asc = $this->getRequest()->getParam('dir'); $od = $this->getRequest()->getParam('order'); if (isset($od) && isset($asc)) { $orders[0] = $od; $orders[1] = $asc; } else { $config2 = $this->getData('order_by'); if (isset($config2)) { //echo 'bb'; $orders = explode(' ', $config2); } } $store_id = Mage::app()->getStore()->getId(); $field_condition = $this->getData('if'); $conditionIf = array(); if ($field_condition != '' && $field_condition != '#') { $field_condition = strtolower($field_condition); $field_condition = $this->standarStr($field_condition); $array_field = $this->toArrExpression($field_condition); $field_widget = '('; foreach ($array_field as $a) { $tmp = array(); $i = 0; //check and process if exists attributes like a,ab,abc foreach ($array_field as $a1) { if ($a != $a1) { if (strstr($a1->field, $a->field)) { $field_condition = str_replace($a1->field, $this->encrypt . $i, $field_condition); $tmp[] = $a1->field; $i++; } } } $field_condition = str_replace($a->field, $a->field . '_table.value', $field_condition); foreach ($tmp as $key => $t) { $field_condition = str_replace($this->encrypt . $key, $t, $field_condition); } $field_widget .= '"' . $a->field . '",'; } $field_widget = substr($field_widget, 0, strlen($field_widget) - 1) . ')'; $dem = 0; $products->distinct(true); $tableEntity = '`e`'; //Join if use catalog flat if (Mage::helper('catalog/product_flat')->isEnabled()) { $products->getSelect()->join(array('entity_table' => Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')), 'e.entity_id=entity_table.entity_id', array()); $tableEntity = 'entity_table'; } foreach ($array_field as $a) { $field = $a->field; $eavAttribute = Mage::getResourceSingleton('catalog/product')->getAttribute($field); //->getData('frontend_input'); $backendType = $eavAttribute->getBackend()->getTable(); $frontendInput = $eavAttribute->getData('frontend_input'); if ($dem == count($array_field) - 1) { if ($frontendInput == 'select') { $products->getSelect()->join(array('cat_table' => $backendType), 'cat_table.entity_id=' . $tableEntity . '.entity_id AND cat_table.store_id in (0,' . $store_id . ') AND cat_table.attribute_id=' . $eavAttribute->getData('attribute_id') . ' AND cat_table.entity_type_id=' . $tableEntity . '.entity_type_id INNER JOIN eav_attribute_option_value as ' . $field . '_table ON cat_table.value=' . $field . '_table.option_id AND (' . $field_condition . ')', array()); } else { $products->getSelect()->join(array($field . '_table' => $backendType), $field . '_table.entity_id=' . $tableEntity . '.entity_id AND ' . $field . '_table.store_id in (0,' . $store_id . ') AND ' . $field . '_table.attribute_id=' . $eavAttribute->getData('attribute_id') . ' AND ' . $field . '_table.entity_type_id=' . $tableEntity . '.entity_type_id and (' . $field_condition . ')', array()); } } else { if ($frontendInput == 'select') { $products->getSelect()->join(array('cat_table' => $backendType), 'cat_table.entity_id=' . $tableEntity . '.entity_id AND cat_table.store_id in in (0,' . $store_id . ') AND cat_table.attribute_id=' . $eavAttribute->getData('attribute_id') . ' AND cat_table.entity_type_id=' . $tableEntity . '.entity_type_id INNER JOIN eav_attribute_option_value as ' . $field . '_table ON cat_table.value=' . $field . '_table.option_id', array()); } else { $products->getSelect()->join(array($field . '_table' => $backendType), $field . '_table.entity_id=' . $tableEntity . '.entity_id AND ' . $field . '_table.store_id in in (0,' . $store_id . ') AND ' . $field . '_table.attribute_id=' . $eavAttribute->getData('attribute_id') . ' AND ' . $field . '_table.entity_type_id=' . $tableEntity . '.entity_type_id', array()); } } $dem++; } } $products->addStoreFilter(Mage::app()->getStore()->getId()); $pageSize = $this->getRequest()->getParam('limit'); if (!$pageSize) { $pageSize = $this->getData('limit_count'); } if (count($orders)) { $products->addAttributeToSort($orders[0], $orders[1]); } if (isset($pageSize)) { $products->setPageSize($pageSize); } $products->setCurPage($this->getRequest()->getParam('p', 1)); $this->setCollection($products); $this->setSize(count($this->_productCollection->getData())); return $this->_productCollection; }