1

I'm trying to send a notification when the product becomes out of stock. This is the code I have on my Observer:

public function detectProductChanges($observer)
{
    $product = $observer->getEvent()->getProduct();

    if ($product->hasDataChanges()) {

        try {
            $oldStock = $product->getStockItem()->getOrigData('qty');
            $newStock = $product->getStockItem()->getData('qty');

            if ($newStock != '0' || $oldStock == $newStock) {
                return $this;
            }

            // send email...

        } catch (Exception $e) {
            Mage::log($e->getTraceAsString(), null, 'my_cute.log');
        }
    }

    return $this;
}

On the config.xml file I got the following:

<!-- ... -->
  <global>
    <events>
      <catalog_product_save_after>
        <observers>
          <reports_detect_product_changes>
            <type>singleton</type>
            <class>reports/observer</class>
            <method>detectProductChanges</method>
          </reports_detect_product_changes>
        </observers>
      </catalog_product_save_after>
    </events>
  </global>
<!-- ... -->

The thing is, if I edit a product on the admin side, everything's fine, but if I proceed to checkout a product, this event is not called, can you help please?

1 Answer 1

1

You also need to observe the event checkout_submit_all_after. That one is dispatched when an order is placed. You should get as parameter the order placed and you can access the products ordered and check their qty changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.