Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
QuantityValidator Class Reference

Public Member Functions

 __construct (Option $optionInitializer, StockItem $stockItemInitializer, StockRegistryInterface $stockRegistry, StockStateInterface $stockState)
 
 validate (Observer $observer)
 

Protected Member Functions

 _removeErrorsFromQuoteAndItem ($item, $code)
 

Protected Attributes

 $optionInitializer
 
 $stockItemInitializer
 
 $stockRegistry
 
 $stockState
 

Detailed Description

Quote item quantity validator.

@api

Since
100.0.2 @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Deprecated:
2.3.0 Replaced with Multi Source Inventory https://devdocs.magento.com/guides/v2.3/inventory/catalog-inventory-replacements.html

Definition at line 33 of file QuantityValidator.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Option  $optionInitializer,
StockItem  $stockItemInitializer,
StockRegistryInterface  $stockRegistry,
StockStateInterface  $stockState 
)
Parameters
Option$optionInitializer
StockItem$stockItemInitializer
StockRegistryInterface$stockRegistry
StockStateInterface$stockState
Returns
void

Definition at line 62 of file QuantityValidator.php.

Member Function Documentation

◆ _removeErrorsFromQuoteAndItem()

_removeErrorsFromQuoteAndItem (   $item,
  $code 
)
protected

Removes error statuses from quote and item, set by this observer

Parameters
Item$item
int$code
Returns
void

Definition at line 258 of file QuantityValidator.php.

259  {
260  if ($item->getHasError()) {
261  $params = ['origin' => 'cataloginventory', 'code' => $code];
262  $item->removeErrorInfosByParams($params);
263  }
264 
265  $quote = $item->getQuote();
266  if ($quote->getHasError()) {
267  $quoteItems = $quote->getItemsCollection();
268  $canRemoveErrorFromQuote = true;
269  foreach ($quoteItems as $quoteItem) {
270  if ($quoteItem->getItemId() == $item->getItemId()) {
271  continue;
272  }
273 
274  $errorInfos = $quoteItem->getErrorInfos();
275  foreach ($errorInfos as $errorInfo) {
276  if ($errorInfo['code'] == $code) {
277  $canRemoveErrorFromQuote = false;
278  break;
279  }
280  }
281 
282  if (!$canRemoveErrorFromQuote) {
283  break;
284  }
285  }
286 
287  if ($canRemoveErrorFromQuote) {
288  $params = ['origin' => 'cataloginventory', 'code' => $code];
289  $quote->removeErrorInfosByParams(null, $params);
290  }
291  }
292  }
$quote
$quoteItem
Definition: quote.php:38
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$code
Definition: info.phtml:12

◆ validate()

validate ( Observer  $observer)

Check product inventory data when quote item quantity declaring

Parameters
\Magento\Framework\Event\Observer$observer
Returns
void
Exceptions

Check if product in stock. For composite products check base (parent) item stock status

Check item for options

Definition at line 108 of file QuantityValidator.php.

109  {
110  /* @var $quoteItem Item */
111  $quoteItem = $observer->getEvent()->getItem();
112  if (!$quoteItem ||
113  !$quoteItem->getProductId() ||
114  !$quoteItem->getQuote()
115  ) {
116  return;
117  }
118  $product = $quoteItem->getProduct();
119  $qty = $quoteItem->getQty();
120 
121  /* @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
122  $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
123  if (!$stockItem instanceof StockItemInterface) {
124  throw new LocalizedException(__('The Product stock item is invalid. Verify the stock item and try again.'));
125  }
126 
127  if (($options = $quoteItem->getQtyOptions()) && $qty > 0) {
128  foreach ($options as $option) {
129  $this->optionInitializer->initialize($option, $quoteItem, $qty);
130  }
131  } else {
132  $this->stockItemInitializer->initialize($stockItem, $quoteItem, $qty);
133  }
134 
135  if ($quoteItem->getQuote()->getIsSuperMode()) {
136  return;
137  }
138 
139  /* @var \Magento\CatalogInventory\Api\Data\StockStatusInterface $stockStatus */
140  $stockStatus = $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId());
141 
142  /* @var \Magento\CatalogInventory\Api\Data\StockStatusInterface $parentStockStatus */
143  $parentStockStatus = false;
144 
148  if ($quoteItem->getParentItem()) {
149  $product = $quoteItem->getParentItem()->getProduct();
150  $parentStockStatus = $this->stockRegistry->getStockStatus(
151  $product->getId(),
152  $product->getStore()->getWebsiteId()
153  );
154  }
155 
156  if ($stockStatus) {
157  if ($stockStatus->getStockStatus() === Stock::STOCK_OUT_OF_STOCK
158  || $parentStockStatus && $parentStockStatus->getStockStatus() == Stock::STOCK_OUT_OF_STOCK
159  ) {
160  $quoteItem->addErrorInfo(
161  'cataloginventory',
162  Data::ERROR_QTY,
163  __('This product is out of stock.')
164  );
165  $quoteItem->getQuote()->addErrorInfo(
166  'stock',
167  'cataloginventory',
168  Data::ERROR_QTY,
169  __('Some of the products are out of stock.')
170  );
171  return;
172  } else {
173  // Delete error from item and its quote, if it was set due to item out of stock
174  $this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
175  }
176  }
177 
181  if ($options) {
182  $qty = $product->getTypeInstance()->prepareQuoteItemQty($qty, $product);
183  $quoteItem->setData('qty', $qty);
184  if ($stockStatus) {
185  $this->checkOptionsQtyIncrements($quoteItem, $options);
186  }
187 
188  // variable to keep track if we have previously encountered an error in one of the options
189  $removeError = true;
190  foreach ($options as $option) {
191  $result = $option->getStockStateResult();
192  if ($result->getHasError()) {
193  $option->setHasError(true);
194  //Setting this to false, so no error statuses are cleared
195  $removeError = false;
196  $this->addErrorInfoToQuote($result, $quoteItem, $removeError);
197  }
198  }
199  if ($removeError) {
200  $this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
201  }
202  } else {
203  if ($quoteItem->getParentItem() === null) {
204  $result = $quoteItem->getStockStateResult();
205  if ($result->getHasError()) {
206  $this->addErrorInfoToQuote($result, $quoteItem);
207  } else {
208  $this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
209  }
210  }
211  }
212  }
__()
Definition: __.php:13
$quoteItem
Definition: quote.php:38

Field Documentation

◆ $optionInitializer

$optionInitializer
protected

Definition at line 38 of file QuantityValidator.php.

◆ $stockItemInitializer

$stockItemInitializer
protected

Definition at line 43 of file QuantityValidator.php.

◆ $stockRegistry

$stockRegistry
protected

Definition at line 48 of file QuantityValidator.php.

◆ $stockState

$stockState
protected

Definition at line 53 of file QuantityValidator.php.


The documentation for this class was generated from the following file: