3

I want to send an email to customer after placing an order in Magento 2.

Please send/tell me how to extend the functionality in Magento 2

Edit:

I have created my new module Roshan_Checkout inside app/code and in etc folder I created a event.xml file and call a method from there which is defined in my observer.

But, I'm not able to reach there in my observer and don't know how to extend the checkout functionality.

Would be greatful to you if you tell the step by step procedure to do the same. Thanks in advanced.

Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83

4 Answers4

1

In order to use the observer, specify the folder frontend or adminhtml in etc folder and inside that use events.xml then call the observer. You can get the order details in observer using $observer->getOrderIds();.

Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83
sabarivel
  • 59
  • 7
1

If you want to observe ONLY the frontend for placing order then you can you the event checkout_onepage_controller_success_action in etc/frontend/events.xml

<event name="checkout_onepage_controller_success_action">
    <observer name="vendor_module_send_email" instance="Vendor\Module\Observer\SendEmail"  />
</event>

Note: This is not going to work if you have custom checkout extension which uses different controller name or you are redirecting customers to payment gateways pages. In that case you can observe sales_order_place_after in both frontend and adminhtml because this event is triggered from the Order model Magento\Sales\Model\Order in place() method.

For sending emails you can search in Google and find some tutorial for sending emails from controller and you just need to move the logic to helper and use it in the observer.

Miroslav Petroff
  • 1,842
  • 1
  • 16
  • 22
1

Use the event "checkout_onepage_controller_success_action" and in the observer use|inject transport builder class to send an email.

sabarivel
  • 59
  • 7
0

Use the event "sales_model_service_quote_submit_success" in the file Module/etc/frontend/events.xml with content :

<event name="sales_model_service_quote_submit_success">
    <observer name="sendNewEmail" instance="Compay\Module\Observer\QuoteSubmitSuccess" />
</event>

In function execute() you can get order detail :

public function execute(\Magento\Framework\Event\Observer $observer)
{
    /** @var \Magento\Sales\Model\Order $order */
    $order = $observer->getEvent()->getOrder();
    // code send email here
}

Then write your code for send email with order detail

Hope it helps !

Magetop E-commerce
  • 632
  • 1
  • 5
  • 26