I was having issues for updating stock quantity items of product in Magento. I was loading the product then setting the stock quantity and saving the product. But that does not work for me. Finally it was resolved, in order to update the stock quantity of the product we shouldn't need to create a new stock_item
nor should you have to save the product.
$product = Mage::getModel('catalog/product')->load($productid);
if (!($stockItem = $product->getStockItem())) {
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->assignProduct($product)
->setData('stock_id', 1)
->setData('store_id', 1);
}
$stockItem->setData('qty', $stockQty)
->setData('is_in_stock', $stockQty > 0 ? 1 : 0)
->setData('manage_stock', 1)
->setData('use_config_manage_stock', 0)
->save();
All done!. Now stock items will be updated to the database.
Thanks for reading the blog.
0 Comment(s)