Magento 2: Can't update product stock (quantity)

Magento 2: Can't update product stock (quantity)

August 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2: Can't update product stock (quantity)

Magento 2: Solving the Invisible Product Quantity Update Issue

You’ve found yourself in a frustrating situation where your custom product update script isn’t changing inventory levels despite following all the documented steps. Let’s troubleshoot this issue and ensure your products’ stock is accurately managed.

The Current Challenge

Your command attempts to loop through all products, modify the quantity, and save each one. You’re running the indexer from the command line after execution, but the product quantities remain unchanged on the frontend and backend.

The segmentation fault you encountered earlier suggested a potential memory management issue or PHP configuration problem with your Vagrant box’s PHP7 installation. Changing the indexer to “Update on Schedule” was a good start, as it likely prevented the segmentation fault by reducing the load during indexing.

However, we still need to ensure that the product quantity is properly saved and indexed after your script runs.

Understanding Magento 2 Indexing

Magento 2 uses indexes to improve performance by pre-sorting data based on specific queries. When you update a product’s stock level, the indexer needs to be updated accordingly for the change to reflect across the platform.

You’re currently running the indexer from the command line manually after executing your script:

php bin/magento indexer:reindex cataloginventory_stock

While this is the correct approach, it seems that either the save() method isn’t properly triggering a reindex or there’s an issue with the indexer itself.

Potential Causes and Solutions

1. Indexing Mode Not Configured Correctly

Ensure your indexing mode is set to “Reindex Automatically” when you update data in the admin section:

  • Go to Stores > Configuration > Catalog > Inventory > Index Management.
  • Set both “Use Flat Indexer for Reorder Items” and “Update on Save” to “Yes”.
  • Click “Save Config”.

This configuration should trigger an automatic reindex whenever a product is saved, but it might not be sufficient in your case.

2. Insufficient Permissions or Missing Files

Check if the necessary permissions are set for the var directory:

chmod -R 777 var/

Also, verify that all required indexer files exist under vendor/magento/module-cataloginventory/etc/indexer.xml.

3. Core Magento Issue with Indexing

There have been known issues in core Magento where the indexer doesn’t update properly after saving data programmatically.

As a workaround, you can try manually triggering the reindex from within your script:

$reindexResult = $objectManager->get('\Magento\Indexer\Model\IndexerFactory')->create()
    ->load('cataloginventory_stock')
    ->reindexAll();

Add this line after your save() method to ensure the indexer is updated immediately.

Improved Script Implementation

Let’s modify your script with some best practices and potential solutions:

uses Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\State;
use Magento\Framework\App\ObjectManager;

class UpdateCommand extends Command
{
    // ... existing code ...

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->state->setAreaCode('adminhtml');
        $this->objectManager->get('\Magento\Indexer\Model\Config')->saveConfig();

        // ... existing loop and saving logic ...

        $reindexResult = $objectManager->get('\Magento\Indexer\Model\IndexerFactory')->create()
            ->load('cataloginventory_stock')
            ->reindexAll();

        $output->writeln('<info>Reindex Results: ' . print_r($reindexResult, true) . '</info>');
    }
}

In this version:

  • We call $this->objectManager->get('\Magento\Indexer\Model\Config')->saveConfig(); to ensure the indexer configuration is up-to-date.
  • After the loop and save() method, we manually trigger the indexer reindex using the provided code.

Conclusion

When dealing with Magento 2 indexing issues, it’s crucial to follow the proper configuration settings and manual reindexing procedures. In your case, the potential causes could be a misconfiguration of the indexing mode or an underlying issue with the core Magento indexing process.

By incorporating the suggested changes in your script and ensuring the necessary permissions are set, you should see your product quantities updating correctly after execution.

Remember that Magento 2’s architecture can sometimes require manual intervention to ensure all components work together seamlessly. If you still encounter issues, consider reaching out to Laravel Company for expert Magento development support and solutions tailored to your specific needs.

Magento 2 Catalog Indexing Performance

Our network