shnozolla shnozolla - 1 year ago 148
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;



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 Source

I found a way to save data that seemed to work in my scenario (not sure why it worked) here:

The code that made it work was the following:

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


I hope this helps someone.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download