3

I want to show delivery date in order-email template.I want to show in delivery date (today date + 10 day). How can I achieve that?

2

2 Answers 2

4

For this you need to create a simple module say Mago_Delivery. Create following files

app/code/Mago/Delivery/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Mago_Delivery',
__DIR__
);

app/code/Mago/Delivery/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="Mago_Delivery" setup_version="1.0.1">
   </module>
</config>

app/code/Mago/Delivery/etc/di.xml

<?xml version="1.0"?>
<config>
    <preference for="Magento\Sales\Model\Order" type="Mago\Delivery\Model\Rewrite\Order" />
</config>

app/code/Mago/Delivery/Model/Rewrite/Order.php

<?php

namespace Mago\Delivery\Model\Rewrite;

class Order extends \Magento\Sales\Model\Order
{

    public function getDeliveryDate()
    {
        $orderDate = $this->getCreatedAt(); //ORDER CREATED DATE
        return date('Y-m-d', strtotime($orderDate. ' + 10 days'));
    }

}

Finally in your email template you can get this date by:

{{var order.getDeliveryDate()}}

Note: I have created getDeliveryDate() function in general way, You can change it as per your requirement.

Hope above will help!

3
  • Thanks for this solution. Worked perfectly in M2.3.5.
    – Webninja
    Oct 7, 2020 at 8:23
  • @Webninja, Most welcome :)
    – Pawan
    Oct 7, 2020 at 8:25
  • 1
    Did not work for me in Magento ver. 2.3.6
    – TheFrakes
    Aug 24, 2021 at 11:41
1

In order to show delivery date in order Email, you have pass variables in your email function.

First, we need to get a delivery date in (today date + 10 days) format so for this you have to define the date in a construct like:

protected $date;

public function __construct(
    ....
    \Magento\Framework\Stdlib\DateTime\DateTime $date,
    ....
) {
    ....
    $this->date = $date;
    ....
}

parent::__construct($date);

$date = $this->date->gmtDate();

//////In Execute Function//////
$date = $this->date->gmtDate();

$DeliveryDate = date('Y-m-d', strtotime('+10 day', strtotime($date)));

$DeliveryDate will return (today date+10) date.

Pass $DeliveryDate variable in your Email function and use it in your email template.

1
  • How to use this codes? Can you eloborate your answer please,it will be helpful to me and fellow newbies,In ..vendor\magento\module-sales\Model\order.php created construct,where to edit execute function,which file..? Dec 18, 2021 at 5:56

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.