Magento Themes and Magento Extensions

Magento force sending emails after order successful

Share

Magento was not sending email notification to customer and magento web store owner after new order was placed. When I checked order in admin it was showing “Customer Notified” with a green tick but no one were receiving any notifications.

When I was updating order through admin, customer was getting notifications.

After a quick look, I couldn’t find the issue as there were so many custom extensions on this client and not sure if any of them was interfering. So, I decided to write an observer that will be called after every successful order placement.

file: /trunk/wholesale/app/code/local/Technooze/Checkout/etc/config.xml

01 <config>
02 ...
03 <global>
04 ...
05 <models>
06 <technooze_checkout>
07 <class>Technooze_Checkout_Model</class>
08 </technooze_checkout>
09 </models>
10 <events>
11 <checkout_onepage_controller_success_action>
12 <observers>
13 <sendOrderEmails>
14 <type>singleton</type>
15 <class>Technooze_Checkout_Model_Observer</class>
16 <method>sendOrderEmails</method>
17 </sendOrderEmails>
18 </observers>
19 </checkout_onepage_controller_success_action>
20 </events>
21 </global>
22 </config>

file: /trunk/wholesale/app/code/local/Technooze/Checkout/Model/Observer.php

01 <?php
02 class Technooze_Checkout_Model_Observer
03 {
04 public function sendOrderEmails(Varien_Event_Observer $observer){
05 $orderIds = $observer->getData('order_ids');
06 foreach($orderIds as $_orderId){
07 $order = Mage::getModel('sales/order')->load($_orderId);
08 try {
09 $order->sendNewOrderEmail();
10 Mage::log('email sent');
11 } catch (Exception $e) {
12 Mage::logException($e);
13 }
14 }
15 return $this;
16 }
17 }

Added store owner’s email address at:

admin / system / configuration / SALES / Sales Emails / Order /

Send Order Email Copy To: owner’s email

Send Order Email Copy To: Bcc or Separate Email

Deleted cache and test. I was able to finally get emails on every new order placed. So the issue is solved. Below files and functions helped me to resolve this issue:

file: /trunk/wholesale/app/code/core/Mage/Checkout/Model/Type/Onepage.php

1 public function saveOrder(){... $order->sendNewOrderEmail(); ...}

file: /trunk/wholesale/app/code/core/Mage/Checkout/controllers/OnepageController.php

1 public function successAction(){... Mage::dispatchEvent('checkout_onepage_controller_success_action',array('order_ids' => array($lastOrderId))); ...}

file: /trunk/wholesale/app/code/core/Mage/Sales/Model/Order.php

01 /**
02 * Sending email with order data
03 *
04 * @return Mage_Sales_Model_Order
05 */
06 public function sendNewOrderEmail()
07 {
08 if (!Mage::helper('sales')->canSendNewOrderEmail($this->getStore()->getId())) {
09 return $this;
10 }
11
12 $translate = Mage::getSingleton('core/translate');
13 /* @var $translate Mage_Core_Model_Translate */
14 $translate->setTranslateInline(false);
15
16 $paymentBlock = Mage::helper('payment')->getInfoBlock($this->getPayment())
17 ->setIsSecureMode(true);
18
19 $paymentBlock->getMethod()->setStore($this->getStore()->getId());
20
21 $mailTemplate = Mage::getModel('core/email_template');
22 /* @var $mailTemplate Mage_Core_Model_Email_Template */
23 $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
24 $copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $this->getStoreId());
25 if ($copyTo && $copyMethod == 'bcc') {
26 foreach ($copyTo as $email) {
27 $mailTemplate->addBcc($email);
28 }
29 }
30
31 if ($this->getCustomerIsGuest()) {
32 $template = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE,$this->getStoreId());
33 $customerName = $this->getBillingAddress()->getName();
34 } else {
35 $template = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $this->getStoreId());
36 $customerName = $this->getCustomerName();
37 }
38
39 $sendTo = array(
40 array(
41 'email' => $this->getCustomerEmail(),
42 'name' => $customerName
43 )
44 );
45 if ($copyTo && $copyMethod == 'copy') {
46 foreach ($copyTo as $email) {
47 $sendTo[] = array(
48 'email' => $email,
49 'name' => null
50 );
51 }
52 }
53
54 foreach ($sendTo as $recipient) {
55 $mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$this->getStoreId()))
56 ->sendTransactional(
57 $template,
58 Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $this->getStoreId()),
59 $recipient['email'],
60 $recipient['name'],
61 array(
62 'order' => $this,
63 'billing' => $this->getBillingAddress(),
64 'payment_html' => $paymentBlock->toHtml(),
65 )
66 );
67 }
68 $this->setEmailSent(true);
69 $this->_getResource()->saveAttribute($this, 'email_sent');
70 $translate->setTranslateInline(true);
71
72 return $this;
73 }

Comments are closed.