If you ever tried to import product stocks in Magento I’m sure you noticed is extremly slow.
Doesn’t matter how many fields you set in the CSV file it always takes the same time.
Digging arround I found this amazing code.
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)
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:
"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
Product Import
No there is no fast product import, yet.


Hi, i`m using this to update my status and it`s working very well. We have more than 5000 products on database. Sometimes the php function does not ends and timeout. What can i do? Maybe updating half of the stock in each function? Hope you can help me, thank you.
Yes, perhaps you could try to do it in two times.
Upload 2 CSV files with 2500 each.
If that’s a lot of trouble you may ask for a server upgrade, having more RAM and CPU it might help your store
Regards
Hey dude.. you rock, but tell me one thing The stock id will automatically will get set? specially visibilty and Is in stock attribute?
hmmmm I think it will only update the defined fields in the CSV.
Regards
Hi again, after rechecking your script, No wonder Your php script completely rock but whenever I update stuff my stock availabilty remains out of stock and same with visibilty, Can you tell me how to add these two attributes also?
Regards,
well in the example you already have is_in_stock and qty fields
Regards
Thankx, Its perfect solution
Do you have any idea where can i exceed execution time? I have tried my php.ini and increase execution time but doesnt work, so any other idea?
It shows me this error: Maximum execution time of 30 seconds exceeded in /home/pentljac/domains/pentlja.com/public_html/app/code/core/Mage/Eav/Model/Config.php on line 560
Try to contact your Hosting provider. Perhaps they have limited that resource from their side.
Regards
Hey, I am looking for some script like this which will add new products in my store but without replacing any information about current products.
Can you do that?
It will b great for me.
I’m sorry but I can’t.
Best of luck !
Ps: I’ve removed your e-mail to prevent spam.
Awesome code – I’ve been looking for something like this for weeks. Thanks!
By the way, in Magento 1.7 (and maybe other versions, I don’t know) there is no /var/import/ folder, instead they combined the import and export folders into /var/importexport/
…which requires a change of the folder location on ~ line 9 of the php file above.
Thanks for the heads up JBH
Thanks for the script. It works beautifully with test CSV file.
However, my POS creates CSV file but it puts different heading so script does not work. I want to automate the process so is there any way to change the names of headers?
Script requires the headers to be
“sku”,”qty”
my CSV is
“ITEM”,”STOCK”
Is there any way for these to different names to be linked within the script so that script sees ITEM as sku and STOCK as qty?
Thanks for your help.
Kevin
Hey Kevin,
Have you actually tried to run the script with your custom fields?
The script looks at the first line (headers) and uses that key. That why you can set any field in there.
Let me know,
Cheers.
Hello Rick
Awesome script thanks! I have same problem that csv is using these headers “ITEM”,”STOCK”
I tried to run the script with “ITEM”,”STOCK” in csv but nothing is updated.
Any suggestions?
That’s weird. I’m guessing you’re using Magento 1.7, right?
Have you tried using other fields?
Do you have the magento cache on?
Did you rewrite the index catalog?
Cheers
Hey Rick, You made a simple, but great solution for our problem. Magento 1.7 import has a BUG : error in qty, and your script saved me for $2000.
Great!
Hi Jerick,
I’m glad it helped !
Feel free to suscribe to my RSS Feed to get future posts
Cheers