Hello @digitalstartup ,
I first want to say big thanks for all your hard work in helping people and sharing knowledge with them - you are a !
Will try keep it to the point:
My setup
- Magento 2.2.3
- Ultimo
- Shared server hosted on Siteground
- Followed Craigâs âLetâs buildâ series which have helped me significantly!
What Iâm trying to do
I have been trying for some time now to send a different email template based on what customers have selected as a shipping method.
For example: If there is âRoyal Mailâ / âMNX Midnite Expressâ / âClick & Collectâ as shipping methods then:
If Royal Mail:
- âYour order is on the road!â
If Click & Collect (âis_pickupâ var in the code below):
- âYour order is ready for Pickup!â
If MNX Midnite Express:
- âYour order is on the road!â
More details: I am using the FlatRate shipping method for Click&Collect (Pickup) orders and the Matrix Table Rates as Royal Mail.
What Iâve already tried
While teaching myself on the internet as to how this could be achieved I came across an article where people managed to reproduce exactly what I am after with a piece of PHP code which doesnât really work for me. Iâve tried creating some variables to use in the newly created email template(see screenshot below) to add some messages based on the shipping method.
Added the variables to the template by creating a plugin for vendor\Magento\Sales\Model\Order\Email\Container\Template
's setTemplateVars
method using the code from below:
class ShippingVars
{
public function beforeSetTemplateVars(Template $subject, array $vars)
{
/** @var Order $order */
$order = $vars['order'];
$method = $order->getShippingMethod();
$vars['is_pickup'] = $method === 'flatrate_flatrate';
return [$vars];
}
}
and this is how I called it in the email template:
{{if is_pickup}}
<p>Your order is ready for Pickup!</p>
{{else}}
<p>Your order is on the road!</p>
{{/if}}
The issue that I have
âŚis that I only get the else statement appearing in the Email that is sent to customers after new order even if they selected the flat rate shipping method(which I am using for âClick & Collectâ).
2nd attempt - I didnât let go and tried a different approach:
I extended the vendor\Magento\Sales\Model\Order\Email\Sender\Shipment Sender
's send
method and added my own variables directly onto the transport:
âis_pickupâ => $order->getShippingMethod() == âflatrate_flatrateâ,
see below the full code: :
class ShipmentSender extends BaseShipmentSender
{
public function send(Shipment $shipment, $forceSyncMode = false)
{
$shipment->setSendEmail(true);
if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
$order = $shipment->getOrder();
$transport = [
'order' => $order,
'shipment' => $shipment,
'comment' => $shipment->getCustomerNoteNotify() ? $shipment->getCustomerNote() : '',
'billing' => $order->getBillingAddress(),
'payment_html' => $this->getPaymentHtml($order),
'store' => $order->getStore(),
'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
'shipping_arrival_date' => $order->getShippingArrivalDate(),
'delivery_time_slot' => $order->getShippingArrivalTimeslot(),
'shipping_arrival_comments' => $order->getShippingArrivalComments(),
'is_pickup' => $order->getShippingMethod() == "flatrate_flatrate",
];
$this->eventManager->dispatch(
'email_shipment_set_template_vars_before',
['sender' => $this, 'transport' => $transport]
);
$this->templateContainer->setTemplateVars($transport);
if ($this->checkAndSend($order)) {
$shipment->setEmailSent(true);
$this->shipmentResource->saveAttribute($shipment, ['send_email', 'email_sent']);
return true;
}
}
$this->shipmentResource->saveAttribute($shipment, 'send_email');
return false;
}
Due to this change I tried to reference it like this in my Email Template:
{{if is_pickup}}
{{trans "Your %store_name order is ready for Pickup!" store_name=$store.getFrontendName()}}
{{else}}
{{trans "Your %store_name order is on the ROOOOOOAD!" store_name=$store.getFrontendName()}}
{{/if}}
but unfortunately no success again.
Then I tried using the Free Shipping method for a âClick & Collectâ service with swapping the âflatrate_flatrateâ code for âfreeâ but also didnât do the trick for me.
My Question
Can you @digitalstartup point me to the right direction once more, please?!
I realise itâs a case of having slightly more knowledge/expertise in javascript/PHP which I still donât so any advise or recommendations would be highly appreciated.
Best regards ,
Teo