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

PrestaShop 1.7:后台英文时按订单语言生成发票失败求助

问题原因分析
  • 未调用父类构造函数:你的子类HTMLTemplateInvoice完全重写了构造函数,但未执行parent::__construct(),导致父类HTMLTemplateInvoiceCore中负责初始化PDF模板、加载对应语言翻译的核心逻辑未触发。父类构造会基于当前Context语言完成模板路径配置、翻译加载等关键操作,跳过这一步后,即便修改了Context语言,后续模板渲染仍会沿用后台默认语言的设置。
  • 语言设置时机错误:即便调用父类构造,若在父类初始化后才修改Context语言,父类已用原语言完成了基础配置,后续的翻译和模板渲染不会自动同步更新。
解决方案

修改HTMLTemplateInvoice类的构造逻辑,先设置订单对应语言,再调用父类构造,确保父类初始化时使用正确语言,同时保留自定义逻辑:

<?php

class HTMLTemplateInvoice extends HTMLTemplateInvoiceCore
{
    public $available_in_your_account = false;

    /**
     * @param OrderInvoice $order_invoice
     * @param $smarty
     * @param bool $bulk_mode
     *
     * @throws PrestaShopException
     */
    public function __construct(OrderInvoice $order_invoice, $smarty, $bulk_mode = false)
    {
        // 获取订单对象及对应语言ID
        $this->order = new Order((int) $order_invoice->id_order);
        $order_lang_id = (int) $this->order->id_lang;

        // 提前设置Context语言为订单语言,确保父类构造基于正确语言初始化
        Context::getContext()->language = new Language($order_lang_id);
        // 同步更新Smarty的语言变量
        Context::getContext()->smarty->assign('language', Context::getContext()->language);

        // 调用父类构造,执行核心初始化逻辑
        parent::__construct($order_invoice, $smarty, $bulk_mode);

        // 自定义标题设置
        $this->title = $order_invoice->getInvoiceNumberFormatted($order_lang_id);
        
        // 保留店铺地址修复逻辑
        if (!isset($this->order_invoice->shop_address) || !$this->order_invoice->shop_address) {
            $this->order_invoice->shop_address = OrderInvoice::getCurrentFormattedShopAddress((int) $this->order->id_shop);
            if (!$bulk_mode) {
                OrderInvoice::fixAllShopAddresses();
            }
        }
    }
}
额外验证事项
  • 确认立陶宛语、拉脱维亚语、爱沙尼亚语已在后台正确安装,且发票模板的对应语言翻译已完成(进入国际>翻译,选择PDF模板进行语言翻译配置)。
  • 批量生成发票时,建议在处理完单个订单后恢复原Context语言,避免不同订单的语言设置互相干扰。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 16:24:13