From ebd94acda9f82b9ca1a87c37764ef8b04e3bb48a Mon Sep 17 00:00:00 2001
From: Andrey Nikolaev <andrey@firebearstudio.com>
Date: Thu, 19 Jan 2023 15:37:31 +0100
Subject: [PATCH] [SUPPORT-14624]. MSI compatibility fix

---
 Controller/Product/UpdateProductInfo.php      | 19 ++++++++++++++----
 Model/Inventory/ProductStockQty.php           | 18 +++++++++++++----
 .../Product/View/Type/Configurable.php        | 20 ++++++++++++++-----
 3 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/Controller/Product/UpdateProductInfo.php b/Controller/Product/UpdateProductInfo.php
index 321ee559..026a29f7 100644
--- a/Controller/Product/UpdateProductInfo.php
+++ b/Controller/Product/UpdateProductInfo.php
@@ -25,6 +25,8 @@ use Magento\Framework\Message\ManagerInterface;
 use Firebear\ConfigurableProducts\Helper\Data as DataHelper;
 use Magento\Framework\Controller\ResultFactory;
 use Magento\InventoryCatalog\Model\GetStockIdForCurrentWebsite;
+use Magento\Framework\App\ObjectManager;
+use Magento\Framework\Module\Manager;
 
 class UpdateProductInfo extends AbstractController
 {
@@ -33,6 +35,11 @@ class UpdateProductInfo extends AbstractController
      */
     const CUSTOM_ATTRIBUTES_KEY = 'customAttributes';
 
+    /**
+     * @var Manager
+     */
+    protected $moduleManager;
+
     /**
      * @var ProductRepositoryInterface
      */
@@ -90,8 +97,8 @@ class UpdateProductInfo extends AbstractController
         ProductAttributesRender $productAttributesRender,
         ProductRepositoryInterface $productRepository,
         ProductStockQty $productStockQty,
-        GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite,
-        DataHelper $icpHelper
+        DataHelper $icpHelper,
+        Manager $moduleManager
     ) {
         parent::__construct($request, $resultJsonFactory, $messageManger);
         $this->productRepository = $productRepository;
@@ -99,8 +106,12 @@ class UpdateProductInfo extends AbstractController
         $this->productAttributesRender = $productAttributesRender;
         $this->icpHelper = $icpHelper;
         $this->resultFactory = $context->getResultFactory();
-        $this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite;
         $this->productStockQty = $productStockQty;
+        $this->moduleManager = $moduleManager;
+        if ($this->moduleManager->isEnabled('Magento_InventoryCatalog')) {
+            $this->getStockIdForCurrentWebsite = ObjectManager::getInstance()
+                ->create(GetStockIdForCurrentWebsite::class);
+        }
     }
 
     /**
@@ -174,7 +185,7 @@ class UpdateProductInfo extends AbstractController
             $this->productAttributesRender->renderCustomBlock($product)
         );
 
-        $stockId = $this->getStockIdForCurrentWebsite->execute();
+        $stockId = $this->getStockIdForCurrentWebsite ? $this->getStockIdForCurrentWebsite->execute() : 0;
         if ($this->icpHelper->getGeneralConfig('general/show_salable_qty')) {
             try {
                 $stockQty = $this->productStockQty->getSalableQty($stockId, $simpleProductSku);
diff --git a/Model/Inventory/ProductStockQty.php b/Model/Inventory/ProductStockQty.php
index 26c2f105..caf19e68 100644
--- a/Model/Inventory/ProductStockQty.php
+++ b/Model/Inventory/ProductStockQty.php
@@ -10,7 +10,9 @@ declare(strict_types=1);
 
 namespace Firebear\ConfigurableProducts\Model\Inventory;
 
+use Magento\Framework\App\ObjectManager;
 use Magento\Framework\App\ResourceConnection;
+use Magento\Framework\Module\Manager;
 use Magento\InventorySales\Model\GetProductSalableQty;
 
 /**
@@ -19,6 +21,11 @@ use Magento\InventorySales\Model\GetProductSalableQty;
  */
 class ProductStockQty
 {
+    /**
+     * @var Manager
+     */
+    protected $moduleManager;
+
     /**
      * @var ResourceConnection
      */
@@ -32,14 +39,17 @@ class ProductStockQty
     /**
      * ProductStockQty constructor.
      * @param ResourceConnection $resourceConnection
-     * @param GetProductSalableQty $productSalableQty
      */
     public function __construct(
         ResourceConnection $resourceConnection,
-        GetProductSalableQty $productSalableQty
+        Manager $moduleManager
     ) {
         $this->resourceConnection = $resourceConnection;
-        $this->getProductSalableQty = $productSalableQty;
+        $this->moduleManager = $moduleManager;
+        if ($this->moduleManager->isEnabled('Magento_InventorySales')) {
+            $this->getProductSalableQty = ObjectManager::getInstance()
+                ->create(GetProductSalableQty::class);
+        }
     }
 
     /**
@@ -69,6 +79,6 @@ class ProductStockQty
      */
     public function getSalableQty($stockId, $sku)
     {
-        return $this->getProductSalableQty->execute($sku, $stockId);
+        return $this->getProductSalableQty ? $this->getProductSalableQty->execute($sku, $stockId) : 0;
     }
 }
