Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento:如何在Magento电子邮件模板中获取当前日期_Magento_Email - Fatal编程技术网

Magento:如何在Magento电子邮件模板中获取当前日期

Magento:如何在Magento电子邮件模板中获取当前日期,magento,email,Magento,Email,如何在Magento电子邮件模板中获取当前日期 <pre><code> $date = date('d-m-Y'); echo $date; </code></pre> {{var dateAndTime}}不起作用。我需要获取发送电子邮件的当前日期。您有两个选项。 选项1。是重写发送电子邮件的方法,并将当前日期作为参数传递。 例如,假设您希望在订单电子邮件中显示日期。 为此,您需要重写Mage\u Sales\u Model\u

如何在Magento电子邮件模板中获取当前日期

<pre><code>
    $date = date('d-m-Y');
    echo $date;
</code></pre>
{{var dateAndTime}}
不起作用。我需要获取发送电子邮件的当前日期。

您有两个选项。
选项1。是重写发送电子邮件的方法,并将当前日期作为参数传递。
例如,假设您希望在订单电子邮件中显示日期。
为此,您需要重写
Mage\u Sales\u Model\u Order::sendNewOrderEmail
方法。
您需要更改以下内容:

    $mailer->setTemplateParams(array(
            'order'        => $this,
            'billing'      => $this->getBillingAddress(),
            'payment_html' => $paymentBlockHtml
        )
    );
为此:

   $mailer->setTemplateParams(array(
            'order'        => $this,
            'billing'      => $this->getBillingAddress(),
            'payment_html' => $paymentBlockHtml,
            'dateAndTime'  => Mage::getModel('core/date')->date('Y-m-d H:i:s'), //change format as needed.
        )
    );
然后,您将能够在新订单电子邮件模板中使用
{{var dateAndTime}}

如果您只想在一个模板中使用日期和时间,这非常方便。
如果您想要更一般的情况,您需要创建自己的指令,请参见选项2

选项2创建自己的
{{}
指令。
假设您希望在每封电子邮件中使用{dateAndTime}}打印到当前日期和时间。
您需要重写类
Mage\u Widget\u Model\u Template\u Filter
,并向其中添加新方法。
.
您的新方法应如下所示:

public function dateAndTimeDirective($construction) {
    return Mage::getModel('core/date')->date('Y-m-d H:i:s');
}
public function dateAndTimeDirective($construction) {
    $params = $this->_getIncludeParameters($construction[2]); 
    $format = isset($params['format']) ? $params['format'] : 'Y-m-d H:i:s'
    return Mage::getModel('core/date')->date($format);
}
您甚至可以将其升级,并能够将日期格式作为如下参数传递:

 {{dateAndTime format="Y-m-d"}}
在这种情况下,处理指令的方法应如下所示:

public function dateAndTimeDirective($construction) {
    return Mage::getModel('core/date')->date('Y-m-d H:i:s');
}
public function dateAndTimeDirective($construction) {
    $params = $this->_getIncludeParameters($construction[2]); 
    $format = isset($params['format']) ? $params['format'] : 'Y-m-d H:i:s'
    return Mage::getModel('core/date')->date($format);
}
  • 从管理面板转到您的电子邮件模板。使用块指令如下

    app/design/frontend/yourtheme/yourtemplate/page/html/date.phtml
    
    当前日期为:

    {{block type='core/template' area='frontend' template='page/html/date.phtml'}}
    
  • 现在在模板中创建一个新文件
    date.phtml
    ,如下所示

    app/design/frontend/yourtheme/yourtemplate/page/html/date.phtml
    
  • 在php标记中添加以下代码(使用php日期函数并回显当前日期,即
    date('d-m-Y')


  • 希望这有帮助

    请在这里提供一些代码。这样我就可以结帐了。你看了吗?很抱歉回复晚了。在
    Mage\u Widget\u Model\u Template\u Filter
    上添加方法不起作用,我不知道为什么。但我在
    Mage\u Core\u Model\u Email\u Template\u Filter
    上添加了相同的方法,效果很好。非常感谢你。