Fatal Error: Call to a Member Function – Magento 1.9 Fix

magento-1.9PHP

I try to get status of stock but i get this error message:

Fatal error: Call to a member function getIsInStock() on null with
$stockProduct = $product->getStockItem()->getIsInStock();

I got this error message when i try to create a new product.

I'm using observer: catalog_product_save_after

Best Answer

You can really use this event catalog_product_save_after and you do something like this:

public function productStatus($observer) {
    $product = $observer->getProduct();
    $stockStatus = $product->getStockItem()->getData();

}

The collection :

$products = Mage::getModel('catalog/product')->getCollection();
foreach ($products as $_product) {
    $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
    echo $stock->getQty();
    echo $stock->getMinQty();
    echo $stock->getMinSaleQty();
}

Nb: We can't get stock directly from product, you need an join.

Related Topic