Update Magento stocks with CSV and PHP script

Update Magento stocks with CSV and PHP script

August 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Update Magento stocks with CSV and PHP script

Updating Magento Stock Status from CSV Without Changing Quantity

Hello developers! I recently came across a useful script for updating product quantities in Magento, but it had an unexpected behavior that I’d like to clarify today.

Understanding the Stock Status vs Quantity

In Magento, two main attributes control stock availability:

  1. Quantity (Qty): The physical number of units available.
  2. Is In Stock (Out_of_stock): A binary flag indicating whether a product is currently in stock or not.

These two fields serve different purposes:

  • Quantity represents the actual inventory levels.
  • Is In Stock determines if a product appears as “In Stock” on the frontend catalog and whether it’s available for purchase.

The Issue: Changing Quantity When Updating Stock Status

The script at www.blog.magepsycho.com works well for updating quantities, but when you try to use it with a CSV containing only “sku” and “is_in_stock”, it incorrectly sets the “qty” to 0 if “is_in_stock” is 0.

This happens because Magento’s setIsInStock method also triggers the save event, which updates all stock data including quantity. The script isn’t aware that you only want to change the status flag and not modify actual inventory levels.

A Better Approach: Manually Set Is In Stock

To solve this issue without touching the quantity:

  1. You should use Magento’s API or directly manipulate the database.
  2. For API, use the catalog_product.update method with the field stock_item.is_in_stock.

Here’s a simple example using Laravel’s Guzzle library to interact with Magento’s SOAP API (assuming you have already configured the API credentials):

use GuzzleHttp\Client;

$client = new Client();

$apiEndPoint = 'http://your_magento_instance/index.php/api/v2_soap/?wsdl=1';
$apiUserName = 'username';
$apiApiKey  = 'apikey';
$apiSessionId = null;

$client->post($apiEndPoint, [
    'auth' => [$apiUserName, $apiApiKey],
    // ... other parameters
]);

$response = json_decode($client->getBody()->getContents(), true);
  1. For database manipulation (not recommended for production), directly update the cataloginventory_stock_item table:
UPDATE cataloginventory_stock_item
SET is_in_stock = 0
WHERE item_id IN (
    SELECT entity_id FROM catalog_product_entity where entity_id in (1,2,3) -- replace with your product IDs
);

Conclusion

When updating stock status from a CSV, it’s crucial to distinguish between the physical quantity and the “in stock” availability flag. Directly manipulating these values avoids unwanted side effects like setting quantities to 0.

Remember that while these methods work, they may require additional configuration or setup depending on your Magento version and environment. Always test changes in a safe development environment before deploying them to production.

Happy coding! Let me know if you have any other questions about Magento development. Learn more about our services at https://laravelcompany.com.

Catalog Database API Import/Export

Our network