I am trying to send an order email confirmation with adding extra attributes values to Admin, But it's not working for me.
Here I have 2 Email templates.
1) It's a default email template
2) Customized email template
First one is the default, and it will send Magento by default.
For the second one I tried below code, it's printing as it is table format.
<?php
namespace ABCSolutions\AvailableStore\Observer;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
class AfterPlaceOrderObserver implements \Magento\Framework\Event\ObserverInterface{
/**
* Store model manager
*
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var \Magento\Framework\Translate\Inline\StateInterface
*/
protected $_inlineTranslation;
/**
* @var \Magento\Framework\Mail\Template\TransportBuilder
*/
protected $_transportBuilder;
/**
* @var \Magento\Catalog\Model\ProductFactory
*/
protected $_productLoader;
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var \Magento\Sales\Model\Order
*/
protected $_order;
/**
* @var \Magento\Sales\Model\OrderNotifier
*/
protected $_orderNotifier;
/**
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
* @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
* @param ProductRepositoryInterface $productRepository
* @param StoreManagerInterface $storeManager
* @param \Magento\Sales\Model\Order $order
* @param \Magento\Sales\Model\OrderNotifier $orderNotifier
*/
public function __construct(
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
//\Magento\Catalog\Model\ProductFactory $_productLoader
ProductRepositoryInterface $productRepository,
StoreManagerInterface $storeManager,
\Magento\Sales\Model\Order $order,
\Magento\Sales\Model\OrderNotifier $orderNotifier
){
$this->_inlineTranslation = $inlineTranslation;
$this->_transportBuilder = $transportBuilder;
//$this->_productLoader = $_productLoader;
$this->productRepository = $productRepository;
$this->storeManager = $storeManager;
$this->_order = $order;
$this->_orderNotifier = $orderNotifier;
}
/**
* @param \Magento\Framework\Event\Observer $observer
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
/* @var $order \Magento\Sales\Model\Order */
$order = $observer->getEvent()->getOrder();
$templateOptions = array('area' => \Magento\Framework\App\Area::AREA_FRONTEND,'store' => 1);
$mediaPath = $this->storeManager->getStore(1)->getBaseURL(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product';
$table = "";
$table .= "<table>";
$table .= "<thead>";
$table .= "<tr>";
$table .= "<th>";
$table .= "Product Image";
$table .= "</th>";
$table .= "<th>";
$table .= "Product Name";
$table .= "</th>";
$table .= "<th>";
$table .= "Qty.";
$table .= "</th>";
$table .= "<th>";
$table .= "Price";
$table .= "</th>";
$table .= "</tr>";
$table .= "</thead>";
//start foreach for items
foreach($order->getAllItems() as $_items){
$_product = $this->productRepository->getById($_items->getProductId());
$imageThumbNail = $mediaPath . $_product->getThumbnail();
$pSKU=$_product->getSku();
$productName = $_product->getName();
$QtyOrdered = $_items->getQtyOrdered();
$pPrice = $_product->getPrice();
$CAField = $_product->getCustomAttributeField();
$table .= "<tr>";
$table .= "<td style='width: 120px;text-align: center;'>";
$table .= "<img src='$imageThumbNail' title='$productName' style='width: 70px;height: 70px;' />";
$table .= "</td>";
$table .= "<td style='width: 200px;'>";
$table .= "<table>";
$table .= "<tr>";
$table .= "<td>";
$table .= "<span style='color: Blue;'>SKU: </span>";
$table .= "</td>";
$table .= "<td>";
$table .= " <span>$pSKU</span>";
$table .= "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td>";
$table .= "<span style='color: Blue;'>Cust. Attr. Field: </span>";
$table .= "</td>";
$table .= "<td>";
$table .= "<span style='font-size: 14px;'>$CAField</span>";
$table .= "</td>";
$table .= "</tr>";
$table .= "</table>";
$table .= "</td>";
$table .= "<td style='width: 50px;text-align: center;'>";
$table .= "<span>$QtyOrdered</span>";
$table .= "</td>";
$table .= "<td style='text-align: center;'>";
$table .= "<span>$pPrice</span>";
$table .= "</td>";
$table .= "</tr>";
}
//end foreach
$table .= "</table>";
$mass = $table;
$templateVars = array(
'store' => 1,
'orderNumber' => $order->getIncrementId(),
'customer_name' => $order->getCustomerName(),
'message' => $mass
);
$from = array(
'email' => '[email protected]',
'name' => 'Customer Care'
);
$to = "[email protected]";
try {
$this->_inlineTranslation->suspend();
$to = array($to);
$transport = $this->_transportBuilder->setTemplateIdentifier('custom_email_template')
->setTemplateOptions($templateOptions)
->setTemplateVars($templateVars)
->setFrom($from)
->addTo($to)
->getTransport();
$transport->sendMessage();
$this->_inlineTranslation->resume();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
}
}
It's printing table format, like below image.
Please suggest us to send the email of order email copy with extra attributes?