2

I need to Custom send mail after place order with some custom value with order id

observer.php

 public function checkout_controller_onepage_save_shipping_method($observer)
        {
            if (Mage::getStoreConfig('deliverydate/deliverydate_general/on_which_page')==1){
                $request = $observer->getEvent()->getRequest();
     //           $quote =  $observer->getEvent()->getQuote();

        $quotes = Mage::getModel('sales/quote')->getCollection();

                $desiredArrivalDate = Mage::helper('deliverydate')->getFormatedDeliveryDateToSave($request->getPost('shipping_arrival_date', ''));
                if (isset($desiredArrivalDate) && !empty($desiredArrivalDate)){
                    $quotes->setShippingArrivalDate($desiredArrivalDate);
                    $quotes->setShippingArrivalComments($request->getPost('shipping_arrival_comments'));
                    $quotes->setShippingArrivalTime($request->getPost('shipping_arrival_time'));
                    $quotes->save();
                }
            }

            return $this;
        }

DB Field name shipping_arrival_comments
shipping_arrival_time
shipping_arrival_date
this 3 fields are storing in sales_flat_order table

My doubt is how to use this variable in in that mail How to achieve this?

  • I want to know is this fields are also available in order object. – Aman Alam Jun 16 '16 at 8:15
  • DB Field name only – Magento 2 Jun 16 '16 at 9:00
0

First you need to defin a order place after observer in a module.

<sales_order_place_after>
    <observers>
        <common_order_place_after> <!-- Any unique name for observer -->
            <type>singleton</type>
            <class>common/observer</class> <!-- in common/observer common is model name and observer is class name. -->
            <method>placeAfter</method> <!-- in placeAfter is a method in class observer that will execute just after placing order. -->
        </common_order_place_after>
    </observers>
</sales_order_place_after>

After that you should create write your logic in you module Namespace_Modulename_Model_Observer. As i seen you are setting data in quote it should available in order object after placing order so your should use below mentioned code to send mail after placing order.

<?php
class Namespace_Modulename_Model_Observer
{
    public function placeAfter($observer){
        $order = $observer->getEvent()->getOrder();

        //Start of mailing function
        $storeId = Mage::app()->getStore()->getId();
        $templateId = 1; //Id should be your transactional mail id.
        $mailSubject = 'Hello !! guest';

        $sender = array('name' => 'example shop', 'email' => 'example@shop-url.com');
        $email = 'customer@test.com'; //sent to
        $name = 'test';  //sent to name

        $mailTemplate = Mage::getModel('core/email_template');

        $vars = array('shipping_arrival_comments'=>$order->getShippingArrivalComments(),'shipping_arrival_time'=> $order->getShippingArrivalTime(),'shipping_arrival_date'=>$order->getShippingArrivalDate());


        $mailTemplate->setTemplateSubject($mailSubject)->sendTransactional(
           $templateId, //int $templateId
           $sender, //string|array $sender sneder informatio, can be declared as part of config path
           $email,  //string $email recipient email
           $name, // string $name recipient name
           $vars,  // array $vars varianles which can be used in template
           $storeId // int/null $storeId
        );
    }
}

After that you should call these above defined variable into your transactional mail like {{var shipping_arrival_comments}},{{var shipping_arrival_time}},{{var shipping_arrival_date}}

Note : As you are saving data in quote object to save this data in order you should have same field in sales_flat_order table so that data will save their. If these field data is not saving in flat order sales_flat_order then you should add below mentioned configration to your modul config.xml file in global tag.

<global>
    <fieldsets>
        <sales_convert_quote>                           
            <shipping_arrival_comments><to_order>*</to_order></shipping_arrival_comments>
        </sales_convert_quote>
        <sales_convert_order>                                              
            <shipping_arrival_comments><to_quote>*</to_quote></shipping_arrival_comments>
        </sales_convert_order>
        <sales_convert_quote>                           
            <shipping_arrival_time><to_order>*</to_order></shipping_arrival_time>
        </sales_convert_quote>
        <sales_convert_order>                                              
            <shipping_arrival_time><to_quote>*</to_quote></shipping_arrival_time>
        </sales_convert_order>
        <sales_convert_quote>                           
            <shipping_arrival_date><to_order>*</to_order></shipping_arrival_date>
        </sales_convert_quote>
        <sales_convert_order>                                              
            <shipping_arrival_date><to_quote>*</to_quote></shipping_arrival_date>
        </sales_convert_order>
    </fieldsets>
</global>

Hope this will help you !

|improve this answer|||||
  • How to get customer mail id & order id – Magento 2 Jun 16 '16 at 9:08
  • For customer email $order->getCustomerEmail(), if you need order id then you should use $order->getId() this will display order entity id if you need increment id then $order->getIncrementId() most probably your order id meant for order increment id – Aman Alam Jun 16 '16 at 9:15
  • Be carefull to attach long running observers to the sales_order_place_after event. They are processed before your customer gets the success page. Any exceptions not properly handled will get caught by the checkout controller, and may lead to false order failure notifications. Read some more here: github.com/ProxiBlue/OrderSyncQueRunner/blob/master/README.md – ProxiBlue Jun 16 '16 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.