3

***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.

2
  • Could you please add more code from your observer? – oleksii.svarychevskyi Mar 3 '14 at 19:13
  • If you solved your own questions - please add this as an answer and accept it. If any of the other provided answers got you on the right track feel free to accept one of those adding a comment what you had to do in addition. – Kristof at Fooman Mar 3 '14 at 21:04
0

I think problems is that $stockItem is event object.

Try to get stock item object from event

$stockItem = $observer->getEvent()->getStockItem();
2
  • hi, I did this and i got this error "Call to a member function getQty() on a non-object" – S. Orrichio Mar 3 '14 at 19:32
  • Then run var_dump($observer) and investigate what is in the object. – oleksii.svarychevskyi Mar 3 '14 at 19:33
1

If you check the _afterSave method in Mage_Core_Model_Abstract that is triggered you can see that the current object is passed into the event dispatcher as 'object': Mage::dispatchEvent('model_save_after', array('object'=>$this));.

This means you can get that object via $observer->getEvent()->getObject() in your own observer.

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.