0

I'm using magento 2.2 with topstore 4th theme. Sending mail after order success observer are not working. i don't know why? Is there any possibility to send mail from success.phtml page manually to custom mail ids and customer id in magento. I need to send it and need to update order mail as sent in the dashboard.

Here my code app/code/Vendor/Module/etc/frontend/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="\Vendor\Module\Plugin\Sales\Order\Email\Container\OrderIdentityPlugin"/>
    </type>
</config>

app/code/Vendor/Module/etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="checkout_onepage_controller_success_action">
       <observer name="process_gateway_redirect" instance="Vendor\Module\Observer\EmailSenderAfterPayment" />
   </event>
</config>

app/code/Vendor/Module/Observer/EmailSenderAfterPayment.php

<?php

    /**
     * Copyright © 2016 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */
    namespace Vendor\Module\Observer;

    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Store\Model\StoreManagerInterface;

    class EmailSenderAfterPayment implements ObserverInterface
    {
        /**
         * @param Observer $observer
         * @return void
         */
        private $storeManager;
        protected $_checkoutSession;
        protected $sender;  
        protected $order;   

        public function __construct(
        StoreManagerInterface $storeManager,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\Order\Email\Sender\OrderSender $sender,
        \Magento\Sales\Model\Order $order

        ) {
            $this->storeManager = $storeManager;
            $this->_checkoutSession = $checkoutSession;
            $this->sender = $sender;
            $this->order = $order;

        }

        public function execute(\Magento\Framework\Event\Observer $observer)
        {
           $orderIds = $observer->getOrderIds();
           $order = $this->order->load($orderIds[0]);
           $this->sender->send($order);
           return;
        }
    }

app/code/Vendor/Module/Plugin/Sales/Order/Order/Email/Container/OrderIdentityPlugin.php

<?php

namespace Vendor\Module\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)
    {
        $returnValue = $proceed();

        $forceOrderMailSentOnSuccess = $this->checkoutSession->getForceOrderMailSentOnSuccess();
        if(isset($forceOrderMailSentOnSuccess) && $forceOrderMailSentOnSuccess)
        {
            if($returnValue)
                $returnValue = false;
            else
                $returnValue = true;

            $this->checkoutSession->unsForceOrderMailSentOnSuccess();
        }

        return $returnValue;
    }
}

All of modules in the theme are located inside vendor/magento/ folder

Is there any possibility to send the mail if its not work. I'm using Payu Payment gateway. I want to send mail from block i need a function to send order mail. Please help me to do this.

  • observer not working means? post your code here so people can understand. – Keyur Shah Jan 21 '18 at 9:03
  • Codes updated. Please check!! – Tamil Selvan Jan 21 '18 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.