We have multiple warehouses for our web-store and we want to update stock via script whenever we get new stock in. However, when I try to update the stock level via program, the stock level goes up superficially but stock in the warehouse remains to show "0". This is a major problem and creates stock issues in our website.

I am attaching a snapshot for you to see what the issue is:

Stock issue

As you can see, the stock itself within the Product attributes state it is 22, however it's value within the warehouse stock = 0. How can I go about updating the stock value in the specific warehouse programmatically?

When I try to update stock via the following code :

$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($xyzModel->getId());
            $stockItem->setData('qty', $newQty);
            $stockItem->setData('is_in_stock', $newInStock);

It only updates the stock superficially, as shown in the photo.

Any help would be appreciated. Thanks in advance!

1 Answer 1


Try this bit of code. Please make sure your paths to files are full paths (especially if you are going to run cronjobs, otherwise this wont work)

Create a file and add the following:


ini_set('display_errors', 1);
require_once '/var/www/html/website/httpdocs/app/Mage.php';

// Function to read each line of csv file seperately
function readCSV($csvFile){
    $file_handle = fopen($csvFile, 'r');
    while (!feof($file_handle) ) {
        $line_of_text[] = fgetcsv($file_handle, 1024, ';');
    return $line_of_text;

$csvFile = '/var/www/html/website/httpdocs/csv-with-stock-and-sku.csv';
$csvdata = readCSV($csvFile);

foreach($csvdata as $data){
    $sku = $data[9]; //SKU
    $stock = $data[6]; //STOCK

    $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
    if ($product) {
        $productId = $product->getId();
        $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
        $stockItem->setData('qty', $stock);

        if($stock <= 0){
            $stockItem->setData('is_in_stock', 0);
        } else{
            $stockItem->setData('is_in_stock', 1);


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.