首页 > 解决方案 > Magento 2更改pdf文件名

问题描述

我想更改 Magento 2 中 PDF 文件的文件名。

默认名称不清楚,我希望发票在保存到我的电脑上的某个位置时可以搜索。

是否可以将 Magento 2 中 PDF 文件的文件名更改为“invoice_1000000123.pdf”之类的格式?

标签: pdffilenamesmagento2

解决方案


永远不应该编辑核心文件。说真的,不要。

由于/vendor/magento/module-sales/Controller/Adminhtml/Invoice/AbstractInvoice/PrintAction.php是一个抽象类,您必须在模块中使用插件或首选项才能覆盖它。

你需要做到这一点:

通常的最小文件: /Vendor/Module/composer.json, /Vendor/Module/registration.php,/Vendor/Module/etc/module.xml

/Vendor/Module/etc/module.xml你应该排序 Magento_Sales

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>

然后您可以使用插件或首选项/Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction" type="Vendor\Module\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction"/>      
</config>

插件看起来像这样:

<type name="Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction">
    <plugin name="Vendor_Module::aroundPrintInvoice" type="Vendor\Module\Block\Class" sortOrder="0"/>
</type>

现在将 a 添加PrintAction.php到您的首选项/插件中指定的路径(首选项示例)

<?php
namespace Vendor\Module\Controller\Adminhtml\Invoice\AbstractInvoice;

class PrintAction extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction
{
    /* Write code here */
}

推荐阅读