Question

I am having a dilemma on how to send a different email content when an order is Shipped based on the Shipping Method chosen.

For example: If there is Delivery / Pick Up / Shipping as shipping methods then:

If Delivery:

  • "Your order is on the road!"

If Pickup:

  • "Your order is ready for Pickup!"

If Shipping:

  • (Default email template with tracking code)

More details: I am using the FlatRate shipping method for Pickup orders and the Table Rates as Delivery.

Thanks!

Was it helpful?

Solution

You can create some variables to use in the email template to add some messages based on the shipping method.

Add the variables to the template by creating a plugin for Magento\Sales\Model\Order\Email\Container\Template's setTemplateVars method. For example:

class ShippingVars
{
    public function beforeSetTemplateVars(\Magento\Sales\Model\Order\Email\Container\Template $subject, array $vars)
    { 
        /** @var Order $order */
        $order = $vars['order'];
        $method = $order->getShippingMethod();

        $vars['is_pickup'] = $method === 'flatrate_flatrate';

        return [$vars];
    }
}

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}}

OTHER TIPS

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}}

Could you do it in Magento 2? I had used it and found nothing. Please can someone help me?

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top