I have made a custom template to use in my order confirmation email template.
I want to get the order data in this template so I can create a custom order grid layout.
I'm not sure on how to do this? Can anyone point me in the right direction?
emailemail-templatesmagento2order-gridsales-order
I have made a custom template to use in my order confirmation email template.
I want to get the order data in this template so I can create a custom order grid layout.
I'm not sure on how to do this? Can anyone point me in the right direction?
To send different order conformation email depending on specific criteria you would either have to :
Rewrite sendNewOrderEmail()
in Mage_Sales_Model_Order and add logic to check the template
Disable magento "order confirmation email" in system config, then create a custom module to send your email by coping logic from sendNewOrderEmail() using event/observer
For simple text changes, you could use template logic {{if order.customer_group_id}}
or {{depend order.customer_group_id}}
but they seem to only evaluate true/false condition, therefore for more advance logic you could include a block
{{block type='core/template' area='frontend' template='sales/custom_logic.phtml' order=$order}}
In custom_logic.phtml
<?php
$order = $this->getOrder()
if($order->getCustomerGroupId() == 1){
///do
....
Solved my self, for the Reference of Thread.
create email_templates.xml
file from etc
folder and paste below code.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Email/etc/email_templates.xsd">
<template id="available_store_template" label="Available Store" file="available_store.html" type="html" module="ABCSolutions_AvailableStore" area="frontend"/>
</config>
Create available_store.html
file from view/frontend/email
folder and paste below code.
<!--@subject Available Stores for the Order of : {{var orderNumber}} @-->
<!--@vars
{"store url=\"\"":"Store Url",
"skin url=\"images/logo_email.gif\" _area='frontend'":"Email Logo Image"}
@-->
<!--@styles
body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
@-->
{{template config_path="design/email/header_template"}}
<div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td align="center" valign="top" style="padding:20px 0 20px 0">
<table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
<tr>
<td valign="top">
<h1 style="font-size:22px;font-weight:normal;line-height:22px;margin:0 0 11px 0;">{{trans "Hello Supervisor"}},</h1>
</td>
</tr>
<tr>
<td valign="top">
<h1 style="font-size:22px;font-weight:normal;line-height:22px;margin:0 0 11px 0;">{{trans "Please find below order information."}}</h1>
</td>
</tr>
<tr>
<td>
{{layout handle="email_product_list" items=$items area="frontend"}}
</td>
</tr>
<tr>
<td valign="top">
<h1 style="font-size:22px;font-weight:normal;line-height:22px;margin:0 0 11px 0;">{{trans "If you have any queries, Please contact Admin: +91 9949906633"}}</h1>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
{{template config_path="design/email/footer_template"}}
Create events.xml
file from etc
folder and paste below code.
<?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="sales_order_place_after">
<observer name="send_available_store_order_items" instance="ABCSolutions\AvailableStore\Observer\AfterPlaceOrderObserver" />
</event>
</config>
Create AfterPlaceOrderObserver.php
file from Observer
folder and paste below code.
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);
$templateVars = array(
'store' => 1,
'orderNumber' => $order->getIncrementId(),
'customer_name' => $order->getCustomerName(),
'items'=> $order->getAllItems(),
);
$from = array(
'email' => 'info@gmail.in',
'name' => 'Customer Care'
);
$to = "abc@gmail.com";
try {
$this->_inlineTranslation->suspend();
$to = array($to);
$transport = $this->_transportBuilder->setTemplateIdentifier('available_store_template')
->setTemplateOptions($templateOptions)
->setTemplateVars($templateVars)
->setFrom($from)
->addTo($to)
->getTransport();
$transport->sendMessage();
$this->_inlineTranslation->resume();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
}
create layout file email_product_list.xml
from view/frontend/layout
and paste below code.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Email Product List" design_abstraction="custom">
<body>
<block class="Magento\Framework\View\Element\Template" name="additional.product.info" template="ABCSolutions_AvailableStore::email/product.phtml"/>
</body>
</page>
And create product.phtml
file from view/frontend/templates/email
and paste below code.
<?php $_items = $block->getItems() ?>
<table class="email-items">
<thead>
<tr>
<th class="item-info">
<?= /* @escapeNotVerified */ __('Product Image'); ?>
</th>
<th class="item-info">
<?= /* @escapeNotVerified */ __('Product Name'); ?>
</th>
<th class="item-qty">
<?= /* @escapeNotVerified */ __('Qty'); ?>
</th>
<th class="item-qty">
<?= /* @escapeNotVerified */ __('Each Price'); ?>
</th>
<th class="item-price">
<?= /* @escapeNotVerified */ __('Total Price'); ?>
</th>
</tr>
</thead>
<tbody>
<?php
$om = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $om->create('\Magento\Store\Model\StoreManagerInterface');
$thumbNail = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product';
$productRepository = $storeManager = $om->create('\Magento\Catalog\Api\ProductRepositoryInterface');
$totalAmount = 0;
?>
<?php foreach ($_items as $_item): ?>
<?php $_product = $productRepository->getById($_item->getProductId()); ?>
<tr>
<td>
<?php $product_image_url = $thumbNail . $_product->getThumbnail();?>
<img id='image' src="<?php echo $product_image_url; ?>" style='border-width:0px;height:80px;width:80px;'>
</td>
<td>
<a href="<?php echo $block->getBaseUrl() . $_item->getProduct()->getUrlKey(); ?>">
<p class="product-name"><?php echo $block->escapeHtml($_item->getName()) ?></p>
</a>
<p class="sku">
<b><?php /* @escapeNotVerified */ echo __('SKU') . ' : ' ?></b>
<a href="<?php echo $block->getBaseUrl() . $_item->getProduct()->getUrlKey(); ?>">
<?php echo $block->escapeHtml($_item->getSku()); ?>
</a>
</p>
<p class="sku">
<b><?php /* @escapeNotVerified */ echo __('Available Store') . ' : ' ?></b>
<?php echo $block->escapeHtml($_product->getAvailableStore()); ?>
</p>
</td>
<td class="item-qty">
<span><?php echo $_item->getQtyOrdered() ?></span>
</td>
<td class="item-price">
<span><?php echo $_item->getOriginalPrice() ?></span>
</td>
<td class="item-price">
<span><?php echo $_item->getQtyOrdered() * $_item->getOriginalPrice() ?></span>
</td>
<?php
$tAmount = $_item->getQtyOrdered() * $_item->getOriginalPrice();
$totalAmount += $tAmount;
?>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="4" style="text-align: right;font-size: 16px;font-weight: bold;">
<span>Total: </span>
</td>
<td style="text-align: right;font-size: 16px;font-weight: bold;">
<span><?php echo $totalAmount ?></span>
</td>
</tr>
</tbody>
</table>
Best Answer
Did you check how Magento default templates works?
e.g. Magento_Sales/view/frontend/email
This is an example how to get customer name in email template: