Depending on a custom order's attribute, we would like to slightly adapt the emails for the new order confirmations.
Therefor, we try to implement a new equals directive in Magento\Framework\Filter\Template
(we extend this class in a custom module, of course). This directive should take two parameters and evaluate whether these values are equal. Magento's build in if
directive only checks, if the given parameter is not equal an empty string.
For example:
{{equals value1 value2}}The two parameters are equal.{{else}}The two parameters are NOT equal.{{/equals}}
We added the following constant to our custom Template.php
:
const CONSTRUCTION_EQUALS_PATTERN = '/{{equals\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/equals\s*}}/si';
The value of this constant is the same as the value of the CONSTRUCTION_IF_PATTERN
constant, except we replaced both if
with equals
.
Furthermore, we override the filter($value)
function of the original Template
and teach this function to execute a new callback named equalsDirective
, if one of the given values
matches to above defined pattern.
foreach ([ self::CONSTRUCTION_DEPEND_PATTERN => 'dependDirective', self::CONSTRUCTION_IF_PATTERN => 'ifDirective', self::CONSTRUCTION_TEMPLATE_PATTERN => 'templateDirective', self::CONSTRUCTION_EQUALS_PATTERN => 'equalsDirective' // this is interesting line ] as $pattern => $directive)
The function equalsDirective
just should write a debug message, when it's got called.
Additionally, we adapt the template for the new orders confirmation emails (Magento\Sales\view\email\order_new.html
) by adding the following beneath the greeting:
{{equals order.checkout_type request}}This is a request.{{else}}This is an order.{{/equals}}
The problem is, the equalsDirective
function never gets called. The given values of the filter
function do not contain the above equals directive. Thus, a pattern -- as defined above -- never is found.
We also verified that {{var order.checkout_type}}
returns the correct value. Surprisingly, this is not part of the given values of the filter
function, as well. But its value is correctly rendered in the email that we received.
Has someone an idea what we missed? Do we have to declare the directives that are parsed from the email templates somewhere else?
We appreciate any help :)
FYI: Couple of years ago we did the same in Magento 1 and it was working fine.