Hey,
So I want to extend a Third Party plugin, in this case the Moloni Plugin for Magento 2.
I want to override a function in this file
\Vendor\moloni\magento2\Libraries\MoloniLibrary\Controllers\Products.php
code\Magento\Moloni\Libraries\MoloniLibrary\etc\di.xmlwith
<?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\moloni\magento2\Libraries\MoloniLibrary\Controllers\Products" type="Moloni\Libraries\MoloniLibrary\Controllers\Products" /> </config>
\code\Magento\Moloni\Libraries\MoloniLibrary\Controllers\Productsthis
<?php namespace Moloni\Libraries\MoloniLibrary\Controllers\Products; class Products extends \Vendor\moloni\magento2\Libraries\MoloniLibrary\Controllers\Products { //All the code from the original file }
Solved! Go to Solution.
Hello @rui_silva1
Try the below code 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="Invoicing\Moloni\Libraries\MoloniLibrary\Controllers\Products" type="[Vendor]\[Module]\Libraries\Products" /> </config>
In Products.php,
<?php namespace [Vendor]\[Module]\Libraries; use Invoicing\Moloni\Libraries\MoloniLibrary\Controllers\Products as vendormoloni class Products extends vendormoloni { // code }
Here, [Vendor] and [Module] is your custom/existing module/plugin.
Note:
To override vendormoloni not extends/inherit private variable and function you redefine it.
Thanks.
Hello @rui_silva1
Seems like you mention wrong path.
Try to open that controller file and take namespace and file name and put into di.xml
hope it will help you.
If works then mark as a solution
Hello @Sunil Patel ,
Thanks for the reply.
In the mean time I tried some things, my di.xml it's like this
<?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\Products" /> </config>
and the Products.php
<?php namespace Vendor\Moloni\Libraries; use moloni\magento2\Libraries\MoloniLibrary\Controllers\Products as vendormoloni; class Products extends vendormoloni { //Code }
i made a registration.php and all worked until I run di:compile that tells me that in Products.php the file orginal it's not found in this path, but isn't that the path? do I need to add the Vendor\ ?
moloni\magento2\Libraries\MoloniLibrary\Controllers\Products
and if I remove the as and extend the error is that the name already exists
Hello @rui_silva1
Try the below code 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="Invoicing\Moloni\Libraries\MoloniLibrary\Controllers\Products" type="[Vendor]\[Module]\Libraries\Products" /> </config>
In Products.php,
<?php namespace [Vendor]\[Module]\Libraries; use Invoicing\Moloni\Libraries\MoloniLibrary\Controllers\Products as vendormoloni class Products extends vendormoloni { // code }
Here, [Vendor] and [Module] is your custom/existing module/plugin.
Note:
To override vendormoloni not extends/inherit private variable and function you redefine it.
Thanks.
Hey,
it works just fine!
Thanks