3

How can I setup the Email Notification for the Low Stock Products and Expired Products?

2

Unfortunately Magento does not support email notifications for out of stock products.

However, there is an RSS feed available out of the box in the backend to be notified about low stock products:

enter image description here

To get notified via email, you can either develop your own custom module (which could be long and complex) or you can use a 3rd party service that automatically sends an email based on a RSS feed such as https://blogtrottr.com/ for example

2

You can create a module that has an observer and depending on where you want to check the inventory, observer those events, like product_save_after and checkout_submit_all_after events.

An observer would look like something below:

In config.xml:

      <events>
        <checkout_submit_all_after>
            <observers>
                <lowstock>
                    <class>lowstock/observer</class>
                    <method>sendLowStockEmailCheckout</method>
                </lowstock>
            </observers>
        </checkout_submit_all_after>
     <events>

In Observer.php

<?php

class Vendor_Lowstockalert_Model_Observer
{

    public function sendLowStockEmailCheckout(Varien_Event_Observer $observer)
    {
        $quote = $observer->getEvent()->getQuote();
        $items = array();
        foreach ($quote->getAllItems() as $item) {
            $productId  = $item->getProductId();

            if (!$productId) {
                continue;
            }
            $product = Mage::getModel('catalog/product')->load($productId);

            if ($product) {
                $stockItem = $product->getStockItem();
                $items[$productId] = array(
                    'item' => $stockItem,
                    'qty'  => $stockItem->getQty()
                );
            }

            if (count($items) > 0) {
                if ($items[$productId]['qty'] <= $items[$productId]['item']->getMinQty()) {
                    $storeId = Mage::app()->getStore()->getId();
                    $templateId = 'low_stock';

                    $emailTemplate = Mage::getModel('core/email_template')->loadByCode($templateId);
                    $vars = array('product_name' => $items[$productId]['item']->getProductName(), 'qty' => $items[$productId]['qty']);

                    $emailTemplate->setSenderEmail(Mage::getStoreConfig('trans_email/ident_general/email', $storeId));

                    $emailTemplate->setSenderName(Mage::getStoreConfig('trans_email/ident_general/name', $storeId));

                    try {
                        $emailTemplate->send(Mage::getStoreConfig('trans_email/ident_general/email', $storeId), Mage::getStoreConfig('trans_email/ident_general/name', $storeId), $vars);
                    } catch (\Exception $e) {
                        Mage::log("Unable to complete. Error: " . $e->getMessage());
                    }
                }
            }
        }
    }
}

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.