1

I am developing a solution with an external payment gateway. I create the order and then send the user to the gateway. When coming back from the gateway with a success i resend the order confirmation in my custom callback controller.

But the order confirmation email is sent once already when the order is first created. How do i intervene and stop this? I does not seem to find the correct event to do this in using an observer.

4 Answers 4

0

I think a better approach would be to add any additional functionality that your are trying to achieve to magento order email. By creating your own order email will limit functionality such as "resend order email".

But if you want continue take a look at send(Order $order, $forceSyncMode = false) in magento2/app/code/Magento/Sales/Model/Order/Email/Sender/OrderSender.php

In your custom module create etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="\Magento\Sales\Model\Order\Email\Sender\OrderSender" type="Company\ModuleName\Model\Order\Email\Sender\OrderSender"/>
</config>
11
  • Thanks! Should be able to solve it with this information.
    – nenne
    Feb 26, 2016 at 8:32
  • I am new to magento2 and developing a custom module for CCavenue payment gateway.I am facing the same issue.Please explain how you solve this issue
    – Renjith R
    Jan 13, 2017 at 7:25
  • @RenonStewart How to override the prepareTemplate function? i want to add a couple of additional data for email template Feb 10, 2017 at 3:32
  • @IdhamChoudry Since it is a protected method you can NOT use plugin, so you would have to use a preference to rewrite the class as shown in the example above. Sometimes you may need to extend the original class when rewriting class Feb 10, 2017 at 14:34
  • @RenonStewart : How to override the prepareTemplate() from vendor\magento\module-sales\Model\Order\Email\Sender.php function? i want to add a couple of additional data for email template. I have override using preference but its calling core prepareTemplate() method. Please help me. since, week I sucked here. Jun 7, 2017 at 12:04
1

Method 1->

You can disable the standard order confirmation email from Admin-

Stores > Configuration > Sales > Sales Email > Order.

Method 2->

Here $this->orderSender->send($order, true); called which extends abstract class method() which check if Order confirmation email is enabled from backend or not so we need create plugin for that

To create a plugin, you need to use di.xml.

[Package_Name]\[Module_Name]\etc\di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Model\Order\Email\Container\OrderIdentity">
        <plugin name="change_is_enable_method" type="\[Package_Name]\[Module_Name]\Plugin\Sales\Order\Email\Container\OrderIdentityPlugin"/>
    </type>
</config>

[Package_Name]\[Module_Name]\Plugin\Sales\Order\Email\Container\ OrderIdentityPlugin.php

<?php
    
    namespace [Package_Name]\[Module_Name]\Plugin\Sales\Order\Email\Container;
    
    class OrderIdentityPlugin
    {
        /**
         * @param \Magento\Sales\Model\Order\Email\Container\OrderIdentity $subject
         * @param callable $proceed
         * @return bool
         */
        public function aroundIsEnabled(\Magento\Sales\Model\Order\Email\Container\OrderIdentity $subject, callable $proceed)
        {
            $returnValue = $proceed();
    
            if(some condition)
            {
                if($returnValue)
                    $returnValue = false;
                else
                    $returnValue = true;
            }
    
            return $returnValue;
        }
    }
0

A better way to do this in newer versions of Magento 2 (2.2.7+) is to create an after plugin.

app/code/MagePal/DisableEmail/etc/frontend/di.xml

<type name="Magento\Sales\Model\Order\Email\Container\OrderIdentity">
    <plugin name="magePalDisableEmailPlugin" sortOrder="1"
            type="MagePal\DisableEmail\Plugin\Order\Email\Container\OrderIdentityPlugin"/>
</type>

app/code/MagePal/DisableEmail/Plugin/Order/Email/Container/OrderIdentityPlugin.php

Class OrderIdentityPlugin {

   public function __construct(
      .....

   ) {
      ...
   }

   public function afterIsEnabled(Magento\Sales\Model\Order\Email\Container\OrderIdentity $subject, $result)
   {
       if( some condition ){
           result = false;
       }

       return $result;
   }      

}
0

Simply create a event observer to do prevent the order confirmation email

<event name="sales_order_place_after">
      <observer name="Test_Module_event_email_stopper" instance="Test\Module\Observer\EmailStopper\OrderEmailStop" />
 </event>

OrderEmailStop.php file

<?php
namespace Test\Module\Observer\EmailStopper;

class OrderEmailStop implements \Magento\Framework\Event\ObserverInterface
{   
    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        try{
            $order = $observer->getEvent()->getOrder();
            $this->_current_order = $order;

            $payment = $order->getPayment()->getMethodInstance()->getCode();

            if($payment == 'Your Payament method code'){ // change your payement method code
                $this->stopNewOrderEmail($order);
            }
        }
        catch (\ErrorException $ee){

        }
        catch (\Exception $ex)
        {

        }
        catch (\Error $error){

        }

    }

    public function stopNewOrderEmail(\Magento\Sales\Model\Order $order){
        $order->setCanSendNewEmailFlag(false);
        $order->setSendEmail(false);
        try{
            $order->save();
        }
        catch (\ErrorException $ee){

        }
        catch (\Exception $ex)
        {

        }
        catch (\Error $error){

        }
    }
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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