0

I have a custom payment module. I want to stop sending the default Magento order confirmation email for this payment method only.

Please give some solutions...

What I have done so far:

I have created one plugin

app/code/Company/OrderEmail/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\Sales\Model\Order\Email\Container\OrderIdentity">
        <plugin name="change_is_enable_method" type="Company\OrderEmail\Plugin\Sales\Order\Email\Container\OrderIdentityPlugin"/>
    </type>
</config>

app/code/Company/OrderEmail/Plugin/Sales/Order/Email/Container/OrderIdentityPlugin.php

<?php

namespace Company\OrderEmail\Plugin\Sales\Order\Email\Container;

class OrderIdentityPlugin
{
    /**
     * @var \Magento\Checkout\Model\Session $checkoutSession
     */
    protected $checkoutSession;

    /**
     * @param \Magento\Checkout\Model\Session $checkoutSession
     *
     * @codeCoverageIgnore
     */
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    )
    {
        $this->checkoutSession = $checkoutSession;
    }

    /**
     * @param \Magento\Sales\Model\Order\Email\Container\OrderIdentity $subject
     * @param callable $proceed
     * @return bool
     */
    public function aroundIsEnabled(\Magento\Sales\Model\Order\Email\Container\OrderIdentity $subject, callable $proceed)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/v1.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('Your text message');

        $returnValue = $proceed();

        $paymentMethodCode = $this->checkoutSession->getQuote()->getPayment()->getMethod();

        $logger->info($paymentMethodCode);

        if($paymentMethodCode = "custom_paymentcode"){
            $forceOrderMailSentOnSuccess = $this->checkoutSession->getForceOrderMailSentOnSuccess();
            if(isset($forceOrderMailSentOnSuccess) && $forceOrderMailSentOnSuccess)
            {
                if($returnValue){
                    $returnValue = false;
                }
                else{
                    $returnValue = true;
                }     
                $this->checkoutSession->unsForceOrderMailSentOnSuccess();
            }

            return $returnValue;
        }
    }
}

But I am not getting the payment method from the checkout session which I have logged into the log file. So I Am not not able to check the condition.

0

Extending the event Manager

Magento2 has event manager which fires when a specific event is getting fired.

If you want to code less Please also see webhooks

inside Magento\vendor\temando\module-shipping-m2\etc\events.xml

events.xml

<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="email_order_set_template_vars_before">
        <observer name="check_payment_method_to_Order" 
            instance="[Vendor]\[ModuleName]\Observer\ObserverforPaymentMethod" />
    </event>
</config>

Observer file:

 <?php
    namespace [Vendor]\[ModuleName]\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\Request\DataPersistorInterface;
    use Magento\Framework\App\ObjectManager;

    class ObserverforPaymentMethodimplements ObserverInterface
    {

        public function __construct(
        ) {
        }

        /**
         *
         * @param \Magento\Framework\Event\Observer $observer
         * @return void
         */
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            /***    Check your payment method here ***/
            //get order 
            $order = Mage::getModel("sales/order")->load($orderId);
            // get payment method
            $payment_method_code = $order->getPayment()->getMethodInstance()->getCode(); 
            //Fetch the payment method code from order


        }
    }

Other events to focus on

checkout_onepage_controller_success_action

| improve this answer | |
  • After checking payment method, how can I send the mail. – Vindhuja Oct 29 '19 at 10:10

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.