Change product quantity having a list of SKUs and quantities
Question
I have product SKU.
using that i have to change product quantity As stock so how to do this.
I am getting a SKU and quantity like this
Array
(
[minimalVS] => 1
[seaglassin] => 0
[half_moon_knuckle_ring] => 90
[unique_Walnut_and_Cherry] => 8
[half_eternity_ring_Sapphire_ring] => 11
[ear_jacket] => 2
[short_Thin_Line] => 44
[totally_Awesome] => 1
[handcrafted_Leather_Briefcase] => 5
[militaryexpedition] => 5
)
Key as SKU and Value as Quantity means [sku] => quantity so how to change quantity
Solution
The load
method accepts the field to load on as a second argument. In this case that would be sku
$productId = Mage::getModel('catalog/product')->getIdBySku([the SKU], 'sku');
$product = Mage::getModel('catalog/product')->load($productId);
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
// some default stock settings that you might need
$stockItem->setData('manage_stock', 1);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('use_config_notify_stock_qty', 0);
$stockItem->setData('qty', [the quantity]);
$stockItem->save();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange