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

3

5

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?

Trochelminth answered 16/7, 2011 at 5:51 Comment(0)
M
8

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.

Mercy answered 28/12, 2011 at 10:13 Comment(0)
E
1

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();
Exieexigency answered 27/7, 2011 at 13:31 Comment(2)
Yes, I have also used that to get the quantity of a product on a product detail page. Unfortunately, that is not what I am trying to do here. I need to get the stockData array so that I can modify it and save it back to the product updated.Trochelminth
so why don't you use the code above without getQty()? Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId()) that should do the trick!Digit
D
1

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();
Daven answered 17/8, 2012 at 13:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.