cancel
Showing results for 
Search instead for 
Did you mean: 

phtml in custom mail template - unable to pass order parameter in layout handle

SOLVED

phtml in custom mail template - unable to pass order parameter in layout handle

Hello people,

 

I'm new here, trying to find my way around in M2. I wish to pull in a custom phtml block in my sales e-mails in order to show a different text according to the shipping method chosen. I created a module with a custom template, layout handle and phtml file. It all seems to work, but I am not able to pass '$order' or '$order_id' as a parameter using a layout handle in my e-mail template; it's always empty when sending a tracking information e-mail.

 

My current situation looks like this:

app
|- code
|- JustMe
|- MyModule
|- etc
| |- email-templates.xml
| |- module.xml
|- view
| |-frontend
| |- email
| | |- shippingmethodtext.html
| |- layout
| | |- shippingmethodtext.xml
| |- templates
| |- email
| |- shippingmethodtext.phtml
|- registration.php

File: email-templates.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
	<template id="JustMe_shippingmethodtext" label="Custom Text per Shipping Method" file="shippingmethodtext.html" type="html" module="JustMe_MyModule" area="frontend"/>
</config>

File: shippingmethodtext.html

is a copy of www/vendor/magento/theme-frontend-luma/Magento_Sales/email/shipment_new.html with an extra line for the layout handle:

{{layout handle="shippingmethodtext" order=$order area="frontend"}}

File: shippingmethodtext.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Custom Block in e-mail" design_abstraction="custom">
    <body>
        <block class="Magento\Framework\View\Element\Template" name="shippingmethodtext" template="JustMe_MyModule::email/shippingmethodtext.phtml"/>
    </body>
</page>

File: shippingmethodtext.phtml

<?php

$_order=$block->getData('order');

if (!empty($_order)) {
	[determine shipping method and do various things accordingly here]
	?>
	<?php endif; 
} ?>

Somehow, the order parameter is always empty when I send a tracking information e-mail from the backend after I created a shipment? When I feed it a random string it works perfectly fine. I must surely be overlooking something here.

 

Can someone here point me in the right direction? I'd be much obliged!

 

Thanks already,

Marselius

1 ACCEPTED SOLUTION

Accepted Solutions

Re: phtml in custom mail template - unable to pass order parameter in layout handle

ok, time for an update.

 

I found out that whenever I create a custom e-mail based on a default template, none of the order items or shipping items get listed in the e-mail sent. These only show when I use the default template.

 

Disabling my module doesn't make any difference, so apparently something goes awry in Magento itself.

 

I ran across this post; https://community.magento.com/t5/Magento-2-x-Technical-Issues/Order-Email-not-showing-itemized-list-... 

 

Setting field [is_legacy] to '1' in the record that contains your template in table [email_template] in your database does the trick somehow and also allows me to pass 'order' as an argument to my custom e-mail template.

View solution in original post

1 REPLY 1

Re: phtml in custom mail template - unable to pass order parameter in layout handle

ok, time for an update.

 

I found out that whenever I create a custom e-mail based on a default template, none of the order items or shipping items get listed in the e-mail sent. These only show when I use the default template.

 

Disabling my module doesn't make any difference, so apparently something goes awry in Magento itself.

 

I ran across this post; https://community.magento.com/t5/Magento-2-x-Technical-Issues/Order-Email-not-showing-itemized-list-... 

 

Setting field [is_legacy] to '1' in the record that contains your template in table [email_template] in your database does the trick somehow and also allows me to pass 'order' as an argument to my custom e-mail template.