From dd9875ee1ca7c0df17d3a95317dae77599b9f7c1 Mon Sep 17 00:00:00 2001
From: Pavel Bystritsky
Date: Mon, 4 Sep 2023 15:31:45 +0200
Subject: [PATCH] Q1-480: Inventory performance improvement.
---
Model/Quote/GetQuoteInventoryData.php | 89 +++++++++------------------
1 file changed, 30 insertions(+), 59 deletions(-)
diff --git a/Model/Quote/GetQuoteInventoryData.php b/Model/Quote/GetQuoteInventoryData.php
index ad61ac3..1a4d6ac 100644
--- a/Model/Quote/GetQuoteInventoryData.php
+++ b/Model/Quote/GetQuoteInventoryData.php
@@ -1,4 +1,5 @@
cartRepository = $cartRepository;
$this->shopIdValidator = $shopIdValidator;
@@ -83,6 +101,8 @@ public function __construct(
$this->inventoryDataFactory = $inventoryDataFactory;
$this->objectManager = $objectManager;
$this->storeManager = $storeManager;
+ $this->moduleManager = $moduleManager;
+ $this->isProductSalableForRequestedQty = $isProductSalableForRequestedQty;
}
/**
@@ -158,84 +178,35 @@ private function isProductSalable(CartItemInterface $item): bool
}
try {
+ if (!$this->moduleManager->isEnabled('Magento_InventorySalesApi')) {
+ return (bool)$item->getProduct()->isSalable();
+ }
$stockResolver = $this->getStockResolverService();
- $productSalableForRequestedQtyService = $this->getProductSalableForRequestedQtyService();
$sku = $item['product']['sku'];
$requestedQty = $item->getParentItem() ? (float)$item->getParentItem()->getQty() : (float)$item->getQty();
- $request = $this->getRequest(
- (string)$sku,
- $requestedQty
- );
- if (!$request || !$stockResolver || !$productSalableForRequestedQtyService) {
- return (bool)$item->getProduct()->isSalable();
- }
$websiteId = (int)$this->storeManager->getStore($item->getStoreId())->getWebsiteId();
$websiteCode = $this->storeManager->getWebsite($websiteId)->getCode();
$stockId = $stockResolver->execute('website', $websiteCode)->getStockId();
- $result = current($productSalableForRequestedQtyService->execute([$request], $stockId));
+ $result = $this->isProductSalableForRequestedQty->execute($sku, $stockId, $requestedQty);
+
return $result->isSalable();
} catch (Exception $e) {
return false;
}
}
- /**
- * Try to build \Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface. If it's not possible, return null.
- *
- * @return \Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface|null
- */
- private function getProductSalableForRequestedQtyService(): ?\Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface
- {
- try {
- $areProductsSalable = $this->objectManager->get(
- \Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface::class
- );
- } catch (Throwable $e) {
- $areProductsSalable = null;
- }
- return $areProductsSalable;
- }
-
/**
* Try to build \Magento\InventorySalesApi\Api\StockResolverInterface. If it's not possible, return null.
*
- * @return \Magento\InventorySalesApi\Api\StockResolverInterface|null
+ * @return StockResolverInterface|null
*/
- private function getStockResolverService(): ?\Magento\InventorySalesApi\Api\StockResolverInterface
+ private function getStockResolverService(): ?StockResolverInterface
{
try {
- $stockResolver = $this->objectManager->get(\Magento\InventorySalesApi\Api\StockResolverInterface::class);
+ $stockResolver = $this->objectManager->get(StockResolverInterface::class);
} catch (Throwable $e) {
$stockResolver = null;
}
return $stockResolver;
}
-
- /**
- * Try to build \Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyRequestInterface.
- * If it's not possible, return null.
- *
- * @param string $sku
- * @param float $qty
- * @return \Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyRequestInterface|null
- */
- private function getRequest(
- string $sku,
- float $qty
- ): ?\Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyRequestInterface {
- try {
- $requestFactory = $this->objectManager->get(
- \Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyRequestInterfaceFactory::class
- );
- $request = $requestFactory->create(
- [
- 'sku' => $sku,
- 'qty' => $qty,
- ]
- );
- } catch (Throwable $e) {
- $request = null;
- }
- return $request;
- }
}