如何在Prestashop 1.6中的电子邮件模板中显示数组中的值

问题描述 投票:0回答:1

我有一个自定义模块。哪个应该向客户发送一些数据。样本数据:

$log[] = array('pid' => '1000', 'price' => '0.00');
$this->sendMail($log);

和我的sendMail函数:

public function sendMail($mailMessage) {
        $id_lang = (int) $this->context->language->id;
        $iso_lang = Language::getIsoById($id_lang);

        if (!is_dir(dirname(__FILE__) . '/mails/' . Tools::strtolower($iso_lang))) {
            $id_lang = Language::getIdByIso('pl');
        }

        Mail::Send(
                $id_lang,
 'notification',
 Mail::l('Notification from Hurto module', (int) $this->context->language->id),
 array('{message}' => Tools::nl2br($mailMessage)),
 Configuration::get('PS_SHOP_EMAIL'),
 null,
 null,
 null,
 null,
 null,
 _PS_MODULE_DIR_ . $this->name . '/mails/'
        );
    }

邮件已发送,但{message}未显示数组中的所有数据。在邮件中我只有一个值 - 1000。还有一件事。如何显示数组中的所有数据?

谢谢

- -编辑

Array ( [0] => Array ( [pid] => 1000 [price] => 0.00 ) )
php prestashop smarty
1个回答
2
投票

首先,您将数组传递给Tools :: nl2br,它只能用于字符串。

你有两个选择来做你想要的。在Mail :: Send之前格式化消息(但根据主题不能有不同的方面)或将数组传递给smarty并在tpl中执行。

选项1:

public function sendMail($mailMessage) {
    $id_lang = (int) $this->context->language->id;
    $iso_lang = Language::getIsoById($id_lang);

    if (!is_dir(dirname(__FILE__) . '/mails/' . Tools::strtolower($iso_lang))) {
        $id_lang = Language::getIdByIso('pl');
    }

    $message = "";
    foreach($mailMessage as $m){
        $message .= "pid {$m['pid']} price {$m['price']}".PHP_EOL;
    }

    Mail::Send(
            $id_lang,
            'notification',
            Mail::l('Notification from Hurto module', (int) $this->context->language->id),
            array('{message}' => Tools::nl2br($message)),
            Configuration::get('PS_SHOP_EMAIL'),
            null,
            null,
            null,
            null,
            null,
            _PS_MODULE_DIR_ . $this->name . '/mails/'
        );
}

选项2:

public function sendMail($mailMessage) {
    $id_lang = (int) $this->context->language->id;
    $iso_lang = Language::getIsoById($id_lang);

    if (!is_dir(dirname(__FILE__) . '/mails/' . Tools::strtolower($iso_lang))) {
        $id_lang = Language::getIdByIso('pl');
    }

    Mail::Send(
            $id_lang,
            'notification',
            Mail::l('Notification from Hurto module', (int) $this->context->language->id),
            array('{message}' => $mailMessage),
            Configuration::get('PS_SHOP_EMAIL'),
            null,
            null,
            null,
            null,
            null,
            _PS_MODULE_DIR_ . $this->name . '/mails/'
        );
}

并在tpl中:

{foreach from=$message item=m}
    {$m['pid']} - {$m['price']} <br />
{/foreach}
© www.soinside.com 2019 - 2024. All rights reserved.