-1

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!

7
  • Why are you make plugin for observer? You can use plugin for this : vendor/magento/module-sales/Model/Order/Email/Sender/OrderSender.php Aug 21, 2020 at 16:58
  • Actually I checked this, it's working fine on my local environement that's why I used here. Aug 21, 2020 at 17:01
  • Add status in log : $orderStatus Aug 21, 2020 at 17:03
  • If invoice automatically created then status is different!!! Are you getting my point? Aug 21, 2020 at 17:03
  • I checked and getting both values in my log file Aug 21, 2020 at 17:04

1 Answer 1

2

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;
    }
9
  • Can you explain that how I use this class? Aug 21, 2020 at 17:07
  • You can create preference for that! 2min iw ill update my ansere Aug 21, 2020 at 17:13
  • Yes sure brother Aug 21, 2020 at 17:15
  • 1
    You can also use plugin !!!! Aug 21, 2020 at 17:18
  • Ok thanks brother for your effort, I will try it tomorrow and update you Aug 21, 2020 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.