shnozolla shnozolla - 7 months ago 58
PHP Question

$product->save(); causing an error - when changing product qty

I am creating a custom module.

I am testing the ability to update a products quantity.

I have created a bare bones magento module and began testing with code inside indexController.php here is my code:

public function indexAction() {

//Just grabbing sku# 62701 for testing...
$tempProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', 62701 );

$stockData['qty'] = 300;
$tempProduct->setStockData($stockData);

$tempProduct->save();

}


By having the last line in there i get the error.

From the error log:

a:5:{i:0;s:46:"Invalid method Varien_Object::save(Array
(
)

Followed by the stack trace...

Any help is appreciated, thank you.

Answer

I found a way to save data that seemed to work in my scenario (not sure why it worked) here: http://stackoverflow.com/a/7608195/1110880

The code that made it work was the following:

    $productId = $tempProduct->getId();
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
    $stockItem->setData('qty', $qty);

    $stockItem->save();

I hope this helps someone.