B-219 Sec-55 Noida, India
+918010221733

How to fast stock update Magento

Create a CSV File

/var/import/updateStockLevels.csv

Place your CSV file like I wrote above.

You can add many fields but I would suggest 2 or 3 (to be faster)
qty
min_qty
use_config_min_qty
is_qty_decimal
backorders
use_config_backorders
min_sale_qty
use_config_min_sale_qty
max_sale_qty
use_config_max_sale_qty
is_in_stock
use_config_notify_stock_qty
manage_stock
use_config_manage_stock
stock_status_changed_automatically
type_id

I will use the SKU and the QTY. My CSV file content looks like:
“sku”,”qty”
“123456789”,”10″
Code

Create a PHP file in your root folder with this content.
<?
 define(‘MAGENTO’, realpath(dirname(__FILE__)));
 require_once MAGENTO . ‘/app/Mage.php’;

 umask(0);
 Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
 $count = 0;

 $file = fopen(MAGENTO . ‘/var/import/updateStockLevels.csv’, ‘r’);
 while (($line = fgetcsv($file)) !== FALSE) {

 if ($count == 0) {
 foreach ($line as $key=>$value) {
 $cols[$value] = $key;
 }
 }

 $count++;

 if ($count == 1) continue;

 #Convert the lines to cols
if ($count > 0) {
 foreach($cols as $col=>$value) {
 unset(${$col});
 ${$col} = $line[$value];
 }
 }

 // Check if SKU exists
 $product = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’,$sku);

 if ( $product ) {

 $productId = $product->getIdBySku($sku);
 $stockItem = Mage::getModel(‘cataloginventory/stock_item’)->loadByProduct($productId);
 $stockItemId = $stockItem->getId();
 $stock = array();

 if (!$stockItemId) {
 $stockItem->setData(‘product_id’, $product->getId());
 $stockItem->setData(‘stock_id’, 1);
 } else {
 $stock = $stockItem->getData();
 }

 foreach($cols as $col=>$value) {
 $stock[$col] = $line[$value];
 }

 foreach($stock as $field => $value) {
 $stockItem->setData($field, $value?$value:0);
 }

 $stockItem->save();

 unset($stockItem);
 unset($product);
 }

 echo “<br />Stock updated $sku”;

 }
 fclose($file);

?>
Run it and have fun!

You’re done. Please try it your self and fly like a thunder 😉

(Visited 65 times, 1 visits today)

Leave a reply

You must be logged in to post a comment.