Question

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.

Was it helpful?

Solution

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>

OTHER TIPS

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;
        }
    }

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;
   }      

}

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){

        }
    }
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top