Question

Here I have created a plugin to stop the order mail notification for some specific payment. This plugin is working fine on my localhost but not working on server.

My di.xml file codes:

<?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="stop_order_email_for_some_payment" type="CompanyName\ModuleName\Plugin\Quote\Observer\SubmitObserver" sortOrder="0" />
        </type> 
</config>

And plugin file (CompanyName/ModuleName/Plugin/Quote/Observer/SubmitObserver.php) codes:

namespace CompanyName\ModuleName\Plugin\Quote\Observer;

class SubmitObserver
{
    public function beforeExecute(
        \Magento\Quote\Observer\SubmitObserver $subject,
        \Magento\Framework\Event\Observer $observer
    ) {
        /** @var  \Magento\Sales\Model\Order $order */
        $order = $observer->getEvent()->getOrder();
        $paymentCode = $order->getPayment()->getMethodInstance()->getCode();
        $orderStatus = $order->getStatus();
        
         if($paymentCode == 'swed_m2s' || $paymentCode == 'seb_m2s' || $paymentCode == 'lhv_m2s' || $paymentCode == 'estcard_m2s' || $paymentCode == 'coop_m2s')
            {
                if($orderStatus == 'pending')
                {
                     $order->setCanSendNewEmailFlag(false);
                }
            }
       
        return [$observer];
    }
}

On local System both column email_sent, send_email of sales_order table are updating with NULL value according to condition and also not getting order mail notification for selected payment methods which are checked in condition.

But on server neither columns are updating nor order mail notification is stopping.

Can anyone help me with this problem, why my plugin is not working on server.

Thanks!

Was it helpful?

Solution

Why are you make plugin for observer? You can use plugin for this : vendor/magento/module-sales/Model/Order/Email/Sender/OrderSender.php

OR Verify the status of order!!! If invoice automatically created then status is different!!! Are you getting my point?

OR Create preference

<?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="Venodr\ModuleRewrite\Model\Order\Email\Sender\OrderSender" />
</config>

And Modify only this function.

public function send(Order $order, $forceSyncMode = false)
    {
        $payment = $order->getPayment()->getMethodInstance()->getCode();
        $orderStatus = $order->getStatus();
        if($paymentCode == 'swed_m2s' || $paymentCode == 'seb_m2s' || $paymentCode == 'lhv_m2s' || $paymentCode == 'estcard_m2s' || $paymentCode == 'coop_m2s')
        {
            if($orderStatus == 'pending')
            {
                 return false;
            }
        }

        $order->setSendEmail($this->identityContainer->isEnabled());

        if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
            if ($this->checkAndSend($order)) {
                $order->setEmailSent(true);
                $this->orderResource->saveAttribute($order, ['send_email', 'email_sent']);
                return true;
            }
        } else {
            $order->setEmailSent(null);
            $this->orderResource->saveAttribute($order, 'email_sent');
        }

        $this->orderResource->saveAttribute($order, 'send_email');

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