I had to study this question and find the problem. The whole data / data element interface is a bit more complicated.
Problem
Mage_Catalog_Model_Abstract->loadByAttribute uses load-collection in the catalog/product_collection with a limit of 1.
The stock items in this collection are loaded through the event observer, Mage_CatalogInventory_Model_Observer->addStockStatusToCollection , which listens for the catalog_product_collection_load_after event. addStockStatusToCollection downloads items if the require_stock_items true flag is set.
In the case of Mage_Catalog_Model_Abstract->loadByAttribute this flag is not set, therefore we get $product->_data['stock_item'] type Varien_Object (see Mage_CatalogInventory_Model_Stock_Status lines 488-491).
It causes an error
Invalid Varien_Object :: save ... method
at $product->save .
I recommend solution 1 if you still save the product and want inventory updates to occur in the same transaction. Solution 2 installs only stock data to avoid costly calls to $product->save .
Solution 1
Use extra load on the product: it loads $product->_data['stock_item'] .
Mage::getModel('catalog/product')->loadByAttribute('sku', 62701 ); $tempProduct->load($tempProduct->getId());
This is an extra read transaction in the database that slows down.
Decision 2
As with the other suggested answers, use the Mage_CatalogInventory_Model_Stock_Item class directly. But you correctly specify the product by calling setProduct to have a working code for both 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();