1

I am creating a order programatically in magento 2. What property I need to set on $order object so order success email will not trigger. I have tried below methods with (bool) false, parameter but no succes.

$order->setCanSendNewEmailFlag(false);
$order->setEmailSent(false);
$order->setSendEmail(false);
1

3 Answers 3

1

A better way to do this in newer versions (2.2.7+) is to use a 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;
   }      

}
2
  • 1
    Thanks, It worked as expected. Commented Oct 19, 2019 at 12:08
  • How can the payment method or order information be retrieved in the afterIsEnabled function? thank you in advanced.
    – Rayed
    Commented Nov 8, 2021 at 22:15
3

You can do that by overiding the

send(Order $order, $forceSyncMode = false)

In

vendor/magento/module-sales/Model/Order/Email/Sender/OrderSender.php
2

You can see here at below path :

vendor/magento/module-sales/Model/Order/Email/Sender/OrderSender.php

You can override this public function send(Order $order, $forceSyncMode = false) function

Or

You can set from admin (It's better) to disable it instead of programmatically :

Admin -> Stores -> Configuration -> Sales -> Sales Email -> Order -> Enabled

Hope, It will helpful for you.

1
  • I need to do that for a specific order that I am creating programatically. It will disable for all orders, that's something not required here. Commented Sep 8, 2018 at 11:25

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.