3

Magento 2:

I was looking to add a custom text on the footer in pdf invoice. I have done this by adding a small piece of code here in this file

vendor/magento/module-sales/Model/Order/Pdf/Invoice.php

I know it's not good to edit core files, so how can i override this file?

I tried by placing the same file with directories in app/code/Local/Sales/Model/Order/Pdf/Invoice.php but not working.

Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155
Waqas Shakeel
  • 81
  • 1
  • 7

2 Answers2

2

Seem that you tried to follow the Magento 1 way. In Magento 2, there are no local, community code pool.

We need to use Dependency injection to override the class.

app/code/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\Model\Order\Pdf\Invoice"
                type="Vendor\Module\Model\Order\Pdf\Invoice"/>
</config>

app/code/Vendor/Module/Model/Order/Pdf/Invoice.php

<?php

namespace Vendor\Module\Model\Order\Pdf;

class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
  //Your custom code lines
}

Recommended way: We can use Plugin to modify the behaviour of methods in Magento 2: http://devdocs.magento.com/guides/v2.1/extension-dev-guide/plugins.html

David Duong
  • 803
  • 1
  • 11
  • 37
Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155
  • I have tried with same but not working. app/code/Vendor/Module/Model/Order/Pdf/Invoice.php in this file i override the getpdf function.

    public function getPdf($invoices = []) { $this->_drawFooter($page);
    $this->_afterGetPdf(); return $pdf; }

    protected function _drawFooter(\Zend_Pdf_Page $page)
    {
       $this->y =70;
       $page->drawText("Tel: +123 456 676", 230, $this->y, 'UTF-8');
    }
    
    – Waqas Shakeel Jan 15 '17 at 14:05
  • You know how to create a custom module in Magento 2? http://magento.stackexchange.com/questions/122287/how-to-create-simple-module-in-magento-2 – Khoa TruongDinh Jan 15 '17 at 14:08
  • We can use Plugin: http://devdocs.magento.com/guides/v2.1/extension-dev-guide/plugins.html. – Khoa TruongDinh Jan 15 '17 at 16:15
  • @WaqasShakeel we need to learn more about Magento 2. Magento 2 is a big application. We need to spend more time to understand Magento system. The good place to start is : http://devdocs.magento.com/ – Khoa TruongDinh Jan 15 '17 at 16:16
0

For Magento 2.3.5 you have to:

  • Create your own module in /app/code (example: Vendor/PdfInvoice )
  • Create a file in Vendor/PdfInvoice/registration.php
  • Create a file in Vendor/PdfInvoice/etc/di.xml
  • Create a file in Vendor/PdfInvoice/etc/module.xml
  • Override the section concerned without forgeting to mention in your di.xml
  • Disable cache ( bin/magento cache:disable )
  • Activate your new module: bin/magento module:enable Vendor_PdfInvoice & bin/magento setup:upgrade
  • Enable cache ( php bin/magento cache:enable )
  • Disco
DDA
  • 1