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.