Custom checkout cart – How to send email after successful checkout

Featured Image

Recently I have been working on a custom checkout page for one of our clients in Sweden. I had some trouble figuring out how to send default Magento order email with all of the order info. After an hour or so of studying Magento core code, here is the solution on how to send email after successful order has been made.

< ?php
$order = new Mage_Sales_Model_Order();
$incrementId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order->loadByIncrementId($incrementId);
try
{
$order->sendNewOrderEmail();
} catch (Exception $ex) {  }
?>

Not sure how useful this alone will be for you, so I’ll throw a little advice along the way. When trying to figure how to reuse Magento code, separate some time to study the Model classes with more detail. Then “tapping into” and reusing some of them should be far more easier.

Cheers…

18
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

CTO / Magento Certified Developer Plus @inchoo. Father, husband!

Other posts from this author

Discussion 18 Comments

Add Comment
  1. Thanks! You wouldn’t know how i can include transaction ID from DIBS which is stored in sales_order_entity_text – as far as i can see – into the email-template?

  2. Hi,
    I am having problem using Buckaroo from opensphere.nl , I does not send new order email. After I create the invoice then it sends an email but not the first new order email. Pls help.

    Here is the code:

    /**
    * Buckaroo Standard Checkout Controller
    *
    */
    class Mage_Buckaroo_StandardController extends Mage_Core_Controller_Front_Action
    {
    /**
    * Order instance
    */
    protected $_order;

    //
    // Flag only used for callback
    // protected $_callbackAction = false;

    protected function _expireAjax()
    {
    if (!Mage::getSingleton(‘checkout/session’)->getQuote()->hasItems()) {
    $this->getResponse()->setHeader(‘HTTP/1.1′,’403 Session Expired’);
    exit;
    }
    }

    /**
    * Get singleton with buckaroo strandard order transaction information
    *
    * @return Mage_Buckaroo_Model_Standard
    */
    public function getStandard()
    {
    return Mage::getSingleton(‘buckaroo/standard’);
    }

    /**
    * When a customer chooses Buckaroo on Checkout/Payment page
    *
    */
    public function redirectAction()
    {

    $session = Mage::getSingleton(‘checkout/session’);
    $session->setBuckarooStandardQuoteId($session->getQuoteId());

    $this->loadLayout();
    $this->getLayout()->getBlock(‘content’)->append($this->getLayout()->createBlock(‘buckaroo/standard_redirect’));
    $this->renderLayout();

    }

    /**
    * When a customer cancel payment from buckaroo.
    */
    public function cancelAction()
    {
    $session = Mage::getSingleton(‘checkout/session’);
    $session->setQuoteId($session->getBuckarooStandardQuoteId(true));
    $this->_redirect(‘checkout/cart’);
    }

    /**
    * when buckaroo returns
    * The order information at this point is in POST
    * variables. However, you don’t want to “process” the order until you
    * get validation from the IPN.
    */
    public function successAction()
    {
    $session = Mage::getSingleton(‘checkout/session’);
    $session->setQuoteId($session->getPaypalStandardQuoteId(true));
    /**
    * set the quote as inactive after back from paypal
    */
    Mage::getSingleton(‘checkout/session’)->getQuote()->setIsActive(false)->save();

    /**
    * send confirmation email to customer
    */
    $order = Mage::getModel(‘sales/order’);

    $order->load(Mage::getSingleton(‘checkout/session’)->getLastOrderId());
    if($order->getId()){
    $order->sendNewOrderEmail();
    }

    //Mage::getSingleton(‘checkout/session’)->unsQuoteId();

    $this->_redirect(‘checkout/onepage/success’, array(‘_secure’=>true));
    }

    //
    // When callback is called from buckaroo
    // just reflect to the success action
    //
    /* public function callbackAction()
    {
    $this->_callbackAction = true;
    $this->successAction();
    }
    */
    ///////////////////////////////////////////////////////////////////////
    // functions to create the error page
    ///////////////////////////////////////////////////////////////////////
    /**
    * Get order
    *
    * @param none
    * @return Mage_Sales_Model_Order
    */
    public function getOrder()
    {
    if ($this->_order == null) {
    $session = Mage::getSingleton(‘checkout/session’);
    $this->_order = Mage::getModel(‘sales/order’);
    $this->_order->loadByIncrementId($session->getLastRealOrderId());
    }
    return $this->_order;
    }

    public function errorAction()
    {
    $session = Mage::getSingleton(‘checkout/session’);
    $errorMsg = Mage::helper(‘buckaroo’)->__(‘ An error occured during the paying process.’); //this is not dispalyed in the front end it all lies in the phtml file

    $order = $this->getOrder();

    if (!$order->getId()) {
    $this->norouteAction();
    return;
    }
    if ($order instanceof Mage_Sales_Model_Order && $order->getId()) {
    $order->addStatusToHistory(
    $order->getStatus(),
    Mage::helper(‘buckaroo’)->__(‘Customer returned from Buckaroo.’) . $errorMsg
    );

    $order->save();
    }

    $this->loadLayout();
    $this->renderLayout();
    Mage::getSingleton(‘checkout/session’)->unsLastRealOrderId();
    //’checkout/onepage/success’
    //$this->_redirect(‘checkout/onepage/error’);
    //return $errorMsg;
    //echo ‘there was an error processing your order’;
    }

    }

  3. Juan Garberi

    Where can I edit this code in Magento, sorry totally newbie here!

  4. I’d really like to add this to my site but not sure where or what file It needs to be added to. Branko, could you give us an idea please?

  5. @Trev

    This code is written for Magento 1.2. Recently I had an upgrade of the same site I implemented this code to latest Magento 1.3.2.4, and it seems to be working OK. To reply on the “what file It needs to be added to”… you can add it to any custom made view file and just call it like core/template type of block.

    Cheers.

  6. Is it possible to send “thank you” e-mail after 2 weeks of purchase?
    How could that be done?

  7. ana hare

    Thank you for that. I added it to the “checkout/onepage/success.phtml” at the top, and finally my TPV payment gateway sends an email to the client.

    I still can’t get it to send me, the admin , an email, but this is great progress.

    One question.. the word ‘try’ in the code, is that a PHP expression, or a comment?

    Thanks again for saving my weekend.

  8. Another job well done by Branko…..

    The ‘try’ and ‘catch’ blocks are a way in php to handle exceptions,to avoid unpredictable behavior in case of an error .Thus , these are not comments .Comments are always within // or /* */.

  9. ana hare

    Hi Mohit, thanks for explaining that… as you can tell I am only a hacker, not a programmer. :-)

  10. Thanks for this work! Really helpful!

    How could I adapt this code to send the invoice as attached file in the mail?

  11. vasanth

    very helpful to me…thanks a lot :)

  12. Amatya Trivedi

    Very helpful code………..

    Branko,could you please tell me that how can I send a email on order failure.

  13. Nice post. You saved me one more time. =]

  14. MJ

    Hi Guy,

    Above code under which page I need to add?

    I am using Magento 1.4.1.

    Thank you

  15. Zard

    Hi Branko,

    Thank you for your post, it was useful to me.
    It works fine on Mangeto 1.5.1

    Not sure on which page I had to put the code.
    I put it on the controller of my custom payment method juste before the redirection on the success page.

  16. mahesh

    Hi

    Can you post an article for

    1) Send a email for Pending Cart
    OR
    2) Failed transactions/checkout process

  17. bernt

    I use a program that solves the problem with paypal autoresponder with product purchase, I have used it for a while and it works fine. You can find it here http://www.addinsoftware.com hope it helps you too

  18. vasanth

    how can save the order in the custom payment.
    Is this correct?Order is not stored and i used the code in my response function.

    $order = Mage::getModel(‘sales/order’);
    $order_id = Mage::getSingleton(‘checkout/session’)->getLastRealOrderId();
    $order->loadByIncrementId($order_id);
    $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, ‘Gateway has authorized the

    payment.’);
    $order->sendNewOrderEmail();
    $order->setEmailSent(true);
    :( please give me solun

    Thnx
    vasa

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top