Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckQuoteItemQtyPlugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use Magento\Framework\DataObject\Factory as ObjectFactory;
23 
29 {
33  private $objectFactory;
34 
38  private $format;
39 
43  private $isProductSalableForRequestedQty;
44 
48  private $getSkusByProductIds;
49 
53  private $stockResolver;
54 
58  private $storeManager;
59 
63  private $backOrderNotifyCustomerCondition;
64 
75  public function __construct(
76  ObjectFactory $objectFactory,
77  FormatInterface $format,
78  IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty,
79  GetSkusByProductIdsInterface $getSkusByProductIds,
80  StockResolverInterface $stockResolver,
81  StoreManagerInterface $storeManager,
82  BackOrderNotifyCustomerCondition $backOrderNotifyCustomerCondition
83  ) {
84  $this->objectFactory = $objectFactory;
85  $this->format = $format;
86  $this->isProductSalableForRequestedQty = $isProductSalableForRequestedQty;
87  $this->getSkusByProductIds = $getSkusByProductIds;
88  $this->stockResolver = $stockResolver;
89  $this->storeManager = $storeManager;
90  $this->backOrderNotifyCustomerCondition = $backOrderNotifyCustomerCondition;
91  }
92 
109  public function aroundCheckQuoteItemQty(
110  StockStateInterface $subject,
111  \Closure $proceed,
112  $productId,
113  $itemQty,
114  $qtyToCheck,
115  $origQty,
116  $scopeId = null
117  ) {
118  $result = $this->objectFactory->create();
119  $result->setHasError(false);
120 
121  $qty = $this->getNumber($itemQty);
122 
123  $skus = $this->getSkusByProductIds->execute([$productId]);
124  $productSku = $skus[$productId];
125 
126  $websiteCode = $this->storeManager->getWebsite()->getCode();
127  $stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
128  $stockId = $stock->getStockId();
129 
130  $isSalableResult = $this->isProductSalableForRequestedQty->execute($productSku, (int)$stockId, $qty);
131 
132  if ($isSalableResult->isSalable() === false) {
134  foreach ($isSalableResult->getErrors() as $error) {
135  $result->setHasError(true)->setMessage($error->getMessage())->setQuoteMessage($error->getMessage())
136  ->setQuoteMessageIndex('qty');
137  }
138  }
139 
140  $productSalableResult = $this->backOrderNotifyCustomerCondition->execute($productSku, (int)$stockId, $qty);
141  if ($productSalableResult->getErrors()) {
143  foreach ($productSalableResult->getErrors() as $error) {
144  $result->setMessage($error->getMessage());
145  }
146  }
147 
148  return $result;
149  }
150 
158  private function getNumber($qty)
159  {
160  if (!is_numeric($qty)) {
161  return $this->format->getNumber($qty);
162  }
163 
164  return $qty;
165  }
166 }
$storeManager
foreach($websiteCodes as $websiteCode) $skus
$format
Definition: list.phtml:12
__construct(ObjectFactory $objectFactory, FormatInterface $format, IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty, GetSkusByProductIdsInterface $getSkusByProductIds, StockResolverInterface $stockResolver, StoreManagerInterface $storeManager, BackOrderNotifyCustomerCondition $backOrderNotifyCustomerCondition)
$stock
if(!isset($_GET['website_code'])) $websiteCode
Definition: website.php:11