1
votes

I am updating the Stock on Hand Inventory Qty on a Magento store through an extension that I have developed, using the following code:

Mage::getModel("cataloginventory/stock_item")
  ->loadByProduct($pid)
  ->setQty($qty)
  ->save();

Now, from my testing, this works fine, however I am a little concerned if this is having any negative affects on the different types of product that can be created in Magento (such as simple and complex products).

Is the above the correct way to update the SOH, and do I need to handle Complex products any differently? My gut feeling is that I don't need to do anything differently with complex products as they all end up deriving from a Simple product which has its own Stock on Hand?

Any advice appreciated

1

1 Answers

3
votes

As long as you only update simples you'll be fine like this. Indeed all other non-virtual product types derive their stock from the simples.

You might even want to add

$stockItem = Mage::getModel("cataloginventory/stock_item")
    ->loadByProduct($pid)
    ->setQty($qty);

if ($stockItem->getCanBackInStock() && $stockItem->getQty() > $stockItem->getMinQty()) {
    $stockItem->setIsInStock(true)
        ->setStockStatusChangedAutomaticallyFlag(true);
}

$stockItem->save();

See Mage_CatalogInventory_Model_Stock::backItemQty() to see how Magento adds stock.