Magento force sending emails after order successful

Posted by Damodar Bashyal on August 07, 2013

 

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

<config>
    ...
    <global>
		...
        <models>
            <technooze_checkout>
                <class>Technooze_Checkout_Model</class>
            </technooze_checkout>
        </models>
        <events>
            <checkout_onepage_controller_success_action>
                <observers>
                    <sendOrderEmails>
                        <type>singleton</type>
                        <class>Technooze_Checkout_Model_Observer</class>
                        <method>sendOrderEmails</method>
                    </sendOrderEmails>
                </observers>
            </checkout_onepage_controller_success_action>
        </events>
    </global>
</config>

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

<?php
class Technooze_Checkout_Model_Observer
{
    public function sendOrderEmails(Varien_Event_Observer $observer){
        $orderIds = $observer->getData('order_ids');
        foreach($orderIds as $_orderId){
            $order = Mage::getModel('sales/order')->load($_orderId);
            try {
                $order->sendNewOrderEmail();
                Mage::log('email sent');
            } catch (Exception $e) {
                Mage::logException($e);
            }
        }
        return $this;
    }
}

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

Configuration   System   Magento Admin

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

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

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

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

   /**
     * Sending email with order data
     *
     * @return Mage_Sales_Model_Order
     */
    public function sendNewOrderEmail()
    {
        if (!Mage::helper('sales')->canSendNewOrderEmail($this->getStore()->getId())) {
            return $this;
        }

        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $translate->setTranslateInline(false);

        $paymentBlock = Mage::helper('payment')->getInfoBlock($this->getPayment())
            ->setIsSecureMode(true);

        $paymentBlock->getMethod()->setStore($this->getStore()->getId());

        $mailTemplate = Mage::getModel('core/email_template');
        /* @var $mailTemplate Mage_Core_Model_Email_Template */
        $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
        $copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $this->getStoreId());
        if ($copyTo && $copyMethod == 'bcc') {
            foreach ($copyTo as $email) {
                $mailTemplate->addBcc($email);
            }
        }

        if ($this->getCustomerIsGuest()) {
            $template = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $this->getStoreId());
            $customerName = $this->getBillingAddress()->getName();
        } else {
            $template = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $this->getStoreId());
            $customerName = $this->getCustomerName();
        }

        $sendTo = array(
            array(
                'email' => $this->getCustomerEmail(),
                'name'  => $customerName
            )
        );
        if ($copyTo && $copyMethod == 'copy') {
            foreach ($copyTo as $email) {
                $sendTo[] = array(
                    'email' => $email,
                    'name'  => null
                );
            }
        }

        foreach ($sendTo as $recipient) {
            $mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$this->getStoreId()))
                ->sendTransactional(
                    $template,
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $this->getStoreId()),
                    $recipient['email'],
                    $recipient['name'],
                    array(
                        'order'         => $this,
                        'billing'       => $this->getBillingAddress(),
                        'payment_html'  => $paymentBlock->toHtml(),
                    )
                );
        }
        $this->setEmailSent(true);
        $this->_getResource()->saveAttribute($this, 'email_sent');
        $translate->setTranslateInline(true);

        return $this;
    }

Let me know if there is easier method.

davids posted on - Monday 2nd of June 2014 10:56:59 AM

I want to test this module on my staaging server so please provide me this

Kreshnik posted on - Tuesday 25th of March 2014 08:24:22 AM

Could you please a download link for this observer?

Whut posted on - Thursday 21st of July 2016 11:35:02 PM

You didn't solve the issue! You added duplicate functionality (a hacky workaround) which is just added code debt, rather than fixing the root cause of the issue!

If the root cause ever resolves itself, like when the problematic extension is uninstalled, customers will be getting 2 order emails.

devikajain posted on - Thursday 4th of August 2016 11:53:32 PM

Thanks for sharing blog such a nice information please tell me which is the best<a> Magento SMS gateway integration</a>.
 
not published on website


QR Code: Magento force sending emails after order successful