0

For some reason, I'm splitting my order in two part (the first is handle by some organization and the other will me handle by the magento owner).

I'm creating programmatically the order, and for one of the part, I need to stop the mail of the order to be sended.

Example : In quote : watch, Tv, video game. Splitting in two parts :

  1. watch, tv
  2. video games

Send email for the watch and tvs Not send mail for video game.

How can I do this ?

EDIT : i'm splitting my code by doing a foreach on the quote item. If the item is in a category, I create a array with those items.

Then I'm creating my order like this : https://www.rakeshjesadiya.com/how-to-create-order-programmatically-in-magento-2/

And i'm calling the method twice, one for the first part, the other for the second.

I've notice that the email is not sended for the second part if I have placed those 2 orders parts. That's ok I guess, but if the first part is not placed, the second part will send a mail.

3
  • Are you able to split the order base on this logic
    – Amit Bera
    Jun 5 '19 at 9:37
  • share the code which you used for split the order. Jun 5 '19 at 9:44
  • I've added an explanation and a link to my methods (my code is exactly the same) Jun 5 '19 at 9:55
2

Magento has an event to send email for order defined in following file.

vendor/magento/module-quote/etc/frontend/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_model_service_quote_submit_success">
        <observer name="sendEmail" instance="Magento\Quote\Observer\SubmitObserver" />
    </event>
</config>

If you will look into the SubmitObserver File, you will find below code.

public function execute(\Magento\Framework\Event\Observer $observer)
{
    /** @var  \Magento\Quote\Model\Quote $quote */
    $quote = $observer->getEvent()->getQuote();
    /** @var  \Magento\Sales\Model\Order $order */
    $order = $observer->getEvent()->getOrder();

    /**
     * a flag to set that there will be redirect to third party after confirmation
     */
    $redirectUrl = $quote->getPayment()->getOrderPlaceRedirectUrl();
    if (!$redirectUrl && $order->getCanSendNewEmailFlag()) {
        try {
            $this->orderSender->send($order);
        } catch (\Exception $e) {
            $this->logger->critical($e);
        }
    }
}

So to stop sending email, you can create a plugin in your module as follows.

app/code/{vendor}/{module}/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\Quote\Observer\SubmitObserver">
        <plugin name="custom_order_stop_email" type="{Vendor}\{Module}\Plugin\SubmitObserver" />
    </type>
</config>

{Vendor}/{Module}/Plugin/SubmitObserver.php

<?php

namespace {Vendor}\{Module}\Plugin;

class SubmitObserver
{
    public function beforeExecute($subject, $observer)
    {
        $order = $observer->getEvent()->getOrder();

        if ('Your product condition') {
            $order->setCanSendNewEmailFlag(false);
        }

        return [$observer];
    }
}

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.