ylliX - Online Advertising Network

Quick tip: updating inventory in Magento using SKU

Its not that bad, all you have to do is get Stock Object, update quantity and save again along with product:

$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $SKU);
$qty = 12;
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product->getId());
$stockItemData = $stockItem->getData();
if (empty($stockItemData)) {

	$stockItem->setData('manage_stock', 1);
	$stockItem->setData('is_in_stock', $qty > 0 ? 1 : 0);
	$stockItem->setData('use_config_manage_stock', 0);
	$stockItem->setData('stock_id', 1);
	$stockItem->setData('product_id', $_product->getId());
	$stockItem->setData('qty', $qty);
	$stockItem->save();

	$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product->getId());
}
$stockItem->setData('is_in_stock', $qty > 0 ? 1 : 0);
$stockItem->setData('qty', $qty);
$stockItem->save();
$_product->save();

 

One thought on “Quick tip: updating inventory in Magento using SKU

Leave a Reply