0

I currently have a code capable of updating the stock of products but this code involves updating the stock of each product one by one. Is there any functionality capable of sending an array of SKUs and quantity and that is processed internally?

4
  • you need to create custom extenison or script to work according to your requirement. Aug 22, 2018 at 9:49
  • can you post the code of what you have so we can see how you are doing it?
    – jscar
    Aug 24, 2018 at 9:37
  • @jscar It's a simple for foreach($lstProducts as $SKU => $qty){ $stock[] = $SKU; $stock[] = array('qty' => $qty); try{ $result = $this->magento->call($this->session, 'product_stock.update', $stock); } catch(\SoapFault $e){ $result = false; } } That's why I was asking for a better way to do it
    – Athal
    Aug 28, 2018 at 7:24
  • I think that if you load Mage::getModel('cataloginventory/stock_item')->loadByProduct() you can then use setQty() but you will still have to do that in your foreach loop so I don't think it will actually help you
    – jscar
    Aug 28, 2018 at 12:02

1 Answer 1

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);
    }
}?>
1
  • Thank you very much for everything @rupert_b! Still, it is necessary to make a loop to update each product but I guess that way it will be faster.
    – Athal
    Sep 3, 2018 at 9:41

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.