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

Prestashop修改PDF发票生成文件名,替换为用户名

How to Rename PrestaShop PDF Invoices to Use Customer Names

Hey there! I’ve helped a few folks tweak their PrestaShop invoice filenames before, so let’s get this sorted for you. Here’s a step-by-step guide tailored to common PrestaShop versions:

For PrestaShop 1.7.x

  1. Locate the core filename method
    Navigate to your PrestaShop installation’s classes/order/Order.php file. Look for the getInvoiceFileName() function (usually around line 2000+, depending on your exact version).

  2. Update the filename logic
    The default code typically looks like this:

    public function getInvoiceFileName()
    {
        return 'invoice' . $this->reference . '.pdf';
    }
    

    Replace it with code that pulls the customer’s full name, cleans it to avoid invalid characters, and optionally adds the invoice reference to prevent duplicates:

    public function getInvoiceFileName()
    {
        // Load the customer using the order's linked customer ID
        $customer = new Customer($this->id_customer);
        // Combine first and last name
        $customerName = $customer->firstname . ' ' . $customer->lastname;
        // Sanitize the name to remove spaces, accents, or special characters that break filenames
        $cleanFileName = Tools::sanitizeFileName($customerName);
        // Optional: Add invoice reference to avoid overwriting files for repeat customers
        return $cleanFileName . '-' . $this->reference . '.pdf';
        // Skip the reference if you prefer: return $cleanFileName . '.pdf';
    }
    
  3. Save and test
    Save the modified file, then generate a test invoice from your admin dashboard. You should now see the filename uses the customer’s name instead of the default "invoiceXXXX" format.

For PrestaShop 1.6.x

The workflow is similar, but the file location shifts:

  1. Open classes/PDF.php and find the generateInvoice() method.
  2. Locate the line defining the filename, which might look like:
    $filename = 'invoice' . $order->reference . '.pdf';
    
  3. Replace that line with the customer name logic:
    $customer = new Customer($order->id_customer);
    $customerName = $customer->firstname . ' ' . $customer->lastname;
    $cleanFileName = Tools::sanitizeFileName($customerName);
    $filename = $cleanFileName . '-' . $order->reference . '.pdf';
    

Key Tips:

  • Backup first: Always make a copy of the original file before editing—this lets you revert if something goes wrong.
  • Avoid duplicates: If you have customers with identical names, adding the invoice reference ensures you won’t overwrite existing files.
  • Sanitize is non-negotiable: Customer names often have accents, spaces, or special characters; Tools::sanitizeFileName() fixes these to prevent download errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:27:32