Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockItem.php
Go to the documentation of this file.
1 <?php
7 
11 
12 class StockItem
13 {
17  protected $quoteItemQtyList;
18 
22  protected $typeConfig;
23 
27  protected $stockState;
28 
34  public function __construct(
38  ) {
39  $this->quoteItemQtyList = $quoteItemQtyList;
40  $this->typeConfig = $typeConfig;
41  $this->stockState = $stockState;
42  }
43 
56  public function initialize(
57  \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem,
59  $qty
60  ) {
61  $product = $quoteItem->getProduct();
65  if ($quoteItem->getParentItem()) {
66  $rowQty = $quoteItem->getParentItem()->getQty() * $qty;
70  $qtyForCheck = $this->quoteItemQtyList
71  ->getQty($product->getId(), $quoteItem->getId(), $quoteItem->getQuoteId(), 0);
72  } else {
73  $increaseQty = $quoteItem->getQtyToAdd() ? $quoteItem->getQtyToAdd() : $qty;
74  $rowQty = $qty;
75  $qtyForCheck = $this->quoteItemQtyList->getQty(
76  $product->getId(),
77  $quoteItem->getId(),
78  $quoteItem->getQuoteId(),
79  $increaseQty
80  );
81  }
82 
83  $productTypeCustomOption = $product->getCustomOption('product_type');
84  if ($productTypeCustomOption !== null) {
85  // Check if product related to current item is a part of product that represents product set
86  if ($this->typeConfig->isProductSet($productTypeCustomOption->getValue())) {
87  $stockItem->setIsChildItem(true);
88  }
89  }
90 
91  $stockItem->setProductName($product->getName());
92 
93  $result = $this->stockState->checkQuoteItemQty(
94  $product->getId(),
95  $rowQty,
96  $qtyForCheck,
97  $qty,
98  $product->getStore()->getWebsiteId()
99  );
100 
101  if ($stockItem->hasIsChildItem()) {
102  $stockItem->unsIsChildItem();
103  }
104 
105  if ($result->getItemIsQtyDecimal() !== null) {
106  $quoteItem->setIsQtyDecimal($result->getItemIsQtyDecimal());
107  if ($quoteItem->getParentItem()) {
108  $quoteItem->getParentItem()->setIsQtyDecimal($result->getItemIsQtyDecimal());
109  }
110  }
111 
117  if ($result->getHasQtyOptionUpdate() && (!$quoteItem->getParentItem() ||
118  $quoteItem->getParentItem()->getProduct()->getTypeInstance()->getForceChildItemQtyChanges(
119  $quoteItem->getParentItem()->getProduct()
120  )
121  )
122  ) {
123  $quoteItem->setData('qty', $result->getOrigQty());
124  }
125 
126  if ($result->getItemUseOldQty() !== null) {
127  $quoteItem->setUseOldQty($result->getItemUseOldQty());
128  }
129 
130  if ($result->getMessage() !== null) {
131  $quoteItem->setMessage($result->getMessage());
132  }
133 
134  if ($result->getItemBackorders() !== null) {
135  $quoteItem->setBackorders($result->getItemBackorders());
136  }
137 
138  $quoteItem->setStockStateResult($result);
139 
140  return $result;
141  }
142 }
initialize(\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem, \Magento\Quote\Model\Quote\Item $quoteItem, $qty)
Definition: StockItem.php:56
__construct(ConfigInterface $typeConfig, QuoteItemQtyList $quoteItemQtyList, StockStateInterface $stockState)
Definition: StockItem.php:34
$quoteItem
Definition: quote.php:38