0

I have a custom shipping module and I am trying to execute some information in an email template based on the shipping method chosen. I have tried things like:

{{if order.getShippingDescription() == 'Shipping Method - Shipping Title'}}
<p>{{trans "Works!"}}</p>
{{/if}}

How can I target the shipping method and show information based on that method in the email template?

UPDATE I have tried creating a plugin in Vendor/Module/Plugin/Order/Email/Container/EmailCode.php:

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

        $vars['shipping_code'] = $method === 'freedelivery_freedelivery';

        return [$vars];
    }
}

and I added in etc/frontend di.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Model\Order\Email\Container\Template">
        <plugin name="email.shipping.code" type="Vendor\Module\Plugin\Order\Email\Container\Template\EmailCode" sortOrder="100"/>
    </type>
</config>

and then this in my email template:

{{if shipping_code}}
<p>{{trans "Email Code Works"}}</p>
{{/if}}

And it didn't work. Any help is appreciated!

Thanks, Stan

1 Answer 1

1

I was able to do this by adding an observer and calling it in events.xml.

namespace Vendor\Module\Observer;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class EmailTemplateVars implements ObserverInterface
{
    protected $customerRepository;

    public function __construct(CustomerRepositoryInterface $customerRepository) 
    {
        $this->customerRepository = $customerRepository;
    }
    
    public function execute(Observer $observer)
    {
        /** @var \Magento\Framework\App\Action\Action $controller */
        $transport = $observer->getEvent()->getTransport();
        
        if($transport->getOrder() != null)
        {
            $transport['checkout_building_address']   = $transport->getOrder()->getCheckoutBuildingAddress(); 
            $transport['checkout_floor_number']       = $transport->getOrder()->getCheckoutFloorNumber(); 
            $transport['checkout_room_number']        = $transport->getOrder()->getCheckoutRoomNumber();  
        }
    }
}

etc/events.xml

 <event name="email_order_set_template_vars_before">
        <observer name="somename_checkout_custom_fields_add_to_email" instance="Vendor\Module\Observer\EmailTemplateVars"/>
    </event>

In the email template:

<p><strong>{{trans "Building Address"}}</strong><br />{{var checkout_building_address|raw}}</p>
<p><strong>{{trans "Floor Number"}}</strong><br />{{var checkout_floor_number|raw}}</p>
<p><strong>{{trans "Room Number"}}</strong><br />{{var checkout_room_number|raw}}</p>

Stan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.