How to set a product to be in/out of stock programatically

Try this instead:

$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);

and then set and save your changes as before.


Try the following. you shouldn't have to load a new stock object

$product->setStockData(
   array( 
          'is_in_stock' => 0, 
          'qty' => 0,
          'manage_stock' => 1,
          'use_config_notify_stock_qty' => 1
   )
 ); 

And the just save the product as you did in your script


As the addition to all answers it may be a good idea to use addData instead of setData in oder to be safe for updating the info and not only setting it.