I have a custom import of stock from a CSV file, if I have a small number of products to update, it works fine.

But When I have 2400 (like I have here and now), the gateway times out.

The code looks like this, and I am executing it one item at the time.

$productToUpdate = $this->stockRegistry->getStockItemBySku($product->Ean);
$this->stockRegistry->updateStockItemBySku($product->Ean, $productToUpdate);

The last time I made something like this, I updated the database directly due to the speed issues, and then rebuilt the stock index.

But isn't it possible to do it "the right way" and disable the update actions while updating, so I can run the updates and then the "post-update actions"?

1 Answer 1


Using the 4 dependencies, the code below seems pretty fast and yet uses very much what the code uses to update a stock inventory. You may have to specify the website_id but hopefully that should give you an idea how to improve your stock update.

\Magento\CatalogInventory\Api\StockItemRepositoryInterface $stockItemRepository; \Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory  $stockItemCriteriaFactory;
        \Magento\CatalogInventory\Api\StockItemRepositoryInterface $stockItemRepository
        \Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory

$criteria = $this->stockItemCriteriaFactory->create();
        $collection = $this->stockItemRepository->getList($criteria);
        $stockItems = $collection->getItems();
        foreach ($stockItems as $stockItem) {
            if ($stockItem && $stockItem->getItemId()) {
                $qty = rand(0, 45);
                $stockItem->setIsInStock(($qty>0) ? 1 : 0);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.