Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Item.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\CatalogInventory\Api\StockConfigurationInterface as StockConfigurationInterface;
11 use Magento\CatalogInventory\Api\StockItemRepositoryInterface as StockItemRepositoryInterface;
16 
24 {
28  const ENTITY = 'cataloginventory_stock_item';
29 
35  protected $_eventPrefix = 'cataloginventory_stock_item';
36 
37  const WEBSITE_ID = 'website_id';
38 
46  protected $_eventObject = 'item';
47 
53  protected $storeManager;
54 
59 
63  protected $stockRegistry;
64 
69 
73  protected $qtyIncrements;
74 
80  protected $storeId;
81 
87  protected $customerGroupId;
88 
92  protected $customerSession;
93 
109  public function __construct(
110  \Magento\Framework\Model\Context $context,
111  \Magento\Framework\Registry $registry,
112  ExtensionAttributesFactory $extensionFactory,
114  \Magento\Customer\Model\Session $customerSession,
119  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
120  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
121  array $data = []
122  ) {
123  parent::__construct(
124  $context,
125  $registry,
126  $extensionFactory,
128  $resource,
129  $resourceCollection,
130  $data
131  );
132  $this->customerSession = $customerSession;
133  $this->storeManager = $storeManager;
134  $this->stockConfiguration = $stockConfiguration;
135  $this->stockRegistry = $stockRegistry;
136  $this->stockItemRepository = $stockItemRepository;
137  }
138 
144  protected function _construct()
145  {
146  $this->_init(\Magento\CatalogInventory\Model\ResourceModel\Stock\Item::class);
147  }
148 
152  public function getItemId()
153  {
154  return $this->_getData(static::ITEM_ID);
155  }
156 
162  public function getWebsiteId()
163  {
164  $websiteId = $this->getData(static::WEBSITE_ID);
165  if ($websiteId === null) {
166  $websiteId = $this->stockConfiguration->getDefaultScopeId();
167  }
168  return (int) $websiteId;
169  }
170 
176  public function getStockId()
177  {
178  $stockId = $this->getData(static::STOCK_ID);
179  if ($stockId === null) {
180  $stockId = $this->stockRegistry->getStock($this->getWebsiteId())->getStockId();
181  }
182  return (int) $stockId;
183  }
184 
190  public function getProductId()
191  {
192  return (int) $this->_getData(static::PRODUCT_ID);
193  }
194 
199  public function getStockStatusChangedAuto()
200  {
201  return (bool) $this->_getData(static::STOCK_STATUS_CHANGED_AUTO);
202  }
203 
207  public function getQty()
208  {
209  return null === $this->_getData(static::QTY) ? null : (float)$this->_getData(static::QTY);
210  }
211 
217  public function getIsInStock()
218  {
219  if (!$this->getManageStock()) {
220  return true;
221  }
222  return (bool) $this->_getData(static::IS_IN_STOCK);
223  }
224 
229  public function getIsQtyDecimal()
230  {
231  return (bool) $this->_getData(static::IS_QTY_DECIMAL);
232  }
233 
238  public function getIsDecimalDivided()
239  {
240  return (bool) $this->_getData(static::IS_DECIMAL_DIVIDED);
241  }
242 
246  public function getLowStockDate()
247  {
248  return $this->_getData(static::LOW_STOCK_DATE);
249  }
250 
258  {
259  return false;
260  }
261 
266  public function getUseConfigMinQty()
267  {
268  return (bool) $this->_getData(static::USE_CONFIG_MIN_QTY);
269  }
270 
276  public function getMinQty()
277  {
278  if ($this->getUseConfigMinQty()) {
279  $minQty = $this->stockConfiguration->getMinQty($this->getStoreId());
280  } else {
281  $minQty = (float)$this->getData(static::MIN_QTY);
282  }
283  return $minQty;
284  }
285 
290  public function getUseConfigMinSaleQty()
291  {
292  return (bool) $this->_getData(static::USE_CONFIG_MIN_SALE_QTY);
293  }
294 
300  public function getMinSaleQty()
301  {
302  if ($this->getUseConfigMinSaleQty()) {
304  $minSaleQty = $this->stockConfiguration->getMinSaleQty($this->getStoreId(), $customerGroupId);
305  } else {
306  $minSaleQty = (float) $this->getData(static::MIN_SALE_QTY);
307  }
308  return $minSaleQty;
309  }
310 
315  public function getUseConfigMaxSaleQty()
316  {
317  return (bool) $this->_getData(static::USE_CONFIG_MAX_SALE_QTY);
318  }
319 
325  public function getMaxSaleQty()
326  {
327  if ($this->getUseConfigMaxSaleQty()) {
329  $maxSaleQty = $this->stockConfiguration->getMaxSaleQty($this->getStoreId(), $customerGroupId);
330  } else {
331  $maxSaleQty = (float) $this->getData(static::MAX_SALE_QTY);
332  }
333  return $maxSaleQty;
334  }
335 
340  public function getUseConfigNotifyStockQty()
341  {
342  return (bool) $this->_getData(static::USE_CONFIG_NOTIFY_STOCK_QTY);
343  }
344 
350  public function getNotifyStockQty()
351  {
352  if ($this->getUseConfigNotifyStockQty()) {
353  return $this->stockConfiguration->getNotifyStockQty($this->getStoreId());
354  }
355  return (float) $this->getData(static::NOTIFY_STOCK_QTY);
356  }
357 
362  public function getUseConfigEnableQtyInc()
363  {
364  return (bool) $this->_getData(static::USE_CONFIG_ENABLE_QTY_INC);
365  }
366 
373  public function getEnableQtyIncrements()
374  {
375  if ($this->getUseConfigEnableQtyInc()) {
376  return $this->stockConfiguration->getEnableQtyIncrements($this->getStoreId());
377  }
378  return (bool) $this->getData(static::ENABLE_QTY_INCREMENTS);
379  }
380 
387  public function getUseConfigQtyIncrements()
388  {
389  return (bool) $this->_getData(static::USE_CONFIG_QTY_INCREMENTS);
390  }
391 
397  public function getQtyIncrements()
398  {
399  if ($this->qtyIncrements === null) {
400  if ($this->getEnableQtyIncrements()) {
401  if ($this->getUseConfigQtyIncrements()) {
402  $this->qtyIncrements = $this->stockConfiguration->getQtyIncrements($this->getStoreId());
403  } else {
404  $this->qtyIncrements = $this->getData(static::QTY_INCREMENTS);
405  }
406 
407  if ($this->getIsQtyDecimal()) { // Cast accordingly to decimal qty usage
408  $this->qtyIncrements = (float) $this->qtyIncrements;
409  } else {
410  $this->qtyIncrements = (int) $this->qtyIncrements;
411  }
412  }
413  if ($this->qtyIncrements <= 0) {
414  $this->qtyIncrements = false;
415  }
416  }
417  return $this->qtyIncrements;
418  }
419 
424  public function getUseConfigBackorders()
425  {
426  return (bool) $this->_getData(static::USE_CONFIG_BACKORDERS);
427  }
428 
434  public function getBackorders()
435  {
436  if ($this->getUseConfigBackorders()) {
437  return $this->stockConfiguration->getBackorders($this->getStoreId());
438  }
439  return (int) $this->getData(static::BACKORDERS);
440  }
441 
446  public function getUseConfigManageStock()
447  {
448  return (bool) $this->_getData(static::USE_CONFIG_MANAGE_STOCK);
449  }
450 
456  public function getManageStock()
457  {
458  if ($this->getUseConfigManageStock()) {
459  return $this->stockConfiguration->getManageStock($this->getStoreId());
460  }
461  return (int) $this->getData(static::MANAGE_STOCK);
462  }
463 
473  protected function _addQuoteItemError(
474  \Magento\Quote\Model\Quote\Item $item,
475  $itemError,
476  $quoteError,
477  $errorIndex = 'error'
478  ) {
479  $item->setHasError(true);
480  $item->setMessage($itemError);
481  $item->setQuoteMessage($quoteError);
482  $item->setQuoteMessageIndex($errorIndex);
483  return $this;
484  }
485 
492  public function save()
493  {
494  $this->stockItemRepository->save($this);
495  return $this;
496  }
497 
504  public function setProduct(Product $product)
505  {
506  $this->setProductId($product->getId())
507  ->setStoreId($product->getStoreId())
508  ->setProductTypeId($product->getTypeId())
509  ->setProductName($product->getName())
510  ->setProductStatusChanged($product->dataHasChangedFor('status'))
511  ->setProductChangedWebsites($product->getIsChangedWebsites());
512  return $this;
513  }
514 
521  public function setStoreId($value)
522  {
523  $this->storeId = $value;
524  return $this;
525  }
526 
532  public function getStoreId()
533  {
534  if ($this->storeId === null) {
535  $this->storeId = $this->storeManager->getStore()->getId();
536  }
537  return $this->storeId;
538  }
539 
545  public function getCustomerGroupId()
546  {
547  if ($this->customerGroupId === null) {
548  return $this->customerSession->getCustomerGroupId();
549  }
550  return $this->customerGroupId;
551  }
552 
559  public function setCustomerGroupId($value)
560  {
561  $this->customerGroupId = $value;
562  return $this;
563  }
564 
565  //@codeCoverageIgnoreStart
566 
571  public function setItemId($itemId)
572  {
573  return $this->setData(self::ITEM_ID, $itemId);
574  }
575 
580  public function setProductId($productId)
581  {
582  return $this->setData(self::PRODUCT_ID, $productId);
583  }
584 
591  public function setWebsiteId($websiteId)
592  {
593  return $this->setData(self::WEBSITE_ID, $websiteId);
594  }
595 
602  public function setStockId($stockId)
603  {
604  return $this->setData(self::STOCK_ID, $stockId);
605  }
606 
611  public function setQty($qty)
612  {
613  return $this->setData(self::QTY, $qty);
614  }
615 
622  public function setIsInStock($isInStock)
623  {
624  return $this->setData(self::IS_IN_STOCK, $isInStock);
625  }
626 
631  public function setIsQtyDecimal($isQtyDecimal)
632  {
633  return $this->setData(self::IS_QTY_DECIMAL, $isQtyDecimal);
634  }
635 
640  public function setUseConfigMinQty($useConfigMinQty)
641  {
642  return $this->setData(self::USE_CONFIG_MIN_QTY, $useConfigMinQty);
643  }
644 
651  public function setMinQty($minQty)
652  {
653  return $this->setData(self::MIN_QTY, $minQty);
654  }
655 
660  public function setUseConfigMinSaleQty($useConfigMinSaleQty)
661  {
662  return $this->setData(self::USE_CONFIG_MIN_SALE_QTY, $useConfigMinSaleQty);
663  }
664 
671  public function setMinSaleQty($minSaleQty)
672  {
673  return $this->setData(self::MIN_SALE_QTY, $minSaleQty);
674  }
675 
680  public function setUseConfigMaxSaleQty($useConfigMaxSaleQty)
681  {
682  return $this->setData(self::USE_CONFIG_MAX_SALE_QTY, $useConfigMaxSaleQty);
683  }
684 
691  public function setMaxSaleQty($maxSaleQty)
692  {
693  return $this->setData(self::MAX_SALE_QTY, $maxSaleQty);
694  }
695 
700  public function setUseConfigBackorders($useConfigBackorders)
701  {
702  return $this->setData(self::USE_CONFIG_BACKORDERS, $useConfigBackorders);
703  }
704 
711  public function setBackorders($backOrders)
712  {
713  return $this->setData(self::BACKORDERS, $backOrders);
714  }
715 
720  public function setUseConfigNotifyStockQty($useConfigNotifyStockQty)
721  {
722  return $this->setData(self::USE_CONFIG_NOTIFY_STOCK_QTY, $useConfigNotifyStockQty);
723  }
724 
731  public function setNotifyStockQty($notifyStockQty)
732  {
733  return $this->setData(self::NOTIFY_STOCK_QTY, $notifyStockQty);
734  }
735 
740  public function setUseConfigQtyIncrements($useConfigQtyIncrements)
741  {
742  return $this->setData(self::USE_CONFIG_QTY_INCREMENTS, $useConfigQtyIncrements);
743  }
744 
752  {
753  return $this->setData(self::QTY_INCREMENTS, $qtyIncrements);
754  }
755 
760  public function setUseConfigEnableQtyInc($useConfigEnableQtyInc)
761  {
762  return $this->setData(self::USE_CONFIG_ENABLE_QTY_INC, $useConfigEnableQtyInc);
763  }
764 
771  public function setEnableQtyIncrements($enableQtyIncrements)
772  {
773  return $this->setData(self::ENABLE_QTY_INCREMENTS, $enableQtyIncrements);
774  }
775 
780  public function setUseConfigManageStock($useConfigManageStock)
781  {
782  return $this->setData(self::USE_CONFIG_MANAGE_STOCK, $useConfigManageStock);
783  }
784 
789  public function setManageStock($manageStock)
790  {
791  return $this->setData(self::MANAGE_STOCK, $manageStock);
792  }
793 
798  public function setLowStockDate($lowStockDate)
799  {
800  return $this->setData(self::LOW_STOCK_DATE, $lowStockDate);
801  }
802 
807  public function setIsDecimalDivided($isDecimalDivided)
808  {
809  return $this->setData(self::IS_DECIMAL_DIVIDED, $isDecimalDivided);
810  }
811 
816  public function setStockStatusChangedAuto($stockStatusChangedAuto)
817  {
818  return $this->setData(self::STOCK_STATUS_CHANGED_AUTO, $stockStatusChangedAuto);
819  }
820 
826  public function getExtensionAttributes()
827  {
828  return $this->_getExtensionAttributes();
829  }
830 
837  public function setExtensionAttributes(
838  \Magento\CatalogInventory\Api\Data\StockItemExtensionInterface $extensionAttributes
839  ) {
840  return $this->_setExtensionAttributes($extensionAttributes);
841  }
842 
843  //@codeCoverageIgnoreEnd
844 }
setEnableQtyIncrements($enableQtyIncrements)
Definition: Item.php:771
setIsDecimalDivided($isDecimalDivided)
Definition: Item.php:807
setExtensionAttributes(\Magento\CatalogInventory\Api\Data\StockItemExtensionInterface $extensionAttributes)
Definition: Item.php:837
_addQuoteItemError(\Magento\Quote\Model\Quote\Item $item, $itemError, $quoteError, $errorIndex='error')
Definition: Item.php:473
setUseConfigNotifyStockQty($useConfigNotifyStockQty)
Definition: Item.php:720
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
setUseConfigQtyIncrements($useConfigQtyIncrements)
Definition: Item.php:740
setUseConfigManageStock($useConfigManageStock)
Definition: Item.php:780
setUseConfigEnableQtyInc($useConfigEnableQtyInc)
Definition: Item.php:760
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Customer\Model\Session $customerSession, \Magento\Store\Model\StoreManagerInterface $storeManager, StockConfigurationInterface $stockConfiguration, StockRegistryInterface $stockRegistry, StockItemRepositoryInterface $stockItemRepository, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Item.php:109
$value
Definition: gender.phtml:16
setUseConfigBackorders($useConfigBackorders)
Definition: Item.php:700
setUseConfigMinSaleQty($useConfigMinSaleQty)
Definition: Item.php:660
setStockStatusChangedAuto($stockStatusChangedAuto)
Definition: Item.php:816
setUseConfigMaxSaleQty($useConfigMaxSaleQty)
Definition: Item.php:680
setUseConfigMinQty($useConfigMinQty)
Definition: Item.php:640