Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 1.5.1.0 - While trying to update stock quantity on products, getStockData is returning NULL

I have products loaded in Magento that I am trying to bulk update the inventory qty on. I created all the products using Mage_Catalog_Model_Product and set the qty on them using setStockData like:

$product = new Mage_Catalog_Model_Product();

$product->setTypeId('simple');
$product->setStatus(1);
$product->setSku($sku);
$product->setStockData(array(
    'is_in_stock' => 1,
    'qty' => $record['stockstatus'],
    'manage_stock' => 0,
));
...

So I've seen setStockData work ... 13,000+ times.

Now, as I said, I'm trying to update the inventory on the products I created using a variation on what I did to create the products ... a variation that I see here and virtually identically elsewhere on the web.

My problem is that I can use a variation of the code at the link above and get a valid product object, but when I call getStockData on the object, it returns NULL:

$product = Mage::getModel('catalog/product')
                ->loadByAttribute('product_code', '678910');

var_dump($product->getName()); // returns 'Hello My Name is Product'
var_dump($product->getProductCode()); // returns '678910'
var_dump($product->getSku()); // returns 'SKU1234'
var_dump($product->getStockData()); // returns NULL (and there is a qty of 52 set)

(I've also tried getting the product with no variations on the code at the link above and have had the same results as I expected.)

$product_id = Mage::getModel('catalog/product')->getIdBySku('SKU1234');
$product = Mage::getModel('catalog/product');
$product->load($product_id);
$stockData = $product->getStockData();

var_dump($product->getName()); // returns 'Hello My Name is Product'
var_dump($product->getProductCode()); // returns '678910'
var_dump($product->getSku()); // returns 'SKU1234'
var_dump($product->getStockData()); // returns NULL (and there is a qty of 52 set)

So, if I can't get the stock data with getStockData, I can't set the stock data like:

$product = Mage::getModel('catalog/product')
                ->loadByAttribute('product_code', $record['productcode']);

$stockData = $product->getStockData();

$stockData['qty'] = $record['stockstatus'];
$stockData['is_in_stock'] = ($record['stockstatus'] > 0) ? 1 : 0;

$product->setStockData($stockData);

$product->save();

Is there something I'm missing? I don't understand why getStockData is returning NULL. Can someone help me understand what I may be doing wrong?

like image 751
demarts Avatar asked Jul 16 '11 05:07

demarts


3 Answers

Use getStockItem() instead of getStockData()

$stockData = $product->getStockItem();
$stockData->setData('qty',555);
$stockData->setData('is_in_stock',1);
$stockData->setData('manage_stock',1);
$stockData->setData('use_config_manage_stock',1);
$stockData->save(); // This enough to save stock data.

Use getData for arrays, use getItem for objects.

like image 50
Peter L Avatar answered Sep 29 '22 02:09

Peter L


yeah no why getStockData() returns null.. But I was able to get the stock quantity using the following:

    $qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId())->getQty();
like image 31
kwek Avatar answered Sep 29 '22 02:09

kwek


You should call save() on stock_item rather than product after changing qty. Following code worked for me:

$product = Mage::getModel('catalog/product')
                 ->loadByAttribute('product_code', '678910');

$stock_obj = Mage::getModel('cataloginventory/stock_item')
                  ->loadByProduct($product->getId());

$stockData = $stock_obj->getData();
$stockData['qty'] = $record['stockstatus'];
$stock_obj->setData($stockData);
$stock_obj->save();
like image 37
umair Avatar answered Sep 29 '22 01:09

umair