I'm trying to edit a function from a third party extension, in this case, the moloni one. I want to override a function that is in Vendor\moloni\magento2\libraries\MoloniLibrary\Controllers\Products.php

Until now I have a folder in app\code with




    <?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="moloni\magento2\Libraries\MoloniLibrary\Controllers\Products" type="Vendor\Moloni\Libraries\MoloniLibrary\Products" />


    <?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_Moloni" setup_version="1.0.1">



namespace Vendor\Moloni\Libraries\MoloniLibrary;

use moloni\magento2\libraries\MoloniLibrary\Controllers\Products as vendormoloni;
use Invoicing\Moloni\Libraries\MoloniLibrary\Moloni;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\ResourceModel\Order\Tax\Item;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Model\Category as CategoryModel;
use Magento\Tax\Api\TaxCalculationInterface;

class Products extends vendormoloni
     * @param \Magento\Sales\Api\Data\OrderInterface $order
     * @return array
    public function setShippingFromOrder($order)
      //the code of the original I want to override

It does not work, what am I missing? When I try to di:compile it sais

PHP Fatal error:  Class 'moloni\magento2\libraries\MoloniLibrary\Controllers\Products' not found in /var/www/html/magento2/app/code/Vendor/Moloni/Libraries/Products.php on line 14

but the path is right, I think :D

Thanks in advance, Regards

edit I got working I think but it gives me an error here

 * @var Tools
private $tools;

telling me that Class Custom\Moloni\Libraries\MoloniLibrary\Tools does not exist

what can I do?


To extend any third-party Module's functionality you can use 2 ways as Override their class using preference or plugin.

But If you want to add your custom methods into their class you can choose preference.

Other than that you have to set dependencey as sequence in your custom module's etc/module.xml file

<?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_Modulename>" setup_version="1.0.0">
            <module name="<ThirdPartyVendor_ThirdPartyModulename"/>         

If you directly change code of that third-party module or place any method into that third-party module's class that will be removed once new update will released.

So Only solution is either Override whole class and add your logic into that class or you can use around Plugin to extend logic of the method.

If you are looking to change into phtml file or xml file that you can do directly place that file into your module using same path.

Hope it helps..


Works the same as Magento based modules.

You need to extend using the classname rather than the path, since composer modules can declare different folder structures in their composer.json.

The easiest way to work out what you need to extend is open up the Vendor\moloni\magento2\libraries\MoloniLibrary\Controllers\Products.php file.

Take the namespace declaration and prepend the class name to it.

For example the following would be Moloni\MoloniLibrary\Controllers\Products


namespace Moloni\MoloniLibrary\Controllers;

class Products extends x

Also you don't need to name your new module Vendor_xxx. It is best practise to use your vendor name to prevent confusion on who wrote the module, so you might name it along the lines of Rui_MoloniLibrary

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.