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