You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PrestaShop 1.7.6.3版本Order_conf邮件模板变量缺失求助

PrestaShop 1.7.6.3:Order_conf邮件模板变量缺失修复方案

你的推测完全正确——邮件模板中无法显示的变量,根源就是没有在OrderHistory.php的$data数组中定义对应的映射关系,导致模板无法解析这些变量。以下是两种可行的解决方法:

方法1:直接扩展核心文件的变量数组(快速实现,需注意备份)

找到你提到的两个$data数组所在的代码位置,将缺失的变量按格式添加进去。以下是添加常见缺失变量的示例(请替换为你实际需要的变量):

针对数组1(发货通知相关逻辑)

$data = array(
    '{lastname}' => $result['lastname'],
    '{firstname}' => $result['firstname'],
    '{id_order}' => (int) $this->id_order,
    '{order_name}' => $order->getUniqReference(),
    '{followup}' => str_replace('@', $order->getWsShippingNumber(), $carrierUrl),
    '{shipping_number}' => $order->getWsShippingNumber(),
    // 添加缺失变量示例
    '{total_paid}' => Tools::displayPrice($order->total_paid, $order->id_currency),
    '{payment_method}' => $order->payment,
    '{order_date}' => Tools::displayDate($order->date_add)
);

针对数组2(虚拟商品发货逻辑)

$data = array(
    '{lastname}' => $customer->lastname,
    '{firstname}' => $customer->firstname,
    '{id_order}' => (int) $order->id,
    '{order_name}' => $order->getUniqReference(),
    '{nbProducts}' => count($virtual_products),
    '{virtualProducts}' => $links,
    // 添加缺失变量示例
    '{total_paid}' => Tools::displayPrice($order->total_paid, $order->id_currency),
    '{payment_method}' => $order->payment,
    '{customer_email}' => $customer->email
);

注意:变量值需从$order、$customer等对象中正确获取,可通过$order->字段名调用ps_orders表的对应字段,$customer->字段名调用ps_customer表对应字段。

方法2:使用钩子注入变量(推荐,避免升级覆盖核心文件)

PrestaShop提供actionMailVariables钩子,可在邮件发送前动态添加变量,无需修改核心代码:

  1. 创建自定义模块(或复用现有模块),在模块主文件中添加以下代码:
public function install()
{
    return parent::install() && $this->registerHook('actionMailVariables');
}

public function hookActionMailVariables($params)
{
    // 仅针对Order_conf模板处理
    if ($params['template'] === 'order_conf') {
        $order = $params['object'];
        $customer = new Customer($order->id_customer);
        
        // 注入缺失的变量
        $params['template_vars']['{total_paid}'] = Tools::displayPrice($order->total_paid, $order->id_currency);
        $params['template_vars']['{payment_method}'] = $order->payment;
        $params['template_vars']['{order_date}'] = Tools::displayDate($order->date_add);
        // 添加其他需要的变量...
    }
}
  1. 安装并启用该模块,邮件发送时会自动将变量注入到模板中。

注意事项

  • 修改核心文件前必须备份,PrestaShop版本升级会覆盖核心文件,因此优先使用钩子方案。
  • 若不确定变量对应的字段名,可直接查看数据库的ps_orders和ps_customer表确认字段。

内容的提问来源于stack exchange,提问作者ItsAnfield

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 18:13:27