I have tried to follow the next solution: Magento 2: Programmatically Updating Inventory
But in Magento 2.3.5 this solution is deprecated. \Magento\CatalogInventory\Api\StockRegistryInterface
Currently what is the solution to update stock?
My code:
public function updateProductStockByData(array $data)
{
$skuArray = $this->getMapWithSkuKey($data);
try {
$productIds = $this->getMapWithProductIdKey($skuArray);
if (count($productIds)) {
$searchCriteria = $this->getSearchCriteria($productIds);
$stockItemCollection = $this->stockItemRepository->getList($searchCriteria);
if ($stockItemCollection->getTotalCount()) {
/** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
foreach ($stockItemCollection->getItems() as $stockItem) {
$newQty = $this->dataDictionaryWithProductIdKey[$stockItem->getProductId()]['STOCK'];
$stockItem->setQty($newQty);
if ($stockItem->getQty() == 0) {
$stockItem->setIsInStock(false);
}
$this->stockItemRepository->save($stockItem);
}
}
}
} catch (\Exception $exception) {
$this->reportManager->reportNewStockException($exception);
}
}