1

I'm trying to update the StockData in catalog_product_save_before observer and not having any luck.

I can get the stock data like this

$_newProduct = $observer->getProduct();
$_newStock = $_newProduct->getStockData();
$PostQty = $_newStock['qty'];

I can update anything that isn't stock data like this

$_newProduct->setData('ebay_qty', $eBayQty);

I need to update the Qty based on other attributes. I have tried all these with no luck

$_newStock['qty'] = $NewQty; // Doesn't work
$_newStock->setData('qty', $NewQty); // Doesn't work
$_newStock->setQty($NewQty); // Doesn't work

I have also tried this

$_newStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_newProduct);
$_newStock->setQty($PossibleQty);
$_newStock->save();
1

If it not must be catalog_product_save_before, try catalog_product_save_after instead and this:

public function setQty(Varien_Event_Observer $observer)
{
    $stockQty = 999;
    $product = $observer->getProduct();
    $product->getStockItem()
        ->setData('qty', $stockQty)
        ->save();
}
  • Thank you for trying, but I already tried that. If you call $product->save() from catalog_product_save_after, it creates an endless loop saving the product. – lv2fly Jun 20 '17 at 18:14
  • Read carefully ... I call 'save()' on the stock item. Code is tested. – sv3n Jun 20 '17 at 18:23
  • You are partially correct. I did call – lv2fly Jun 20 '17 at 23:01
  • You're completely correct. Thank you! I dismissed this because I was trying with save_after for many weeks and thought I had tried every possibility. But I think I was getting the stockItem incorrectly before. Thanks again! Not much info out there on this type of stuff. – lv2fly Jun 20 '17 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.