objectFactory = $objectFactory; $this->format = $format; $this->isProductSalableForRequestedQty = $isProductSalableForRequestedQty; $this->getSkusByProductIds = $getSkusByProductIds; $this->stockResolver = $stockResolver; $this->storeManager = $storeManager; $this->backOrderNotifyCustomerCondition = $backOrderNotifyCustomerCondition; } /** * Replace legacy quote item check * * @param StockStateInterface $subject * @param \Closure $proceed * @param int $productId * @param float $itemQty * @param float $qtyToCheck * @param float $origQty * @param int|null $scopeId * * @return DataObject * @throws LocalizedException * @throws NoSuchEntityException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundCheckQuoteItemQty( StockStateInterface $subject, \Closure $proceed, $productId, $itemQty, $qtyToCheck, $origQty, $scopeId = null ) { $result = $this->objectFactory->create(); $result->setHasError(false); $qty = $this->getNumber($itemQty); $skus = $this->getSkusByProductIds->execute([$productId]); $productSku = $skus[$productId]; $websiteCode = $this->storeManager->getWebsite()->getCode(); $stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode); $stockId = $stock->getStockId(); $isSalableResult = $this->isProductSalableForRequestedQty->execute($productSku, (int)$stockId, $qty); if ($isSalableResult->isSalable() === false) { /** @var ProductSalabilityError $error */ foreach ($isSalableResult->getErrors() as $error) { $result->setHasError(true)->setMessage($error->getMessage())->setQuoteMessage($error->getMessage()) ->setQuoteMessageIndex('qty'); } } $productSalableResult = $this->backOrderNotifyCustomerCondition->execute($productSku, (int)$stockId, $qty); if ($productSalableResult->getErrors()) { /** @var ProductSalabilityError $error */ foreach ($productSalableResult->getErrors() as $error) { $result->setMessage($error->getMessage()); } } return $result; } /** * Convert quantity to a valid float * * @param string|float|int|null $qty * * @return float|null */ private function getNumber($qty) { if (!is_numeric($qty)) { return $this->format->getNumber($qty); } return $qty; } }