0

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);
$productToUpdate->setQty($product->Stock);
$productToUpdate->setIsInStock((bool)$product->Stock);
$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

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();
        $criteria->setProductsFilter($this->getProductIds($input->getArgument('product_ids')));
        $collection = $this->stockItemRepository->getList($criteria);
        $stockItems = $collection->getItems();
        foreach ($stockItems as $stockItem) {
            if ($stockItem && $stockItem->getItemId()) {
                $qty = rand(0, 45);
                $stockItem->setQty($qty);
                $stockItem->setIsInStock(($qty>0) ? 1 : 0);
                $this->stockItemRepository->save($stockItem);
            }
        }
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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