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;
}
}
Best Answer
you have to load product to get thumbnail and url .
//get image url