0

I have been added 5000 products in my store and initially all products QTY is 0 and STOCK AVAILABILITY is "OUT OF STOCK" i want to update all my products stock from a single csv so when this csv file run it update the qty and set the STOCK AVAILABILITY "IN STOCK". please tell me how to do that because this is very confusing for me and which tool is used to update stock in this way

  • Have you tried anything? Do you have code samples? It should be pretty simple using Varien_File_Csv. – pspahn Sep 20 '14 at 23:03
2

The column to set stock availability is is_in_stock 0 = out of stock, 1 = in stock

0

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:

<?php 

set_time_limit(0);
ini_set('display_errors', 1);
ini_set('memory_limit','1024M');
require_once '/var/www/html/website/httpdocs/app/Mage.php';
Mage::app(); 


// 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, ';');
    }
    fclose($file_handle);
    return $line_of_text;
}

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



/* UPDATE PRICES */
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);
        }

        $stockItem->save();
        unset($stockItem);
        unset($product);
    }
}?>

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.