2

we have an extension that sends out a new order e-mail to our own private mailbox. Which is different from the ones that customers get. We print these mails automatically on a receipt printer.

We use Magento 2.1.16.

But not all orders are being printed. Canceled and sometimes regular orders are not printed.

This extension is abandoned by its developer .... so I have to figure it out myself.

Can someone have a look at this? This mail should be sent out together with the sales order mail to the customer, and not only when the order is successful.

I think this file takes care of the sending ....

Observer/NewEmail.php

<?php

namespace Neworderemail\Adminemails\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Sales\Model\Order\Email\Container\OrderIdentity;
use Magento\Sales\Model\Order\Address\Renderer;

class NewEmail implements ObserverInterface {

    /**
     * @var \Magento\Framework\Translate\Inline\StateInterface
     */
    protected $inlineTranslation;

    /**
     * Store manager
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $_scopeConfig;

    /**
     * Get extension configuration helper
     * @var \Neworderemail\Adminemails\Helper\Config
     */
    public $dataHelper;

    /**
     * Pricing Helper
     * @var \Magento\Framework\Pricing\Helper\Data
     */
    public $curHelper;

    /**
     * Order sender
     */
    public $orderSender;

    /**
     * Sales order Model
     * @var \Magento\Sales\Model\Order
     */
    protected $order;

    /**
     * Initialize dependencies.
     *
     * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Neworderemail\Adminemails\Helper\Config $dataHelper
     */
    public function __construct(
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Sales\Model\Order $order,
        \Neworderemail\Adminemails\Helper\Data $dataHelper,
        \Magento\Framework\Pricing\Helper\Data $curHelper,
        PaymentHelper $paymentHelper,
        OrderIdentity $identityContainer,
        Renderer $addressRenderer

    ) {
        $this->inlineTranslation = $inlineTranslation;
        $this->_storeManager = $storeManager;
        $this->_transportBuilder = $transportBuilder;
        $this->_scopeConfig = $scopeConfig;
        $this->order = $order;
        $this->dataHelper = $dataHelper;
        $this->curHelper = $curHelper;
        $this->paymentHelper = $paymentHelper;
        $this->addressRenderer = $addressRenderer;
        $this->identityContainer = $identityContainer;
    }

     /**
     * Private method to send email
     */
    protected function _sendEmail($from, $to, $templateId, $vars, $store, $area = \Magento\Framework\App\Area::AREA_FRONTEND) {
        $this->inlineTranslation->suspend();
        $this->_transportBuilder
                ->setTemplateIdentifier($templateId)
                ->setTemplateOptions([
                    'area' => $area,
                    'store' => $store->getId()
                ])
                ->setTemplateVars($vars)
                ->setFrom($from)
                ->addTo($to['email'], $to['name']);
        $transport = $this->_transportBuilder->getTransport();
        $transport->sendMessage();

        $this->inlineTranslation->resume();

        return true;
    }

    /**
     * Get payment info block as html
     *
     * @param Order $order
     * @return string
     */
    public function getPaymentHtml($order)
    {
        return $this->paymentHelper->getInfoBlockHtml(
            $order->getPayment(),
            $this->identityContainer->getStore()->getStoreId()
        );
    }

    /**
     * @param Order $order
     * @return string|null
     */
    public function getFormattedShippingAddress($order)
    {
        return $order->getIsVirtual()
            ? null
            : $this->addressRenderer->format($order->getShippingAddress(), 'html');
    }

    /**
     * @param Order $order
     * @return string|null
     */
    public function getFormattedBillingAddress($order)
    {
        return $this->addressRenderer->format($order->getBillingAddress(), 'html');
    }

    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return $this
     */
    public function execute(\Magento\Framework\Event\Observer $observer) {
        $isNewOrderEnable = $this->dataHelper->getConfigVal('adminemailssection/generalgroup/enabled');

        // Check if new order email notification enabled
        if($isNewOrderEnable){

            // Set current order details
            $orderIds = $observer->getEvent()->getOrderIds();
            $order = $this->order->load($orderIds);
            $orderIncId = $order->getIncrementId();

            // Set email config options
            $store = $this->_storeManager->getStore();
            $from = $this->dataHelper->getConfigVal('adminemailssection/generalgroup/send_from');
            $to = [
                'email' => $this->dataHelper->getConfigVal('adminemailssection/generalgroup/email'),
                'name' => 'Administrator'
            ];
            $templateId = $this->dataHelper->getConfigVal('adminemailssection/generalgroup/template');

            //Start set template vars
            $transport = [
                'order' => $order,
                'billing' => $order->getBillingAddress(),
                'payment_html' => $this->getPaymentHtml($order),
                'store' => $order->getStore(),
                'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
            ];

            $transport = new \Magento\Framework\DataObject($transport);
            $vars = $transport->getData();

            // Call send email function with the necessary parameters
            $this->_sendEmail($from, $to, $templateId, $vars, $store);
        }
    }
}

This is my event File

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="customer_repeat_notification" instance="Neworderemail\Adminemails\Observer\NewEmail" />
    </event>
</config>
2
  • Are you use which payment method? Jun 28 '19 at 5:09
  • this should work with every payment method. I have tested it with placing an order and then cancel the payment. But this mail should be sent out on the moment you place an order - and not wait on the payment ....
    – Ronny
    Jun 28 '19 at 7:12
1

The extension code shown above works based on event/observer concept. Whenever a new order is placed the above code gets executed. You need to look at the file called events.xml in etc/events.xml.

In the events.xml you may need to specify the additional event (in your case it is cancel/completed) and assign the observer.

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/events-and-observers.html

For Knowing the complete events in magento2.1 refer the below link, i think "order_cancel_after" will be the event for your scenario

https://cyrillschumacher.com/magento-2.1-list-of-all-dispatched-events/

Hope this helps you.

7
  • i added the events.xml. should the event be 'sales_model_service_quote_submit_success'?
    – Ronny
    Jun 23 '19 at 13:38
  • Your extension already should have this event for new order email to admin, for your usecase the event should be "order_cancel_after"
    – mydons
    Jun 24 '19 at 6:12
  • i added ` <event name="order_cancel_after"> <observer name="customer_repeat_notification" instance="WebdesignStudenten\Adminemails\Observer\NewEmail" /> </event>` in the events.xml. But that did not the trick ....
    – Ronny
    Jun 24 '19 at 7:37
  • This event will trigger when you cancel the order in admin. if possible share me the source code so that i will take a look at it.
    – mydons
    Jun 24 '19 at 10:21
  • What code do you want to see?
    – Ronny
    Jun 24 '19 at 10:29
1

For the cancel order use below event

<event name="order_cancel_after">
    <observer name="order_cancel_after" instance="Sm\BackEnd\Observer\NewEmail" />
</event>

Also, change the event.xml file path. Move event.xml file from etc/frontend/events.xml to etc/events.xml

1
  • Just tried this .... only with instance="Neworderemail\Adminemails\Observer\NewEmail" but without any luck. When the order is cancelled (on front-end because payment failed) still no newordermail.
    – Ronny
    Jun 27 '19 at 19:31

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .