cancel
Showing results for 
Search instead for 
Did you mean: 

Override Third Party Module

SOLVED

Override Third Party Module

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
I tried to create a di.xml in
code\Magento\Moloni\Libraries\MoloniLibrary\etc\di.xml
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="Vendor\moloni\magento2\Libraries\MoloniLibrary\Controllers\Products" type="Moloni\Libraries\MoloniLibrary\Controllers\Products" />
</config>
 
and then in
\code\Magento\Moloni\Libraries\MoloniLibrary\Controllers\Products
this
<?php
namespace Moloni\Libraries\MoloniLibrary\Controllers\Products;

class Products extends \Vendor\moloni\magento2\Libraries\MoloniLibrary\Controllers\Products {
   
//All the code from the original file

}
but does not work, what am I doing wrong?
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Override Third Party Module

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.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

View solution in original post

4 REPLIES 4

Re: Override Third Party Module

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


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Override Third Party Module

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

Re: Override Third Party Module

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.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Override Third Party Module

Hey,

 

it works just fine!

Thanks Smiley Very Happy