diff --git a/vendor/magento/module-inventory-catalog/Model/SourceItemsSaveSynchronization/SetDataToLegacyCatalogInventory.php b/vendor/magento/module-inventory-catalog/Model/SourceItemsSaveSynchronization/SetDataToLegacyCatalogInventory.php index b4cd49e7bcc..cdf13dc1bfd 100644 --- a/vendor/magento/module-inventory-catalog/Model/SourceItemsSaveSynchronization/SetDataToLegacyCatalogInventory.php +++ b/vendor/magento/module-inventory-catalog/Model/SourceItemsSaveSynchronization/SetDataToLegacyCatalogInventory.php @@ -17,11 +17,14 @@ use Magento\Framework\Exception\NoSuchEntityException; use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface; use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockItem; use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockStatus; -use Magento\InventoryCatalogApi\Model\SourceItemsSaveSynchronizationInterface; +use Magento\CatalogInventory\Model\Indexer\Stock\CacheCleaner; +use Magento\Framework\App\ObjectManager; use Magento\InventorySalesApi\Api\AreProductsSalableInterface; /** * Set Qty and status for legacy CatalogInventory Stock Information tables. + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class SetDataToLegacyCatalogInventory { @@ -60,6 +63,11 @@ class SetDataToLegacyCatalogInventory */ private $indexerProcessor; + /** + * @var CacheCleaner + */ + private $cacheCleaner; + /** * @var AreProductsSalableInterface */ @@ -74,6 +82,8 @@ class SetDataToLegacyCatalogInventory * @param Processor $indexerProcessor * @param SetDataToLegacyStockStatus $setDataToLegacyStockStatus * @param AreProductsSalableInterface $areProductsSalable + * @param CacheCleaner|null $cacheCleaner + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( SetDataToLegacyStockItem $setDataToLegacyStockItem, @@ -83,7 +93,8 @@ class SetDataToLegacyCatalogInventory StockStateProviderInterface $stockStateProvider, Processor $indexerProcessor, SetDataToLegacyStockStatus $setDataToLegacyStockStatus, - AreProductsSalableInterface $areProductsSalable + AreProductsSalableInterface $areProductsSalable, + ?CacheCleaner $cacheCleaner = null ) { $this->setDataToLegacyStockItem = $setDataToLegacyStockItem; $this->setDataToLegacyStockStatus = $setDataToLegacyStockStatus; @@ -93,13 +104,13 @@ class SetDataToLegacyCatalogInventory $this->stockStateProvider = $stockStateProvider; $this->indexerProcessor = $indexerProcessor; $this->areProductsSalable = $areProductsSalable; + $this->cacheCleaner = $cacheCleaner ?? ObjectManager::getInstance()->get(CacheCleaner::class); } /** * Updates Stock information in legacy inventory. * * @param array $sourceItems - * @return void */ public function execute(array $sourceItems): void { @@ -109,6 +120,36 @@ class SetDataToLegacyCatalogInventory } $stockStatuses = $this->getStockStatuses($skus); + $productIds = []; + foreach ($sourceItems as $sourceItem) { + $sku = $sourceItem->getSku(); + + try { + $productIds[] = (int)$this->getProductIdsBySkus->execute([$sku])[$sku]; + } catch (NoSuchEntityException $e) { + // Skip synchronization of for not existed product + continue; + } + } + + if (count($productIds) > 0) { + $this->cacheCleaner->clean( + $productIds, + function () use ($sourceItems) { + $this->updateSourceItems($sourceItems); + } + ); + } + } + + /** + * Updates Stock information in legacy inventory. + * + * @param array $sourceItems + * @return void + */ + private function updateSourceItems(array $sourceItems): void + { $productIds = []; foreach ($sourceItems as $sourceItem) { $sku = $sourceItem->getSku();