1

I'm trying to send an email to notify us if a specific sku is ordered. I've created the following module, but I'm not receiving the email.

app/code/local/MyCompany/MyModule/etc/config.xml:

<?xml version="1.0"?>
<config>
<modules>
    <MyCompany_MyModule>
        <version>0.1.0</version>   
    </MyCompany_MyModule>
</modules>

<adminhtml>
    <models>
        <mycompanymymodule>
            <class>MyModule_Model</class>
        </mycompanymymodule>
    </models>
    <events>
        <sales_order_save_after>
            <observers>
                <mycompany_mymodule_model_observer>
                    <type>singleton</type>
                    <class>MyCompany_MyModule_Model_Observer</class>
                    <method>sendEmail</method>
                </mycompany_mymodule_model_observer>
            </observers>
        </sales_order_save_after>
    </events>
</adminhtml>
</config>

app/code/local/MyCompany/MyModule/Model/Observer.php:

class MyCompany_MyModule_Model_Observer 
{
    public function sendEmail($observer) {
        $orderId = $observer->getEvent()->getOrder()->getId();
        $order = Mage::getModel('sales/order')->load($orderId);
        $itemCollection = $order->getItemsCollection();
        foreach($itemCollection as $item) {
        $product = Mage::getModel('catalog/product')->load($item->getProductId());
        if($product->getSku() == 'SKU') {
             $mail = Mage::getModel('core/email');
             $mail->setToName('Your name');
             $mail->setToEmail('myemail@mycompany.com');
             $mail->setBody('Order number '.$order->getIncrementId().' has items that need action');
             $mail->setSubject('Order '.$order->getIncrementId().' needs attention');
             $mail->setFromName('Your from name');
             $mail->setFromEmail('customerservice@mycompany.com');
             $mail->setType('text');

                try {
                    $mail->send();
                } catch (Exception $e) {
                    Mage::log($e);
                }
            }
        }
    }
}

I checked my error log file but not seeing anything. I've also sales_order_save_after for my event observer.

I still get the order confirmation email after ordering that particular sku but not the extra notification email. Not sure where I went wrong?

EDIT

I now have this working, I've edited the code above with proper changes. However, now I'm receiving my notification email twice instead of just one time.

  • How to use district in if condition? – zus Aug 22 '19 at 12:37
0

I was able to solve the duplicate email by changing my observer FROM sales_order_save_after TO sales_order_place_after

My final code looks like this

app/code/local/MyCompany/MyModule/etc/config.xml:

<?xml version="1.0"?>
<config>
<modules>
    <MyCompany_MyModule>
        <version>0.1.0</version>   
    </MyCompany_MyModule>
</modules>

<adminhtml>
    <models>
        <mycompanymymodule>
            <class>MyModule_Model</class>
        </mycompanymymodule>
    </models>
    <events>
        <sales_order_place_after>
            <observers>
                <mycompany_mymodule_model_observer>
                    <type>singleton</type>
                    <class>MyCompany_MyModule_Model_Observer</class>
                    <method>sendEmail</method>
                </mycompany_mymodule_model_observer>
            </observers>
        </sales_order_place_after>
    </events>
</adminhtml>
</config>

app/code/local/MyCompany/MyModule/Model/Observer.php:

class MyCompany_MyModule_Model_Observer 
{
    public function sendEmail($observer) {
        $orderId = $observer->getEvent()->getOrder()->getId();
        $order = Mage::getModel('sales/order')->load($orderId);
        $itemCollection = $order->getItemsCollection();
        foreach($itemCollection as $item) {
        $product = Mage::getModel('catalog/product')->load($item->getProductId());
        if($product->getSku() == 'SKU') {
             $mail = Mage::getModel('core/email');
             $mail->setToName('Your name');
             $mail->setToEmail('myemail@mycompany.com');
             $mail->setBody('Order number '.$order->getIncrementId().' has items that need action');
             $mail->setSubject('Order '.$order->getIncrementId().' needs attention');
             $mail->setFromName('Your from name');
             $mail->setFromEmail('customerservice@mycompany.com');
             $mail->setType('text');

                try {
                    $mail->send();
                } catch (Exception $e) {
                    Mage::log($e);
                }
            }
        }
    }
}
0

You need change scope from <adminhtml> to <global>.

Edit:

If you want to fire mail only when order place,then you can use any of below events which are fire one time for everyOrder

You have done some mistake.

  • Model have define for admin area <adminhtml> create.It should be for global area <global>.. <global> <!-- It should be global from adminhml --> <models> <mycompanymymodule> <class>MyModule_Model</class> </mycompanymymodule> </models> ... </global> <!-- It should be global from adminhml -->

  • As you have define,the observer for admin area that it have only firing for admin section that mean if you do anything to an Order from backend/admin then mail will fire

            <events>
                <sales_order_save_after>
                    <observers>
                        <mycompany_mymodule_model_observer>
                            <type>singleton</type>
                            <class>MyCompany_MyModule_Model_Observer</class>
                            <method>sendEmail</method>
                        </mycompany_mymodule_model_observer>
                    </observers>
                </sales_order_save_after>
            </events>
        </global>
    

For duplicated mail on same sku:

Reason:

  • As you have using sales_order_save_after event then observer will run several times because of whenever save() of Mage_Sales_Model_Order will call,your mail is fired.
  • If a configurable product id is place then as per as your code two mails will fire be for single configurable products.Beause of when a configurable product place then two rows will be insert at sales_order_item.

    One have Parent product id & child product sku

    Another is Child product id & child product sku

    So,you need to prevent it by

    if ($item->isDeleted() || $item->getParentItemId()) {
    continue; //
     $product = Mage::getModel('catalog/product')->load($item->getProductId());
    
  • Thanks Amit! So this particular sku is a simple product and is not visible on the front-end, orders for this sku can only be placed from the admin, so that's why I'm using the adminhtml scope. I went ahead and tried global and have the same result of 2 emails, is there a different observer I should be using to only send once when the order is placed from the admin? – AJ47 Jan 28 '16 at 16:42

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.