cancel
Showing results for 
Search instead for 
Did you mean: 

Email Template format date

SOLVED

Email Template format date

Hey,

 

I'm new to Magento and I'm trying to use variables in a custom email template I created.

I want to show the date and the available variable it's var created_at_formatted but I need it without the time, just let's say dd/mm/yyy.

I couldn't find any variant to do that is there any or it's needed to override something to do that? and if it's needed what and how

 

 

Thanks Smiley Very Happy

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Email Template format date

Hello @rui_silva1 

Try the below solution:

 

$templateOptions = [
             'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
             'store' => $this->storeManager->getStore()->getId()
            ];
            $templateVars = [
                        'store' => $this->storeManager->getStore(),
                        'admin_name' => 'Admin',
                        'subject'    => 'subject',
                        'created_at_formatted'    => date("d/m/Y", strtotime($str));
                    ];
            $from = ['email' => 'from@email.com', 'name' => 'from name'];
            $to= "test@gmial.com"
            $this->inlineTranslation->suspend();
            $transport = $this->transportBuilder->setTemplateIdentifier('template name or id')
                    ->setTemplateOptions($templateOptions)
                    ->setTemplateVars($templateVars)
                    ->setFrom($from)
                    ->addTo($to)
                    ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();

Use in email template {{var created_at_formatted}} variable

 

Hope it helps.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

View solution in original post

8 REPLIES 8

Re: Email Template format date

Hello @rui_silva1 

Try the below solution:

 

$templateOptions = [
             'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
             'store' => $this->storeManager->getStore()->getId()
            ];
            $templateVars = [
                        'store' => $this->storeManager->getStore(),
                        'admin_name' => 'Admin',
                        'subject'    => 'subject',
                        'created_at_formatted'    => date("d/m/Y", strtotime($str));
                    ];
            $from = ['email' => 'from@email.com', 'name' => 'from name'];
            $to= "test@gmial.com"
            $this->inlineTranslation->suspend();
            $transport = $this->transportBuilder->setTemplateIdentifier('template name or id')
                    ->setTemplateOptions($templateOptions)
                    ->setTemplateVars($templateVars)
                    ->setFrom($from)
                    ->addTo($to)
                    ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();

Use in email template {{var created_at_formatted}} variable

 

Hope it helps.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Email Template format date

Hey, @Sanjay Jethva 

 

Thanks for your reply, this might be a stupid question but i'm not really familiarized with extending so, where's the best place to put that code or what to extend?

 

Thanks Smiley Very Happy

Re: Email Template format date

Hey @rui_silva1 again,

 

Please refer https://magento.stackexchange.com/a/203195/24801

 

Thanks.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Email Template format date

Hey again @Sanjay Jethva,

 

I'm sorry to keep bothering, but there is no way that can just edit the variable or even create one based on that one?

 

I used extending in /app/code/custom_name/Emails registration.php just to create a custom variable, it's possible use like this just to edit date format

 

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'custom_name_Emails',
    __DIR__
);

 

 /app/code/custom_name/Emails/etc events.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <event name="email_order_set_template_vars_before">
        <observer name="custom_name_emails_variable_order" instance="custom_name\Emails\Observer\AddMyData" />
    </event>
</config>

 

/app/code/custom_name/Emails/etc module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="custom_name_Emails" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>

and /app/code/custom_name/Emails/Observer AddMyData.php

<?php

namespace custom_data\Emails\Observer;

use Magento\Framework\Event\ObserverInterface;

class AddMyData implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $transport = $observer->getTransport();
        $transport['myvar'] = 'My Value';
    }
}

 

I'm really confused with extending I checked the docs but no luck with understanding the way to do it

 

(edited)

Sorry and thanks Smiley Very Happy

Re: Email Template format date

Hello @rui_silva1 

No worries, I'm happy to be of any help Smiley Happy

 

Please try the below code, it may help you:

public function execute(\Magento\Framework\Event\Observer $observer)
{
    /** @var \Magento\Framework\App\Action\Action $controller */
    $transport = $observer->getEvent()->getTransport();
    $transport['myvar'] = 'My Value';
}

Thanks.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Email Template format date

Hello @rui_silva1 ,

Please follow this link https://meetanshi.com/blog/use-custom-variables-in-transactional-email-in-magento-2/

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Email Template format date

Hey @Sanjay Jethva,

 

Thanks! I got it to work with

 

<?php

namespace custom_name\Emails\Observer;

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

class AddMyData implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
    /** @var \Magento\Framework\App\Action\Action $controller */
    $transport = $observer->getEvent()->getTransport();
    $dateraw = $transport->getOrder()->getCreatedAt();
    $transport['myvar'] = date("d-m-Y", strtotime($dateraw));
    }
}

Thank you so much! Smiley Very Happy

Re: Email Template format date

3 thinngs you need to know about Magento 2 email template as a newbie

 

Trusted Magento 2 Extensions