/** * Callback method for applying price filter. * @param Mage_Catalog_Model_Resource_Product_Collection $_collection * @param Mage_Adminhtml_Block_Widget_Grid_Column $_column */ public function _filterPrice($_collection, $_column) { $_field = $_column->getFilterIndex() ? $_column->getFilterIndex() : $_column->getIndex(); $_condition = $_column->getFilter()->getCondition(); if (!$_field || !is_array($_condition)) { return; } if (!array_key_exists('type', $_condition) || !is_numeric($_condition['type'])) { $_collection->addFieldToFilter($_field, $_condition); } else { $_storeId = (int) $this->getRequest()->getParam('store', 0); $_store = Mage::app()->getStore($_storeId); $_joinCondition = array('`e`.`entity_id` = `at_reservation_price`.`entity_id`', '`at_reservation_price`.`store_id` = ' . $_store->getId(), '`at_reservation_price`.`ptype` = ' . $_condition['type']); if (array_key_exists('from', $_condition)) { $_joinCondition[] = '`at_reservation_price`.`price` >= ' . $_condition['from']; } if (array_key_exists('to', $_condition)) { $_joinCondition[] = '`at_reservation_price`.`price` <= ' . $_condition['to']; } $_joinCondition[] = '`at_reservation_price`.`date_from` = \'0000-00-00 00:00:00\' OR DATE(`at_reservation_price`.`date_from`) <= DATE(\'' . date('Y-m-d H:i:s', Mage::getModel('core/date')->gmtTimestamp(time())) . '\')'; $_joinCondition[] = '`at_reservation_price`.`date_to` = \'0000-00-00 00:00:00\' OR DATE(`at_reservation_price`.`date_to`) >= DATE(\'' . date('Y-m-d H:i:s', Mage::getModel('core/date')->gmtTimestamp(time())) . '\')'; $_collection->getSelect()->joinInner(array('at_reservation_price' => $_collection->getTable('payperrentals/reservationprices')), '(' . implode(') AND (', $_joinCondition) . ')', array('price_type' => 'at_reservation_price.ptype', 'reservation_price' => 'at_reservation_price.price', 'reservation_number' => 'at_reservation_price.numberof')); /** TODO Check collection count calculation with group. I think need change join for use distinct */ $_collection->getSelect()->group('e.entity_id'); } }
/** * @dataProvider setOrderDataProvider */ public function testSetOrder($order, $expectedOrder) { $this->_collection->setOrder($order); $this->_collection->load(); // perform real SQL query $selectOrder = $this->_collection->getSelect()->getPart(Zend_Db_Select::ORDER); foreach ($expectedOrder as $field) { $orderBy = array_shift($selectOrder); $this->assertArrayHasKey(0, $orderBy); $this->assertTrue(false !== strpos($orderBy[0], $field), 'Ordering by same column more than once is restricted by multiple RDBMS requirements.'); } }
/** * @param Mage_Catalog_Model_Resource_Product_Collection $collection * @param string $currDir * * @return $this */ public function apply($collection, $currDir) { if (!$this->isEnabled()) { return $this; } $alias = 'price_index'; if (preg_match('/`([a-z0-9\\_]+)`\\.`final_price`/', $collection->getSelect()->__toString(), $m)) { $alias = $m[1]; } $price = Mage::getStoreConfig('amsorting/general/profit_price'); $attribute = Mage::getStoreConfig('amsorting/general/product_attribute'); $collection->joinAttribute('cost', Mage_Catalog_Model_Product::ENTITY . '/' . $attribute, 'entity_id', null, 'left', Mage::app()->getStore()->getId()); $collection->getSelect()->columns(array('profit' => "(`{$alias}`.`{$price}` - IF(`at_cost`.`value` IS NULL, 0, `at_cost`.`value`))")); $collection->getSelect()->order("profit {$currDir}"); return $this; }
/** * Add only is in stock products filter to product collection * * @param Mage_Catalog_Model_Resource_Product_Collection $collection * * @return Mage_CatalogInventory_Model_Resource_Stock_Status */ public function addIsInStockFilterToCollection($collection) { $websiteId = Mage::app()->getStore($collection->getStoreId())->getWebsiteId(); $joinCondition = $this->_getReadAdapter()->quoteInto('e.entity_id = status_table_mli.product_id' . ' AND status_table_mli.store_id = ?', Mage::app()->getStore()->getId()); $collection->getSelect()->join(array('status_table_mli' => $this->getTable('demac_multilocationinventory/stock_status_index')), $joinCondition, array())->where('status_table_mli.is_in_stock=1'); return $this; }
/** * @param Mage_Catalog_Model_Resource_Product_Collection $productCollection * @return mixed */ public function sort(Mage_Catalog_Model_Resource_Product_Collection $productCollection) { //Pass a small validation if (!$this->_getHelper()->isCategorySortingEnable() || $productCollection->getFlag(self::MARKETO_SORTED)) { return $productCollection; } $categoriesList = $this->_getPesonalizeCalculator()->getScoreCategoryParams(HooshMarketing_Marketo_Model_Personalize_Calculator::CATEGORY_ID_AND_SCORE); //get List of categories with key -> Category_id and value - Score if (!count($categoriesList)) { //if we havn`t score categories do nothing return $productCollection; } uasort($categoriesList, function ($f, $s) { return $f > $s ? 1 : -1; //sort by descending }); //Sort categories in order to show top scored categories first $categoriesList = array_keys($categoriesList); // get only category ids try { $productCategoryTable = $this->_getCoreResource()->getTableName("catalog_category_product"); $productCollection->getSelect()->joinLeft(array("marketo_category_table" => $productCategoryTable), "marketo_category_table.product_id = e.entity_id", array("top_category_id" => "marketo_category_table.category_id"))->group("entity_id")->order("FIELD(top_category_id, " . implode(',', $categoriesList) . ") DESC"); //order by top $productCollection->setFlag(self::MARKETO_SORTED, true); //to set order only one time } catch (Exception $e) { Mage::logException($e); } return $productCollection; }
/** * Make collection not to load products that are in specified quote * * @param Mage_Catalog_Model_Resource_Product_Collection $collection * @param int $quoteId * @return Mage_Checkout_Model_Resource_Cart */ public function addExcludeProductFilter($collection, $quoteId) { $adapter = $this->_getReadAdapter(); $exclusionSelect = $adapter->select()->from($this->getTable('sales/quote_item'), array('product_id'))->where('quote_id = ?', $quoteId); $condition = $adapter->prepareSqlCondition('e.entity_id', array('nin' => $exclusionSelect)); $collection->getSelect()->where($condition); return $this; }
/** * @param Mage_Catalog_Model_Resource_Product_Collection $collection * @param string $currDir * * @return $this */ public function apply($collection, $currDir) { if (!$this->isEnabled()) { return $this; } $sorters = $this->getSorters(); if (Mage::getStoreConfig('amsorting/general/use_index')) { foreach ($sorters as $sorter) { $collection->joinField($sorter->getCode(), $sorter->getIndexTable(), $sorter->getCode(), 'id=entity_id', array('store_id' => Mage::app()->getStore()->getId()), 'left'); } } else { $select = $collection->getSelect(); $col = $select->getPart('columns'); foreach ($sorters as $sorter) { $col[] = array('', $sorter->getColumnSelect(), $sorter->getCode()); } $select->setPart('columns', $col); } $collection->getSelect()->order(new Zend_Db_Expr('(' . $sorters['dividend']->getCode() . '/' . $sorters['divider']->getCode() . ') ' . $currDir)); return $this; }
/** * Get link collection * * @return Mage_Catalog_Model_Resource_Product_Collection|null */ public function getLinkCollection() { if (is_null($this->_linkCollection)) { $this->_linkCollection = $this->_getTargetLinkCollection(); if ($this->_linkCollection) { // Perform rotation mode $select = $this->_linkCollection->getSelect(); $rotationMode = $this->getTargetRuleHelper()->getRotationMode($this->getProductListType()); if ($rotationMode == Enterprise_TargetRule_Model_Rule::ROTATION_SHUFFLE) { Mage::getResourceSingleton('enterprise_targetrule/index')->orderRand($select); } else { $select->order('link_attribute_position_int.value ASC'); } } } return $this->_linkCollection; }
/** * Add low stock filter to product collection * * @param Mage_Catalog_Model_Resource_Product_Collection $collection * @param array $fields * @return Mage_CatalogInventory_Model_Resource_Stock */ public function addLowStockFilter(Mage_Catalog_Model_Resource_Product_Collection $collection, $fields) { $this->_initConfig(); $adapter = $collection->getSelect()->getAdapter(); $qtyIf = $adapter->getCheckSql('invtr.use_config_notify_stock_qty', $this->_configNotifyStockQty, 'invtr.notify_stock_qty'); $conditions = array(array($adapter->prepareSqlCondition('invtr.use_config_manage_stock', 1), $adapter->prepareSqlCondition($this->_isConfigManageStock, 1), $adapter->prepareSqlCondition('invtr.qty', array('lt' => $qtyIf))), array($adapter->prepareSqlCondition('invtr.use_config_manage_stock', 0), $adapter->prepareSqlCondition('invtr.manage_stock', 1))); $where = array(); foreach ($conditions as $k => $part) { $where[$k] = join(' ' . Zend_Db_Select::SQL_AND . ' ', $part); } $where = $adapter->prepareSqlCondition('invtr.low_stock_date', array('notnull' => true)) . ' ' . Zend_Db_Select::SQL_AND . ' ((' . join(') ' . Zend_Db_Select::SQL_OR . ' (', $where) . '))'; $collection->joinTable(array('invtr' => 'cataloginventory/stock_item'), 'product_id = entity_id', $fields, $where); return $this; }
/** * Add only is in stock products filter to product collection * * @param Mage_Catalog_Model_Resource_Product_Collection $collection * @return Mage_CatalogInventory_Model_Resource_Stock_Status */ public function addIsInStockFilterToCollection($collection) { $websiteId = Mage::app()->getStore($collection->getStoreId())->getWebsiteId(); $joinCondition = $this->_getReadAdapter()->quoteInto('e.entity_id = stock_status_index.product_id' . ' AND stock_status_index.website_id = ?', $websiteId); $joinCondition .= $this->_getReadAdapter()->quoteInto(' AND stock_status_index.stock_id = ?', Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID); $collection->getSelect()->join(array('stock_status_index' => $this->getMainTable()), $joinCondition, array())->where('stock_status_index.stock_status=?', Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK); return $this; }
/** * Add index select in product collection * * @param Mage_Catalog_Model_Resource_Product_Collection $collection * @param int $customerGroupId * @return Enterprise_CatalogPermissions_Model_Resource_Permission_Index */ public function addIndexToProductCollection($collection, $customerGroupId) { $parts = $collection->getSelect()->getPart(Zend_Db_Select::FROM); if (isset($parts['perm'])) { return $this; } if ($collection->getFlag('disable_root_category_filter')) { $permColumns = $this->_getPermColumns(); } else { $permColumns = array('grant_catalog_category_view', 'grant_catalog_product_price', 'grant_checkout_items'); } $collection->getSelect()->joinLeft(array('perm' => $this->getTable('permission_index')), 'perm.category_id=cat_index.category_id AND perm.customer_group_id= ' . $customerGroupId . ' AND perm.website_id=' . Mage::app()->getStore()->getWebsiteId(), $permColumns); if (!Mage::helper('enterprise_catalogpermissions')->isAllowedCategoryView()) { $collection->getSelect()->where('perm.grant_catalog_category_view = ?', Enterprise_CatalogPermissions_Model_Permission::PERMISSION_ALLOW); } else { $collection->getSelect()->where('perm.grant_catalog_category_view != ?' . ' OR perm.grant_catalog_category_view IS NULL', Enterprise_CatalogPermissions_Model_Permission::PERMISSION_DENY); } $collection->getSelect()->where('cat_index.store_id=' . $collection->getStoreId()); if ($collection->getFlag('disable_root_category_filter')) { $collection->getSelect()->where('cat_index.is_parent=1'); $collection->getSelect()->group('cat_index.product_id'); } if ($this->_isLinkCollection($collection)) { $collection->getSelect()->where('perm.grant_catalog_product_price!=' . Enterprise_CatalogPermissions_Model_Permission::PERMISSION_DENY . ' OR perm.grant_catalog_product_price IS NULL')->where('perm.grant_checkout_items!=' . Enterprise_CatalogPermissions_Model_Permission::PERMISSION_DENY . ' OR perm.grant_checkout_items IS NULL'); } return $this; }
/** * Add join for catalog in stock field to product collection * * @param Mage_Catalog_Model_Resource_Product_Collection $productCollection * * @return Mage_CatalogInventory_Model_Resource_Stock_Item */ public function addCatalogInventoryToProductCollection($productCollection) { $productCollection->getSelect()->joinLeft(array('cisi' => Mage::getSingleton('core/resource')->getTableName('demac_multilocationinventory/stock_status_index')), 'e.entity_id = cisi.product_id' . ' AND cisi.store_id = ' . Mage::app()->getStore()->getId(), array('is_saleable' => 'is_in_stock', 'inventory_in_stock' => 'is_in_stock')); return $this; }
/** * Add index select in product collection * * @param Mage_Catalog_Model_Resource_Product_Collection $collection * @param int $customerGroupId * @return Enterprise_CatalogPermissions_Model_Resource_Permission_Index */ public function addIndexToProductCollection($collection, $customerGroupId) { $adapter = $this->_getReadAdapter(); $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[] = $adapter->quoteInto('permission_index_product.store_id = ?', $collection->getStoreId()); } $conditions[] = $adapter->quoteInto('permission_index_product.customer_group_id = ?', $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 != ?' . ' OR permission_index_product.grant_catalog_category_view IS NULL', Enterprise_CatalogPermissions_Model_Permission::PERMISSION_DENY); } /* * 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 != ?' . ' OR permission_index_product.grant_catalog_product_price IS NULL', Enterprise_CatalogPermissions_Model_Permission::PERMISSION_DENY); } 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 != ?' . ' OR permission_index_product.grant_checkout_items IS NULL', Enterprise_CatalogPermissions_Model_Permission::PERMISSION_DENY); } } } } return $this; }