Prestashop修改PDF发票生成文件名,替换为用户名
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
Locate the core filename method
Navigate to your PrestaShop installation’sclasses/order/Order.phpfile. Look for thegetInvoiceFileName()function (usually around line 2000+, depending on your exact version).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'; }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:
- Open
classes/PDF.phpand find thegenerateInvoice()method. - Locate the line defining the filename, which might look like:
$filename = 'invoice' . $order->reference . '.pdf'; - 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

