I took another approach to solving this issue:
I extended the Magento\Sales\Model\Order\Email\Sender\Shipment Sender
's send
method and added my own variables directly onto the transport.
My approach was a result of a 3rd party extension also injecting their own variables through the ShipmentSender
.
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",
'is_delivery' => $order->getShippingMethod() == "shippingtable_shippingtable1",
'is_shipment' => ($order->getShippingMethod() != "flatrate_flatrate" && $order->getShippingMethod() != "shippingtable_shippingtable1"),
'is_pickup_or_delivery' => ($order->getShippingMethod() == "flatrate_flatrate" || $order->getShippingMethod() == "shippingtable_shippingtable1"),
];
$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 was able to reference it like this in my shipment_new.html
template.
{{if is_pickup}}
{{trans "Your %store_name order is ready for Pickup!" store_name=$store.getFrontendName()}}
{{/if}}
{{if is_delivery}}
{{trans "Your %store_name order is on the road!" store_name=$store.getFrontendName()}}
{{/if}}
{{if is_shipment}}
{{trans "Your %store_name order has shipped!" store_name=$store.getFrontendName()}}
{{/if}}