Magento – Create Observer to Notify When Item Goes Out of Stock

adminemailevent-observerinventory

***SOLVED ON MY OWN****
Hi guys,

I just solved it using a debugger. It seems that getStockItem() was trying to get an object that doesn't exist that's why it was returning "null". The correct call was getItem(). This got me all the information I needed to know. Thank you for your help guys.


I have created a module in magento that will send an email when an item goes out of stock. I used this as a template : Daily admin email for products that went out of stock that day

My issue, that if($stockItem->getQty() === $stockItem->getNotifyStockQty()) is always to null… I am thinking that the event I am listening for in my config.xml file is the wrong event. The event in question: cataloginventory_stock_item_save_after.

I get an email, but there is no data, it just sends me the strings written in the observer file :: just Ran out of stock: Current Qty: Low Stock Date:

What would be the right event to listen for?

class MyCompany_MyModule_Model_Observer
{
    public function lowStockReport($observer)
    {
        $stockItem = $observer->getEvent();
        if($stockItem->getQty() === $stockItem->getNotifyStockQty()){
            //stock is lower than the notify amount, send email
            $product = Mage::getModel('catalog/product')->load($stockItem->getProductId());
            $body = "{$product->getName()} :: {$product->getSku()} just Ran out of stock:\n\n";
            $body .= "Current Qty: {$stockItem->getQty()}\n";
            $body .= "Low Stock Date: {$stockItem->getLowStockDate()}\n";
            $mail = new Zend_Mail();
            $mail->setType(Zend_Mime::MULTIPART_RELATED);
            $mail->setBodyHtml($body);
            $mail->setFrom('lowstock@yourwebsite.com', 'Low Stock Notifier');
            $mail->addTo('youremail@gmail.com', 'Your Name Here');
            $mail->setSubject('[Notice] An Item Has Gone Out of Stock');
            $mail->send();
        }
    }
}

Thank you in advanced.

Best Answer

I think problems is that $stockItem is event object.

Try to get stock item object from event

$stockItem = $observer->getEvent()->getStockItem();