diff --git a/Plugin/Block/ConfigurableProduct/Product/View/Type/Configurable.php b/Plugin/Block/ConfigurableProduct/Product/View/Type/Configurable.php
index d42dca59..0e504328 100644
--- a/Plugin/Block/ConfigurableProduct/Product/View/Type/Configurable.php
+++ b/Plugin/Block/ConfigurableProduct/Product/View/Type/Configurable.php
@@ -37,6 +37,8 @@ use Magento\Framework\Registry;
 use Magento\Framework\View\LayoutInterface;
 use Magento\Swatches\Helper\Data as SwatchesHelper;
 use Magento\InventoryCatalog\Model\GetStockIdForCurrentWebsite;
+use Magento\Framework\App\ObjectManager;
+use Magento\Framework\Module\Manager;
 
 class Configurable
 {
@@ -66,6 +68,11 @@ class Configurable
         'short_description'
     ];
 
+    /**
+     * @var Manager
+     */
+    protected $moduleManager;
+
     /**
      * @var ConfigurableBlock
      */
@@ -175,7 +182,6 @@ class Configurable
      * @param Json $jsonSerializer
      * @param Logger $logger
      * @param ProductAttributes $renderProductAttributes
-     * @param GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite
      */
     public function __construct(
         ICPHelper $icpHelper,
@@ -191,7 +197,7 @@ class Configurable
         Json $jsonSerializer,
         Logger $logger,
         ProductAttributes $renderProductAttributes,
-        GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite
+        Manager $moduleManager
     ) {
         $this->cpiHelper = $icpHelper;
         $this->jsonSerializer = $jsonSerializer;
@@ -206,7 +212,11 @@ class Configurable
         $this->productRepository = $productRepository;
         $this->logger = $logger;
         $this->renderProductAttributes = $renderProductAttributes;
-        $this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite;
+        $this->moduleManager = $moduleManager;
+        if ($this->moduleManager->isEnabled('Magento_InventoryCatalog')) {
+            $this->getStockIdForCurrentWebsite = ObjectManager::getInstance()
+                ->create(GetStockIdForCurrentWebsite::class);
+        }
     }
 
     /**
@@ -272,7 +282,7 @@ class Configurable
             } else {
                 $config['bundle_id'] = null;
             }
-                $this->prepareGeneralConfig($config);
+            $this->prepareGeneralConfig($config);
             if ($fullActionName !== self::CUSTOM_CATALOGSEARCH_RESULT_INDEX) {
                 $this->prepareParentProductOptions($config);
                 $this->prepareSimpleProductOptions($config);
@@ -598,7 +608,7 @@ class Configurable
             $productId = (int)$product->getId();
             $product = $this->productRepository->getById($productId);
             $sku = $product->getSku();
-            $stockId = $this->getStockIdForCurrentWebsite->execute();
+            $stockId = $this->getStockIdForCurrentWebsite ? $this->getStockIdForCurrentWebsite->execute() : 0;
             if ($this->cpiHelper->getGeneralConfig('general/show_salable_qty')) {
                 $stockQty = $this->productStockQty->getSalableQty($stockId, $sku);
             } else {
-- 
2.37.0

