How I can change stock availability ie in stock and out of stock of product programmatically using product ID?

  • 1
    Hi Ganesh, you really need to work at your accept ratio.
    – user487772
    Jul 26, 2013 at 13:52
  • for going through your answers and accepting those which were answered correctly :-)
    – user487772
    Jul 31, 2013 at 14:21

2 Answers 2


The following code should work for you

$stock_item = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product_id);
if (!$stock_item->getId()) {
    $stock_item->setData('product_id', $product_id);
    $stock_item->setData('stock_id', 1); 

$stock_item->setData('is_in_stock', 1); // is 0 or 1
$stock_item->setData('manage_stock', 1); // should be 1 to make something out of stock

try {
} catch (Exception $e) {
    echo "{$e}";

Someone marked this as duplicate, it is not. Those looking to do it through database can refer to original answer of Shoaib Munir - Magento 2 - Bulk update stock programmatically - Using database query

Apart from two updates in cataloginventory_stock_item and cataloginventory_stock_status; also update inventory_source_item. Your cron should reindex automatically after that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.