cancel
Showing results for 
Search instead for 
Did you mean: 

How to forward a customer order as an purchase order email by clicking a button in Magento 2 ?

How to forward a customer order as an purchase order email by clicking a button in Magento 2 ?

Hi,
I want to forward all order info with an email to the supplier by manually clicking a button in order page.

I want to forward order shipping info (name, address, phone) and ordered items list (with cost price) to a specific email address by clicking a button.

The email will be one of the email templates.

Just click and send an email with order info.

Thanks ...

 

1 REPLY 1

Re: How to forward a customer order as an purchase order email by clicking a button in Magento 2 ?

Hello @seryum,

 


You have to customize functionality for it. Please follow the below steps to achieve this functionality

  • Create module required file then please add below things in it.
  • Add button on order detail page
  • Apply logic for send order to purchase order email.

Create a plugin in Company/Module/etc/adminhtml/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Backend\Block\Widget\Button\Toolbar">
        <plugin name="MagePal_TestBed::pluginBefore" type="MagePal\TestBed\Plugin\PluginBefore" />
    </type>
</config>

Then in Plugin/PluginBefore.php

 

namespace MagePal\TestBed\Plugin;

class PluginBefore
{
    public function beforePushButtons(
        \Magento\Backend\Block\Widget\Button\Toolbar\Interceptor $subject,
        \Magento\Framework\View\Element\AbstractBlock $context,
        \Magento\Backend\Block\Widget\Button\ButtonList $buttonList
    ) {

        $this->_request = $context->getRequest();
        if($this->_request->getFullActionName() == 'sales_order_view'){
              $buttonList->add(
                'mybutton',
				[
					'label' => __('My Button'),
					'onclick' => 'setLocation(\'' . $this->getCustomUrl() . '\')',
					'class' => 'reset'
				],
                -1
            );
        }

    }
	
	public function getCustomUrl()
    {
        $email = 'john@doe.com';
        $order = $this->_objectManager->create('Magento\Sales\Model\Order')->load(1);
        $order->setCustomerEmail($email);
        if ($order) {
            try {
                $this->_objectManager->create('\Magento\Sales\Model\OrderNotifier')
                    ->notify($order);
                $this->messageManager->addSuccess(__('You sent the order email.'));
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addError(__('We can\'t send the email order right now.'));
                $this->_objectManager->create('Magento\Sales\Model\OrderNotifier')->critical($e);
            }
        }
    }
}

In above file, if you didn't get $this->_objectManager value then please create construct function for it and you have to pass order id in getCustomUrl then you will get $this->getRequest()->getParam('order_id'); 

 

 

 --
If my answer is useful, please Accept as Solution & give Kudos