Question

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();
Était-ce utile?

La solution

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();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top