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
13 
15 
22 {
28  protected $_inventoryItemJoined = false;
29 
35  protected $_inventoryItemTableAlias = 'lowstock_inventory_item';
36 
40  protected $stockRegistry;
41 
46 
50  protected $_itemResource;
51 
84  public function __construct(
85  \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
86  \Psr\Log\LoggerInterface $logger,
87  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
88  \Magento\Framework\Event\ManagerInterface $eventManager,
89  \Magento\Eav\Model\Config $eavConfig,
90  \Magento\Framework\App\ResourceConnection $resource,
91  \Magento\Eav\Model\EntityFactory $eavEntityFactory,
92  \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
93  \Magento\Framework\Validator\UniversalFactory $universalFactory,
94  \Magento\Store\Model\StoreManagerInterface $storeManager,
95  \Magento\Framework\Module\Manager $moduleManager,
96  \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState,
97  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
98  \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
99  \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
100  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
101  \Magento\Customer\Model\Session $customerSession,
102  \Magento\Framework\Stdlib\DateTime $dateTime,
103  \Magento\Customer\Api\GroupManagementInterface $groupManagement,
104  \Magento\Catalog\Model\ResourceModel\Product $product,
105  \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory,
106  \Magento\Catalog\Model\Product\Type $productType,
107  \Magento\Quote\Model\ResourceModel\Quote\Collection $quoteResource,
108  \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
109  \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration,
110  \Magento\CatalogInventory\Model\ResourceModel\Stock\Item $itemResource,
111  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
112  ) {
113  parent::__construct(
114  $entityFactory,
115  $logger,
116  $fetchStrategy,
117  $eventManager,
118  $eavConfig,
119  $resource,
120  $eavEntityFactory,
121  $resourceHelper,
122  $universalFactory,
125  $catalogProductFlatState,
126  $scopeConfig,
127  $productOptionFactory,
128  $catalogUrl,
129  $localeDate,
130  $customerSession,
131  $dateTime,
132  $groupManagement,
133  $product,
134  $eventTypeFactory,
135  $productType,
138  );
139  $this->stockRegistry = $stockRegistry;
140  $this->stockConfiguration = $stockConfiguration;
141  $this->_itemResource = $itemResource;
142  }
143 
149  protected function _getInventoryItemTable()
150  {
151  return $this->_itemResource->getMainTable();
152  }
153 
159  protected function _getInventoryItemIdField()
160  {
161  return $this->_itemResource->getIdFieldName();
162  }
163 
169  protected function _getInventoryItemTableAlias()
170  {
172  }
173 
181  protected function _addInventoryItemFieldToSelect($field, $alias = null)
182  {
183  if (empty($alias)) {
184  $alias = $field;
185  }
186 
187  if (isset($this->_joinFields[$alias])) {
188  return $this;
189  }
190 
191  $this->_joinFields[$alias] = ['table' => $this->_getInventoryItemTableAlias(), 'field' => $field];
192 
193  $this->getSelect()->columns([$alias => $field], $this->_getInventoryItemTableAlias());
194  return $this;
195  }
196 
203  protected function _getInventoryItemField($field)
204  {
205  return sprintf('%s.%s', $this->_getInventoryItemTableAlias(), $field);
206  }
207 
214  public function joinInventoryItem($fields = [])
215  {
216  if (!$this->_inventoryItemJoined) {
217  $this->getSelect()->join(
219  sprintf(
220  'e.%s = %s.product_id',
221  $this->getEntity()->getEntityIdField(),
223  ),
224  []
225  );
226  $this->_inventoryItemJoined = true;
227  }
228 
229  if (!is_array($fields)) {
230  if (empty($fields)) {
231  $fields = [];
232  } else {
233  $fields = [$fields];
234  }
235  }
236 
237  foreach ($fields as $alias => $field) {
238  if (!is_string($alias)) {
239  $alias = null;
240  }
241  $this->_addInventoryItemFieldToSelect($field, $alias);
242  }
243 
244  return $this;
245  }
246 
254  public function filterByProductType($typeFilter)
255  {
256  if (!is_string($typeFilter) && !is_array($typeFilter)) {
257  throw new LocalizedException(__('The product type filter specified is incorrect.'));
258  }
259  $this->addAttributeToFilter('type_id', $typeFilter);
260  return $this;
261  }
262 
268  public function filterByIsQtyProductTypes()
269  {
270  $this->filterByProductType(array_keys(array_filter($this->stockConfiguration->getIsQtyTypeIds())));
271  return $this;
272  }
273 
280  public function useManageStockFilter($storeId = null)
281  {
282  $this->joinInventoryItem();
283  $manageStockExpr = $this->getConnection()->getCheckSql(
284  $this->_getInventoryItemField('use_config_manage_stock') . ' = 1',
285  (int)$this->stockConfiguration->getManageStock($storeId),
286  $this->_getInventoryItemField('manage_stock')
287  );
288  $this->getSelect()->where($manageStockExpr . ' = ?', 1);
289  return $this;
290  }
291 
298  public function useNotifyStockQtyFilter($storeId = null)
299  {
300  $this->joinInventoryItem(['qty']);
301  $notifyStockExpr = $this->getConnection()->getCheckSql(
302  $this->_getInventoryItemField('use_config_notify_stock_qty') . ' = 1',
303  (int)$this->stockConfiguration->getNotifyStockQty($storeId),
304  $this->_getInventoryItemField('notify_stock_qty')
305  );
306  $this->getSelect()->where($this->_getInventoryItemField('qty') . ' < ?', $notifyStockExpr);
307  return $this;
308  }
309 }
addAttributeToFilter($attribute, $condition=null, $joinType='inner')
__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\Catalog\Model\ResourceModel\Product $product, \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory, \Magento\Catalog\Model\Product\Type $productType, \Magento\Quote\Model\ResourceModel\Quote\Collection $quoteResource, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration, \Magento\CatalogInventory\Model\ResourceModel\Stock\Item $itemResource, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null)
Definition: Collection.php:84
$fields
Definition: details.phtml:14
$storeManager
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$logger
if(!trim($html)) $alias
Definition: details.phtml:20
$connection
Definition: bulk.php:13