Magento – Magento 2 – Place an order without sending email

magento2order-emailorderspayment-gateway

I'm using the following code to place an order programmatically in Magento 2:

    // Configure the quote
    $quote = $this->cart->getQuote();
    $quote->setInventoryProcessed(false);
    $quote->setPaymentMethod('custom_gateway');
    $quote->save();

    // Set the payment method
    $payment = $quote->getPayment();
    $payment->importData(['method' => 'custom_gateway']);

    // Update the quote
    $quote->collectTotals()->save();

    // Place the order
    $this->cartManagement->placeOrder($quote->getId());

This code is creating the order as expected, but is also sending an email to the shopper.

I would like to send the confirmation email at a later stage in the process, only when the payment has been confirmed.

How can I place an order without notifying the customer?

Best Answer

We can set the flag for order: $order->setCanSendNewEmailFlag(false)

We can see some good examples for online payments:

vendor/magento/module-quote/Observer/Webapi/SubmitObserver.php

vendor/magento/module-paypal/Model/Payflowlink.php
Related Topic