10

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?

1
  • Your code is working for me, though, you have to have the setting to allow the stock to be managed via the backend.
    – philwinkle
    Mar 19, 2013 at 18:42

9 Answers 9

15

Try this instead:

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

and then set and save your changes as before.

3
  • Thanks for that, I solved the issue by manually switching to "manage stock" = yes in the admin panel. Once that's done, your code works perfectly.
    – CodeByMike
    Mar 19, 2013 at 16:09
  • this worked for magento 1.6
    – ahnbizcad
    Nov 4, 2015 at 2:24
  • When i do this in an observer function, below error message is displaying The requested quantity for "Productname" is not available. is there any method $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($postProductId);$stockItem->setData('qty', $currentStock); $stockItem->save();
    – Naveenbos
    Jan 22, 2018 at 12:48
8

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

3
  • Does not work under 1.8 CE...
    – Dor
    Mar 28, 2014 at 8:45
  • 2
    OP posted the question for ce1.6. But what exactly is not working. Does it throw an error? Mar 28, 2014 at 8:56
  • Does work under 1.9 CE. Feb 24, 2017 at 12:53
3

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();
3
  • 1
    $product->save(); - is it required after saving $stockItem?
    – Styx
    Mar 29, 2015 at 23:01
  • 1
    Probably not for stock item alone unless you're changing other product attributes.
    – Raj
    Apr 1, 2015 at 7:37
  • Yes, because if you create it from scratch you need to add the stockItem to the product, an Observer will pick it up and save it later (so duplicate saving isn't recquired
    – Max
    Mar 23, 2020 at 12:29
3

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() );
2
  • Mate, took me a long time to find the right solution for my problem. You comment saved my life. Cheers! ;-)
    – medina
    Aug 4, 2015 at 6:55
  • Saved my life too!!!!
    – Lock
    Apr 14, 2016 at 11:54
3

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.

0
2

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();
1

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;!

1

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()
0

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

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.