Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory;
12 
20 class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
21 {
27  protected $_selectionTable;
28 
32  private $itemPrototype = null;
33 
37  private $catalogRuleProcessor = null;
38 
44  private $websiteScopePriceJoined = false;
45 
49  private $stockItem;
50 
79  public function __construct(
80  \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
81  \Psr\Log\LoggerInterface $logger,
82  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
83  \Magento\Framework\Event\ManagerInterface $eventManager,
84  \Magento\Eav\Model\Config $eavConfig,
86  \Magento\Eav\Model\EntityFactory $eavEntityFactory,
87  \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
88  \Magento\Framework\Validator\UniversalFactory $universalFactory,
90  \Magento\Framework\Module\Manager $moduleManager,
91  \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState,
92  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
93  \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
94  \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
95  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
96  \Magento\Customer\Model\Session $customerSession,
97  \Magento\Framework\Stdlib\DateTime $dateTime,
98  \Magento\Customer\Api\GroupManagementInterface $groupManagement,
99  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
100  ProductLimitationFactory $productLimitationFactory = null,
101  \Magento\Framework\EntityManager\MetadataPool $metadataPool = null,
102  \Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer $tableMaintainer = null,
103  \Magento\CatalogInventory\Model\ResourceModel\Stock\Item $stockItem = null
104  ) {
105  parent::__construct(
106  $entityFactory,
107  $logger,
108  $fetchStrategy,
109  $eventManager,
110  $eavConfig,
111  $resource,
112  $eavEntityFactory,
113  $resourceHelper,
114  $universalFactory,
117  $catalogProductFlatState,
118  $scopeConfig,
119  $productOptionFactory,
120  $catalogUrl,
121  $localeDate,
122  $customerSession,
123  $dateTime,
124  $groupManagement,
125  $connection,
126  $productLimitationFactory,
127  $metadataPool,
128  $tableMaintainer
129  );
130 
131  $this->stockItem = $stockItem
132  ?? ObjectManager::getInstance()->get(\Magento\CatalogInventory\Model\ResourceModel\Stock\Item::class);
133  }
134 
140  protected function _construct()
141  {
142  parent::_construct();
143  $this->setRowIdFieldName('selection_id');
144  $this->_selectionTable = $this->getTable('catalog_product_bundle_selection');
145  }
146 
152  public function _afterLoad()
153  {
154  return parent::_afterLoad();
155  }
156 
162  protected function _initSelect()
163  {
164  parent::_initSelect();
165  $this->getSelect()->join(
166  ['selection' => $this->_selectionTable],
167  'selection.product_id = e.entity_id',
168  ['*']
169  );
170  }
171 
178  public function joinPrices($websiteId)
179  {
180  $connection = $this->getConnection();
181  $priceType = $connection->getCheckSql(
182  'price.selection_price_type IS NOT NULL',
183  'price.selection_price_type',
184  'selection.selection_price_type'
185  );
186  $priceValue = $connection->getCheckSql(
187  'price.selection_price_value IS NOT NULL',
188  'price.selection_price_value',
189  'selection.selection_price_value'
190  );
191  $this->getSelect()->joinLeft(
192  ['price' => $this->getTable('catalog_product_bundle_selection_price')],
193  'selection.selection_id = price.selection_id AND price.website_id = ' . (int)$websiteId .
194  ' AND selection.parent_product_id = price.parent_product_id',
195  [
196  'selection_price_type' => $priceType,
197  'selection_price_value' => $priceValue,
198  'parent_product_id' => 'price.parent_product_id',
199  'price_scope' => 'price.website_id'
200  ]
201  );
202  $this->websiteScopePriceJoined = true;
203 
204  return $this;
205  }
206 
214  {
215  if (!empty($optionIds)) {
216  $this->getSelect()->where('selection.option_id IN (?)', $optionIds);
217  }
218  return $this;
219  }
220 
227  public function setSelectionIdsFilter($selectionIds)
228  {
229  if (!empty($selectionIds)) {
230  $this->getSelect()->where('selection.selection_id IN (?)', $selectionIds);
231  }
232  return $this;
233  }
234 
240  public function setPositionOrder()
241  {
242  $this->getSelect()->order('selection.position asc')->order('selection.selection_id asc');
243  return $this;
244  }
245 
252  public function addQuantityFilter()
253  {
254  $manageStockExpr = $this->stockItem->getManageStockExpr('stock_item');
255  $backordersExpr = $this->stockItem->getBackordersExpr('stock_item');
256  $minQtyExpr = $this->getConnection()->getCheckSql(
257  'selection.selection_can_change_qty',
258  $this->stockItem->getMinSaleQtyExpr('stock_item'),
259  'selection.selection_qty'
260  );
261 
262  $where = $manageStockExpr . ' = 0';
263  $where .= ' OR ('
264  . 'stock_item.is_in_stock = ' . \Magento\CatalogInventory\Model\Stock::STOCK_IN_STOCK
265  . ' AND ('
266  . $backordersExpr . ' != ' . \Magento\CatalogInventory\Model\Stock::BACKORDERS_NO
267  . ' OR '
268  . $minQtyExpr . ' <= stock_item.qty'
269  . ')'
270  . ')';
271 
272  $this->getSelect()
273  ->joinInner(
274  ['stock_item' => $this->stockItem->getMainTable()],
275  'selection.product_id = stock_item.product_id',
276  []
277  )->where($where);
278 
279  return $this;
280  }
281 
286  public function getNewEmptyItem()
287  {
288  if (null === $this->itemPrototype) {
289  $this->itemPrototype = parent::getNewEmptyItem();
290  }
291  return clone $this->itemPrototype;
292  }
293 
304  public function addPriceFilter($product, $searchMin, $useRegularPrice = false)
305  {
307  $this->addPriceData();
308  if ($useRegularPrice) {
309  $minimalPriceExpression = self::INDEX_TABLE_ALIAS . '.price';
310  } else {
311  $this->getCatalogRuleProcessor()->addPriceData($this, 'selection.product_id');
312  $minimalPriceExpression = 'LEAST(minimal_price, IFNULL(catalog_rule_price, minimal_price))';
313  }
314  $orderByValue = new \Zend_Db_Expr(
315  '(' .
316  $minimalPriceExpression .
317  ' * selection.selection_qty' .
318  ')'
319  );
320  } else {
321  $connection = $this->getConnection();
322  $priceType = $connection->getIfNullSql(
323  'price.selection_price_type',
324  'selection.selection_price_type'
325  );
326  $priceValue = $connection->getIfNullSql(
327  'price.selection_price_value',
328  'selection.selection_price_value'
329  );
330  if (!$this->websiteScopePriceJoined) {
331  $websiteId = $this->_storeManager->getStore()->getWebsiteId();
332  $this->getSelect()->joinLeft(
333  ['price' => $this->getTable('catalog_product_bundle_selection_price')],
334  'selection.selection_id = price.selection_id AND price.website_id = ' . (int)$websiteId,
335  []
336  );
337  }
338  $price = $connection->getCheckSql(
339  $priceType . ' = 1',
340  (float) $product->getPrice() . ' * '. $priceValue . ' / 100',
341  $priceValue
342  );
343  $orderByValue = new \Zend_Db_Expr('('. $price. ' * '. 'selection.selection_qty)');
344  }
345 
346  $this->getSelect()->reset(Select::ORDER);
347  $this->getSelect()->order(new \Zend_Db_Expr($orderByValue . ($searchMin ? Select::SQL_ASC : Select::SQL_DESC)));
348  $this->getSelect()->limit(1);
349  return $this;
350  }
351 
359  private function getCatalogRuleProcessor()
360  {
361  if (null === $this->catalogRuleProcessor) {
362  $this->catalogRuleProcessor = \Magento\Framework\App\ObjectManager::getInstance()
363  ->get(\Magento\CatalogRule\Model\ResourceModel\Product\CollectionProcessor::class);
364  }
365 
366  return $this->catalogRuleProcessor;
367  }
368 }
const ORDER
Definition: Select.php:54
$storeManager
addPriceData($customerGroupId=null, $websiteId=null)
$resource
Definition: bulk.php:12
$price
$logger
$priceType
Definition: msrp.phtml:18
const SQL_DESC
Definition: Select.php:82
$connection
Definition: bulk.php:13
addPriceFilter($product, $searchMin, $useRegularPrice=false)
Definition: Collection.php:304
__construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Eav\Model\Config $eavConfig, \Magento\Framework\App\ResourceConnection $resource, \Magento\Eav\Model\EntityFactory $eavEntityFactory, \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper, \Magento\Framework\Validator\UniversalFactory $universalFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Module\Manager $moduleManager, \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory, \Magento\Catalog\Model\ResourceModel\Url $catalogUrl, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, ProductLimitationFactory $productLimitationFactory=null, \Magento\Framework\EntityManager\MetadataPool $metadataPool=null, \Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer $tableMaintainer=null, \Magento\CatalogInventory\Model\ResourceModel\Stock\Item $stockItem=null)
Definition: Collection.php:79
const SQL_ASC
Definition: Select.php:81