0

I need to get the value of an email variable in a phtml file so I can test the value.

In transactional emails I would use, as an exmaple

<td>"Delivery Date" : {{var deliveryDate}}</td>

to render the order specific value for the order delivery but that doesn't work in phtml if I try and do something like

<?php $originaldate = {{var deliveryDate}}; ?>
<?php $newDate =  date('Y-m-d', strtotime($originaldate. ' + 10 days')); ?>
<?php echo $newDate; ?>

When I do this it doesn't return a value and I don't know how to get order or transactional email variables in phtml.

Sorry if this a bit basic but I'm a bit new to some of this and would appreciate your help.

  • share your phtml file code – Mohit Rane Jul 25 '19 at 9:43
  • What's the easiest way to do that? – wjpbill Jul 25 '19 at 9:54
  • if you want value in phtml file than you can use $block-> and then the method name for getting deliverydate value. – Mohit Rane Jul 25 '19 at 9:57
  • Thank you. How do i get the method name? In an email template I just use {{var deliveryDate}} do you mean something like <?php echo __('Delivery date') . ': ' . $block->escapeHtml($order->getShqDeliveryDate()) ?> – wjpbill Jul 25 '19 at 10:00
  • yes, you got it right – Mohit Rane Jul 25 '19 at 10:03
0

I've found a lot of the email logic abstract and you are right. In one or two places it is difficult to follow. One of the simpler examples is the contact us form. I'm bringing together logic from several files but in summary you have the $_POST variables

$post = $this->getRequest()-getPostValue();

Which means you could dump out name

$name = $this->getRequest()->getPostValue('name');
var_dump($name);

Before the rest of the journey works like this...

Variables for template

$variables = ['data' => new DataObject($post)];

Building email

        $this->inlineTranslation->suspend();
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $transport = $this->transportBuilder
            ->setTemplateIdentifier($this->contactsConfig->emailTemplate())
            ->setTemplateOptions(
                [
                    'area' => Area::AREA_FRONTEND,
                    'store' => $this->storeManager->getStore()->getId(),
                ]
            )
            ->setTemplateVars($variables)
            ->setFrom($this->contactsConfig->emailSender())
            ->addTo($this->contactsConfig->emailRecipient())
            ->setReplyTo($replyTo, $replyToName)
            ->getTransport();

        $transport->sendMessage();
        $this->inlineTranslation->resume();

Email html

<tr>
    <td><strong>{{trans "Name"}}</strong></td>
    <td>{{var data.name}}</td>
</tr>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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