/** * @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.'); } }
/** * Make collection not to load products that are in specified quote * * @param \Magento\Catalog\Model\Resource\Product\Collection $collection * @param int $quoteId * @return $this */ public function addExcludeProductFilter($collection, $quoteId) { $adapter = $this->_getReadAdapter(); $exclusionSelect = $adapter->select()->from($this->getTable('sales_flat_quote_item'), array('product_id'))->where('quote_id = ?', $quoteId); $condition = $adapter->prepareSqlCondition('e.entity_id', array('nin' => $exclusionSelect)); $collection->getSelect()->where($condition); return $this; }
/** * Separate query for product and order data * * @param array $productIds * @return array * @throws \Magento\Framework\Exception\LocalizedException */ protected function getProductData(array $productIds) { $productConnection = $this->productResource->getConnection('read'); $productAttrName = $this->productResource->getAttribute('name'); $productAttrNameId = (int) $productAttrName->getAttributeId(); $productAttrPrice = $this->productResource->getAttribute('price'); $productAttrPriceId = (int) $productAttrPrice->getAttributeId(); $select = clone $this->productResource->getSelect(); $select->reset(); $select->from(['main_table' => $this->getTable('catalog_product_entity')])->useStraightJoin(true)->joinInner(['product_name' => $productAttrName->getBackend()->getTable()], 'product_name.entity_id = main_table.entity_id' . ' AND product_name.attribute_id = ' . $productAttrNameId . ' AND product_name.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID, ['name' => 'product_name.value'])->joinInner(['product_price' => $productAttrPrice->getBackend()->getTable()], "product_price.entity_id = main_table.entity_id AND product_price.attribute_id = {$productAttrPriceId}", ['price' => new \Zend_Db_Expr('product_price.value')])->where('main_table.entity_id IN (?)', $productIds); $productData = $productConnection->fetchAssoc($select); return $productData; }
/** * Add only is in stock products filter to product collection * * @param \Magento\Catalog\Model\Resource\Product\Collection $collection * @return $this */ public function addIsInStockFilterToCollection($collection) { $websiteId = $this->_storeManager->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 = ?', Stock::DEFAULT_STOCK_ID); $collection->getSelect()->join(array('stock_status_index' => $this->getMainTable()), $joinCondition, array())->where('stock_status_index.stock_status=?', Stock\Status::STATUS_IN_STOCK); return $this; }
/** * Add low stock filter to product collection * * @param \Magento\Catalog\Model\Resource\Product\Collection $collection * @param array $fields * @return $this */ public function addLowStockFilter(\Magento\Catalog\Model\Resource\Product\Collection $collection, $fields) { $this->_initConfig(); $adapter = $collection->getSelect()->getAdapter(); $qtyIf = $adapter->getCheckSql('invtr.use_config_notify_stock_qty > 0', $this->_configNotifyStockQty, 'invtr.notify_stock_qty'); $conditions = [[$adapter->prepareSqlCondition('invtr.use_config_manage_stock', 1), $adapter->prepareSqlCondition($this->_isConfigManageStock, 1), $adapter->prepareSqlCondition('invtr.qty', ['lt' => $qtyIf])], [$adapter->prepareSqlCondition('invtr.use_config_manage_stock', 0), $adapter->prepareSqlCondition('invtr.manage_stock', 1)]]; $where = []; foreach ($conditions as $k => $part) { $where[$k] = join(' ' . \Zend_Db_Select::SQL_AND . ' ', $part); } $where = $adapter->prepareSqlCondition('invtr.low_stock_date', ['notnull' => true]) . ' ' . \Zend_Db_Select::SQL_AND . ' ((' . join(') ' . \Zend_Db_Select::SQL_OR . ' (', $where) . '))'; $collection->joinTable(['invtr' => 'cataloginventory_stock_item'], 'product_id = entity_id', $fields, $where); return $this; }
/** * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute * @param \Magento\Catalog\Model\Resource\Product\Collection $collection * @return $this */ protected function addGlobalAttribute(\Magento\Catalog\Model\Resource\Eav\Attribute $attribute, \Magento\Catalog\Model\Resource\Product\Collection $collection) { $storeId = $this->storeManager->getStore()->getId(); switch ($attribute->getBackendType()) { case 'decimal': case 'datetime': case 'int': $alias = 'at_' . $attribute->getAttributeCode(); $collection->addAttributeToSelect($attribute->getAttributeCode(), 'inner'); break; default: $alias = 'at_' . md5($this->getId()) . $attribute->getAttributeCode(); $collection->getSelect()->join([$alias => $collection->getTable('catalog_product_index_eav')], "({$alias}.entity_id = e.entity_id) AND ({$alias}.store_id = {$storeId})" . " AND ({$alias}.attribute_id = {$attribute->getId()})", []); } $this->joinedAttributes[$attribute->getAttributeCode()] = $alias; return $this; }