0

We need to override following files in custom module.

/data/magento2/vendor/magento/module-sales/Model/Order/Pdf/AbstractPdf.php
/data/magento2/vendor/magento/module-sales/Model/Order/Pdf/Invoice.php
/data/magento2/vendor/magento/module-sales/Model/Order/Pdf/Items/AbstractItems.php
/data/magento2/vendor/magento/module-sales/Model/Order/Pdf/Items/Invoice/DefaultInvoice.php

We've tried many different ways but that didn't work. So Please help me the proper ways to override vendor files in App/Code. Magento 2.2

  • so do you have final solution? can you share your experience how did you get it done finally?Thanks – Paul Fan Dec 16 '19 at 19:07
  • 1
    Check my answer. Someone gave me di.xml example So based on that I created new Module and Following are XML which I created. – KHIMAJI VALUKIYA Dec 18 '19 at 4:49
1

Please Try Following methods in 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\Model\Order\Pdf\Invoice" type="ExtensionName\Modulename\Model\Order\Pdf\Invoice"/>
    <preference for="Magento\Sales\Model\Order\Pdf\AbstractPdf" type="ExtensionName\Modulename\Model\Order\Pdf\AbstractPdf"/>
</config>

Hope it will work for you.

1

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="Fooman\EmailAttachments\Observer\BeforeSendInvoiceObserver" type="Myvendorfolder\EmailAttachments\Observer\BeforeSendInvoiceObserver" />
        <virtualType name="myvendorfolder_emailattachments_invoice_pdf_renderer" type="\Myvendorfolder\EmailAttachments\Model\PdfRenderer">
            <arguments>
                <argument name="pdfRenderer" xsi:type="object">\Magento\Sales\Model\Order\Pdf\Invoice</argument>
            </arguments>
        </virtualType>
        <type name="\Myvendorfolder\EmailAttachments\Observer\AbstractSendInvoiceObserver">
            <arguments>
                <argument name="pdfRenderer" xsi:type="object">myvendorfolder_emailattachments_invoice_pdf_renderer</argument>
            </arguments>
        </type>
    </config>

events.xml

<?xml version="1.0"?>
    <!--
        /**
        * @author     Kristof Ringleff
        * @package    Fooman_EmailAttachments
        * @copyright  Copyright (c) 2015 Fooman Limited (http://www.fooman.co.nz)
        *
        * For the full copyright and license information, please view the LICENSE
        * file that was distributed with this source code.
        */
    -->
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <!--Invoice Emails-->
        <event name="myvendorfolder_emailattachments_before_send_invoice">
            <observer name="myvendorfolder_emailattachments_before_send_invoice_observer"
                      instance="Myvendorfolder\EmailAttachments\Observer\BeforeSendInvoiceObserver"/>
        </event>
    </config>

module.xml

<?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="Myvendorfolder_EmailAttachments" setup_version="2.1.0">
            <sequence>
                <module name="Fooman_EmailAttachments"/>
            </sequence>
        </module>
    </config>
0

Please Try Below Method in Your Module di.xml File

<?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="vendor/magento/module-sales/Model/Order/Pdf/AbstractPdf" type="Your_ExtensionName/Your_ModuleName/Model/Order/Pdf/AbstractPdf" />
<preference for="vendor/magento/module-sales/Model/Order/Pdf/Invoice" type="Your_ExtensionName/Your_ModuleName/Model/Order/Pdf/Invoice" />
<preference for="vendor/magento/module-sales/Model/Order/Pdf/Items/AbstractItems" type="Your_ExtensionName/Your_MOduleName/Model/Order/Pdf/Items/AbstractItems" />
<preference for="vendor/magento/module-sales/Model/Order/Pdf/Items/Invoice/DefaultInvoice" type="Your_ExtensionName/Your_MOduleName/Model/Order/Pdf/Items/Invoice/DefaultInvoice" />
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.