From 4baaa7e824ba09b89a077a5a4d7cf148dd288dbe Mon Sep 17 00:00:00 2001 From: paresh patel Date: Wed, 19 Jul 2023 12:19:05 +0530 Subject: [PATCH 1/3] Fixed- configurable product not back in stock after updating qty for the variant product by API/import --- ...egacyStockStatusForConfigurableProduct.php | 88 +++++++++++++++++++ InventoryConfigurableProduct/etc/di.xml | 2 + 2 files changed, 90 insertions(+) create mode 100644 InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php diff --git a/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php b/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php new file mode 100644 index 00000000000..c922bacff11 --- /dev/null +++ b/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php @@ -0,0 +1,88 @@ +getProductIdsBySkus = $getProductIdsBySkus; + $this->changeParentStockStatus = $changeParentStockStatus; + $this->productFactory = $productFactory; + } + + /** + * @param StockRegistryInterface $subject + * @param int $result + * @param string $productSku + * @param StockItemInterface $stockItem + * @return int + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterUpdateStockItemBySku( + StockRegistryInterface $subject, + $result, + string $productSku, + StockItemInterface $stockItem + ) { + $productIds[] = $this->resolveProductId($productSku); + if (!empty($productIds)) { + $this->changeParentStockStatus->execute($productIds); + } + return $result; + } + + /** + * @param string $productSku + * @return int + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + protected function resolveProductId($productSku) + { + $product = $this->productFactory->create(); + $productId = $product->getIdBySku($productSku); + if (!$productId) { + throw new \Magento\Framework\Exception\NoSuchEntityException( + __( + 'The Product with the "%1" SKU doesn\'t exist.', + $productSku + ) + ); + } + return $productId; + } +} diff --git a/InventoryConfigurableProduct/etc/di.xml b/InventoryConfigurableProduct/etc/di.xml index 139741a9df4..be9161608b9 100644 --- a/InventoryConfigurableProduct/etc/di.xml +++ b/InventoryConfigurableProduct/etc/di.xml @@ -39,6 +39,8 @@ + Date: Thu, 28 Dec 2023 10:39:07 +0530 Subject: [PATCH 2/3] fixed test failed issue --- ...egacyStockStatusForConfigurableProduct.php | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php b/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php index c922bacff11..cdb4e0891fd 100644 --- a/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php +++ b/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php @@ -10,9 +10,11 @@ use Magento\CatalogInventory\Api\Data\StockItemInterface; use Magento\CatalogInventory\Api\StockRegistryInterface; use Magento\Catalog\Model\ProductFactory; -use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface; use Magento\ConfigurableProduct\Model\Inventory\ChangeParentStockStatus; +/*** + * Update stock status of configurable products when update children products stock status + */ class UpdateLegacyStockStatusForConfigurableProduct { /** @@ -20,32 +22,26 @@ class UpdateLegacyStockStatusForConfigurableProduct */ private $changeParentStockStatus; - /** - * @var GetProductIdsBySkusInterface - */ - private $getProductIdsBySkus; - /** * @var ProductFactory */ protected $productFactory; /** - * @param GetProductIdsBySkusInterface $getProductIdsBySkus * @param ChangeParentStockStatus $changeParentStockStatus * @param ProductFactory $productFactory */ public function __construct( - GetProductIdsBySkusInterface $getProductIdsBySkus, ChangeParentStockStatus $changeParentStockStatus, ProductFactory $productFactory ) { - $this->getProductIdsBySkus = $getProductIdsBySkus; $this->changeParentStockStatus = $changeParentStockStatus; $this->productFactory = $productFactory; } /** + * Update product stock item by product SKU + * * @param StockRegistryInterface $subject * @param int $result * @param string $productSku @@ -59,19 +55,21 @@ public function afterUpdateStockItemBySku( string $productSku, StockItemInterface $stockItem ) { - $productIds[] = $this->resolveProductId($productSku); - if (!empty($productIds)) { - $this->changeParentStockStatus->execute($productIds); + $productIds = $this->resolveProductId($productSku); + if ($productIds) { + $this->changeParentStockStatus->execute([$productIds]); } return $result; } /** + * Resolve and retrieve the product ID based on the SKU + * * @param string $productSku * @return int * @throws \Magento\Framework\Exception\NoSuchEntityException */ - protected function resolveProductId($productSku) + private function resolveProductId($productSku) { $product = $this->productFactory->create(); $productId = $product->getIdBySku($productSku); From 754118f68a2f81d80127e490b0aeeade1bce3243 Mon Sep 17 00:00:00 2001 From: paresh patel Date: Mon, 1 Jan 2024 10:39:48 +0530 Subject: [PATCH 3/3] Fixed helth index test --- .../UpdateLegacyStockStatusForConfigurableProduct.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php b/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php index cdb4e0891fd..d5c1e721a3f 100644 --- a/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php +++ b/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php @@ -25,7 +25,7 @@ class UpdateLegacyStockStatusForConfigurableProduct /** * @var ProductFactory */ - protected $productFactory; + private $productFactory; /** * @param ChangeParentStockStatus $changeParentStockStatus