Magento 2 – Send Different Email Templates for Ship Based on Shipping Method

emailemail-templatesmagento2shipment-trackingshipping-methods

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!

Best Answer

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