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


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.

  • I want to get the stock status, and i get this with $product->getStockItem()->getIsInStock(), but I'm getting a fatal error. – Prog6 Jul 13 '17 at 16:26
  • Yes use this : $newStatus = $product->getStatus(); or use $newStatus = $product->getData(); then select what you want – PЯINCƏ Jul 13 '17 at 16:28
  • and how can i select the status of stock? and this will return to me a boolean value? – Prog6 Jul 13 '17 at 16:34
  • Look my update please – PЯINCƏ Jul 13 '17 at 16:52
  • anyone help me to solve add to cart button error -> code -> justpaste.it/49eqz – zus Aug 17 '18 at 11:27

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.