sourceItemFactory = $sourceItemFactory; $this->sourceItemsSave = $sourceItemsSave; $this->getSkusByProductIds = $getSkusByProductIds; $this->getDefaultSourceItemBySku = $getDefaultSourceItemBySku; $this->defaultSourceProvider = $defaultSourceProvider; } /** * @param Item $legacyStockItem * @return void * @throws \Magento\Framework\Exception\CouldNotSaveException * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Validation\ValidationException */ public function execute(Item $legacyStockItem) { $productSku = $this->getSkusByProductIds ->execute([$legacyStockItem->getProductId()])[$legacyStockItem->getProductId()]; $sourceItem = $this->getDefaultSourceItemBySku->execute($productSku); if ($sourceItem === null) { /** @var SourceItemInterface $sourceItem */ $sourceItem = $this->sourceItemFactory->create(); $sourceItem->setSourceCode($this->defaultSourceProvider->getCode()); $sourceItem->setSku($productSku); } $sourceItem->setQuantity((float)$legacyStockItem->getQty()); $sourceItem->setStatus((int)$legacyStockItem->getIsInStock()); $this->sourceItemsSave->execute([$sourceItem]); } }