I have created new order from admin. Now it is in Pending state. I need to print invoice pdf but for this order should be in Completed state. I am not able to figure out the way to make it completed from Admin. Any help will be appreciated.
Magento 2 Admin – Change Order Status from Pending to Complete
magento-2.1
Related Solutions
in a template .phtml are hooked with blocks. If you need it for some specific page then the way suggested is 1. In layout handle add a new block. 2. Associate a .phtml 3. remove block.
with this in mind now copy existing .phtml and create a new .phtme with needed specific changes. So find out the handle for the URL you want changed layout and use above provided hints.
As you specified in your question that you have disabled Order Confirmation email from backend, I have assumed the same and write code in such a way that email will not sent twice even if you have enabled Order Confirmation email from backend. So, Order Confirmation Email will only sent from success page when you have disabled it from backend.
To send email on order success page, we need to bind observer with event checkout_onepage_controller_success_action. Here, We write our code send mail. To assure that email is not sent twice, I have created plugin for isEnable() method of OrderIdentity.php class from Magento_Sales module.
First, you need to register event using events.xml. Check the below code.
[Package_Name]\[Module_Name]\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="checkout_onepage_controller_success_action_sendmail" instance="[Package_Name]\[Module_Name]\Observer\SendMailOnOrderSuccess" />
</event>
</config>
After registering event, We will create Observer Class.
[Package_Name]\[Module_Name]\Observer\SendMailOnOrderSuccess.php
<?php
namespace [Package_Name]\[Module_Name]\Observer;
use Magento\Framework\Event\ObserverInterface;
class SendMailOnOrderSuccess implements ObserverInterface
{
/**
* @var \Magento\Sales\Model\OrderFactory
*/
protected $orderModel;
/**
* @var \Magento\Sales\Model\Order\Email\Sender\OrderSender
*/
protected $orderSender;
/**
* @var \Magento\Checkout\Model\Session $checkoutSession
*/
protected $checkoutSession;
/**
* @param \Magento\Sales\Model\OrderFactory $orderModel
* @param \Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender
* @param \Magento\Checkout\Model\Session $checkoutSession
*
* @codeCoverageIgnore
*/
public function __construct(
\Magento\Sales\Model\OrderFactory $orderModel,
\Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender,
\Magento\Checkout\Model\Session $checkoutSession
)
{
$this->orderModel = $orderModel;
$this->orderSender = $orderSender;
$this->checkoutSession = $checkoutSession;
}
/**
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$orderIds = $observer->getEvent()->getOrderIds();
if(count($orderIds))
{
$this->checkoutSession->setForceOrderMailSentOnSuccess(true);
$order = $this->orderModel->create()->load($orderIds[0]);
$this->orderSender->send($order, true);
}
}
}
After adding observer, order cofirmation email will sent on order success page, if we have enabled order confirmation email from Stores > Configuration > Sales > Sales Email > Order. But, now order confirmation sent twice if we enable this field. So, we need to create plugin to avoid this scenario.
To create plugin, you need to use di.xml.
[Package_Name]\[Module_Name]\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="\[Package_Name]\[Module_Name]\Plugin\Sales\Order\Email\Container\OrderIdentityPlugin"/>
</type>
</config>
[Package_Name]\[Module_Name]\Plugin\Sales\Order\Order\Email\Container\ OrderIdentityPlugin.php
<?php
namespace [Package_Name]\[Module_Name]\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;
}
}
Related Topic
- Magento 2.1 Invoice PDF – How to Add Custom Text in Invoice PDF Header
- Magento – Magento 2 : Rupee (currency) symbol overlapping on price
- Magento2 – Plugin/Event After Invoice is Created
- Magento 2.1 – How to Get Order Items and Quote Items When Creating Order from Admin Panel
- Magento – Magento 2 Show custom fee in admin invoice pdf
- Magento 2 – How to Add Custom Field in Customer Admin and Process Data
Best Answer
You can print invoice your Magento order from admin by follow below steps
Now you can see your order invoice in "Invoices" tab just under "ORDER VIEW".
Click on "View" link, you can see you print button for invoice print.