$ Product-> Save (); causing an error - when changing the quantity of goods

I am creating a custom module.

I am testing the ability to update the number of products.

I created an empty bone framework module and started testing with the 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(); } 

Having the last line there, I get an error.

From the error log:

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

Following the stack trace ...

Any help is appreciated, thanks.

+4
source share
4 answers

I found a way to save the data that seemed to work in my script (I don't know why this worked) here: fooobar.com/questions/433771 / ...

The code that earned was as follows:

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

I hope this helps someone.

+4
source

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(); 
+4
source

I see two potential problems.

  • Are you sure loadByAttribute('sku', 62701 ) can only return one element? (This error showed when people tried to update several things at once.)
  • $stockData not defined. At a minimum, it would be safer to define it.

It looks like you are missing something like this:

 $stockData = $tempProduct->getStockData(); 
+1
source

I'm probably doing something wrong, but I had the same problem and it helped:

 $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId()); $stockItem->setProductId($product->getId()); $stockItem->setStockId(Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID); 
+1
source

Source: https://habr.com/ru/post/1436004/


All Articles