2

I am trying to achieve the following:

When an item is Out of Stock send an E-Mail Notification to the warehouse. First question would be: what's the easiest and cleanest way to achieve this?!

My approach was:

I wrote an observer which fires on the sales_order_save_after event. So basically after every order all Items in this order should be checked wether these are still in Stock after the order.

The Observer.php looks like this:

public function checkStock($observer)
{
    $active = Mage::getStoreConfig('chimney_notifystock/general/notification_enabled');
    if ($active == 1){

        $order = $observer->getEvent()->getOrder();

        foreach ($order->getAllItems() as $item) {

            $product = $item->getProduct();
            $stockItem = $product->getStockItem();

            if ($stockItem->getIsInStock() == 0) {
                $this->sendMail($product);
            }
        }
    }
}

Now the problem is that obviously on sales_order_save_after the value of is_in_stock isn't set to 0 yet. The getIsInStock() method always returns 1 so the sendMail() won't be executed. Everything else works fine and the e-mail is sent correctly when I set the
if ($stockItem->getIsInStock() == 0)
to
if ($stockItem->getIsInStock() == 1)

2

use event because it will called after the updating table data.

 sales_order_save_commit_after
  • unfortunately for some reason this still doesn't work. When I complete the order the Stock is reduced correctly and if the stock hits 0 the product is set to "out of stock" but when I request the "is in stock status" or the qty from within the sales_order_save_commit_after i still get the qty from before the order and the product isn't set to out of stock yet. So this must be happening somewehere later on – Chaz Nov 6 '18 at 14:13
  • @Chaz did you tried to get new object of product data? – PROGOSTECH Nov 6 '18 at 15:09
  • What do you mean? How do I do that? How do I have to adjust my Code? – Chaz Nov 6 '18 at 15:59
  • @Chaz $product = $item->getProductId(); you will get id here . load new product data with model and check you will get the updated data. maybe you get the out of stock data correctly. – PROGOSTECH Nov 6 '18 at 16:01
  • When I load the $stockItem like this the qty gets updated like you said. The "is in stock" status however isn't any ideas when this happens? Anyways I think I can work with it like that, thank you very much! – Chaz Nov 6 '18 at 16:38

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.