0

I can not seem to override the Magento Invoice class.

I've created a module, installed it, enabled it. I have the following in my 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="Magento\Sales\Model\Order\Pdf\Invoice" type="Dojo\InvoiceBarcodes\Model\Order\Pdf\Invoice" />
</config>

I've run the following commands:

./bin/magento cache:flush
./bin/magento setup:di:compile

If I run dev:di:info, it shows my class as a preference...

$ ./bin/magento dev:di:info "Magento\Sales\Model\Order\Pdf\Invoice"

DI configuration for the class Magento\Sales\Model\Order\Pdf\Invoice in the GLOBAL area

Preference: Dojo\InvoiceBarcodes\Model\Order\Pdf\Invoice

Constructor Parameters:
+-------------------+-----------------------------------------------------+-----------------------------------------------------------------------+
| Name              | Requested Type                                      | Configured Value                                                      |

I can verify it's not using my class because if I alter the Magento Invoice file, it reflects when I click print invoice.

I've checked all the logs, and there are no errors.

I'm not sure if I'm doing something wrong, if you can't override this class, or need to override in a different way.

Magento version 2.3.2

Any help would be appreciated.

1
  • which file you want to override in pdf invoice ?? Commented Apr 16, 2020 at 20:05

2 Answers 2

0

I have create the custom module check this code :-

Mage/Mohit/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Mage_Mohit',
    __DIR__
);

Mage/Mohit/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Mage_Mohit" setup_version="1.0.0"/>
</config>

Mage/Mohit/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="Mage\Mohit\Model\Order\Pdf\Invoice" />
</config>

Mage/Mohit/Model/Order/Pdf/Invoice.php

<?php


namespace Mage\Mohit\Model\Order\Pdf;
class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
  // Add your file code 
}

finally run this command :-

rm -rf var/cache/*
rm -rf var/view_preprocessed/*
php bin/magento s:up
php bin/magento setup:di:compile
php bin/magento i:rei
php bin/magento c:c
php bin/magento c:f

Any query let me know ...

Thanks.

0

Thanks for the help.

In this case, another module was already trying to override the same class. I had to switch my code to override that module's class. Rather than the core magento class.

2
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
    – Jarnail S
    Commented Apr 17, 2020 at 17:18
  • The answer provided by @mohit-patel is the correct way to override the PDF class, it is also how my code was structured, and how several other examples on the internet show how to override the class. In my case, there was a different underlying issue, which I could not find an answer for on the internet, or even pointers to troubleshoot. While I agree the answer isn't great, I believe it does provide some value. Maybe it or the question should be revised? If not feel free to delete, or mark the other answer.. Just trying to save someone else a headache. Commented Apr 18, 2020 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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