diff --git a/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php b/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php new file mode 100644 index 00000000000..d5c1e721a3f --- /dev/null +++ b/InventoryConfigurableProduct/Plugin/CatalogInventory/Api/StockRegistry/UpdateLegacyStockStatusForConfigurableProduct.php @@ -0,0 +1,86 @@ +changeParentStockStatus = $changeParentStockStatus; + $this->productFactory = $productFactory; + } + + /** + * Update product stock item by product SKU + * + * @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 ($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 + */ + private 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 34438c885b3..06cc56819e2 100644 --- a/InventoryConfigurableProduct/etc/di.xml +++ b/InventoryConfigurableProduct/etc/di.xml @@ -36,6 +36,8 @@ +