Custom checkout cart – How to send email after successful checkout

Custom checkout cart – How to send email after successful checkout

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…

You made it all the way down here so you must have enjoyed this post! You may also like:

How To Connect Google Analytics 4 To Magento 2 Bojan Mareljic
Bojan Mareljic, | 36

How To Connect Google Analytics 4 To Magento 2

Meet Magento Belarus features an Inchooer talking Magento 2 Checkout Ivona Namjesnik
Ivona Namjesnik, | 0

Meet Magento Belarus features an Inchooer talking Magento 2 Checkout

Tracking Onepage Checkout abandonment using Google Analytics Drazen Karacic-Soljic
Drazen Karacic-Soljic, | 70

Tracking Onepage Checkout abandonment using Google Analytics

23 comments

  1. Hi Branko Ajzele ,

    I want to send email after successful checkout

    getLastRealOrderId();
    $order->loadByIncrementId($incrementId);
    try
    {
    $order->sendNewOrderEmail();
    } catch (Exception $ex) { }
    ?>

    in this code which file i will paste please give the folder structure and file name

  2. 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

  3. Hi

    Can you post an article for

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

  4. 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.

  5. Very helpful code………..

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

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

  7. 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 /* */.

  8. 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.

  9. @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.

  10. 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?

  11. 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’;
    }

    }

  12. 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?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.