Question

I tried a few snippets found online but with no success.

Just trying to set a product to be in stock / out of stock with code, this is as far as I've gotten:

$product = Mage::getModel('catalog/product')->load( 'id' );

$stockItem = $product->getStockItem();
$stockItem->setData('manage_stock', 1);
$stockItem->setData('is_in_stock', 0);
$stockItem->setData('use_config_notify_stock_qty', 0);
$stockItem->setData('qty', 0);

$stockItem->save();
$product->save();

It's making no changes to the product information when viewed through the admin panel, any ideas?

Was it helpful?

Solution

Try this instead:

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

and then set and save your changes as before.

OTHER TIPS

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

I had a similar issue but with the difference that there wasn't any stock item object for a particular product. When this happens it's good to create the stock item object initially before setting the quantity (unless there is a way to them at the same time).

// Check if there is a stock item object
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
$stockItemData = $stockItem->getData();
if (empty($stockItemData)) {

    // Create the initial stock item object
    $stockItem->setData('manage_stock',1);
    $stockItem->setData('is_in_stock',$qty ? 1 : 0);
    $stockItem->setData('use_config_manage_stock', 0);
    $stockItem->setData('stock_id',1);
    $stockItem->setData('product_id',$product->getId());
    $stockItem->setData('qty',0);
    $stockItem->save();

    // Init the object again after it has been saved so we get the full object
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
}

// Set the quantity
$stockItem->setData('qty',$qty);
$stockItem->save();
$product->save();

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.

trying on magento 1.8.1.0 CE and couldn't save "qty" due to missing "type_id" value (for new item). solved by

$stockItem->setTypeId( $product->getTypeId() );

I want to suggest a change to Raj's answer to support odd situations. In order for the answer to work I had to comment out the if statement, because products created in a programmatic manner can have partial, yet incomplete, stock data. This was the only way that I was able to get it to work.

// Check if there is a stock item object
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
$stockItemData = $stockItem->getData();
//if (empty($stockItemData)) {
    // Create the initial stock item object
    $stockItem->setData('manage_stock',1);
    $stockItem->setData('is_in_stock',$qty ? 1 : 0);
    $stockItem->setData('use_config_manage_stock', 0);
    $stockItem->setData('stock_id',1);
    $stockItem->setData('product_id',$product->getId());
    $stockItem->setData('qty',0);
    $stockItem->save();

    // Init the object again after it has been saved so we get the full object
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
//}

// Set the quantity
$stockItem->setData('qty',$qty);
$stockItem->save();
$product->save();

Here's a possible cause that cost me about 2 hours of work: I was trying this for ages wondering why it wouldn't save. I had an exit; php statement after the ->save() and a bunch of echos so it wouldn't move onto the next screen automatically (to help debug).

However the commits to the database must occur late on in the page load it wouldn't save for me until I removed the exit;!

As in other answers suggested one can use the class Mage_CatalogInventory_Model_Stock_Item directly. But one has to reference the product correctly via a call to setProduct to have working code both for new and existing stock items.

$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
$stockItem->setProduct($product)
    ->setData('stock_id', Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID)
    ->setData('qty', 1)
    ->setData('is_in_stock', 1)
    ->setData('manage_stock', 1)
    ->setData('use_config_manage_stock', 0)
    ->setData('use_config_backorders', 0)
    ->setData('backorders', 0)
    ->setData('use_config_max_sale_qty', 0)
    ->setData('max_sale_qty', 1)                
    ->save();

Alternative solution

However if you call $product->save anyway i would just set the stock data via $product->setStockdata($array) as follows. This way Magento creates a correct stock item from stock data. Big advantage is that the stock item update happens in the same transaction as $product->save(). This is important for data consistency.

$stockData = array();
$stockData['qty'] = 1;
$stockData['is_in_stock'] = 1;
$stockData['manage_stock'] = 1;
$stockData['use_config_manage_stock'] = 0;
$stockData['use_config_backorders'] = 0;
$stockData['backorders'] = 0;
$stockData['use_config_max_sale_qty'] = 0;
$stockData['max_sale_qty'] = 1;
$product->setStockData($stockData);
//...
$product->save()

This is my C# code

var inStock = qty > 0 ? 1 : 0;    
var stock_data = new catalogInventoryStockItemUpdateEntity()
            {
                qty = qty,
                is_in_stock = inStock,
                manage_stock = 1,
                is_in_stockSpecified = true,
            };

is_in_stockSpecified is important

This is the output

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top