1

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

app\code\Vendor\Moloni\registration.php

 <?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Moloni',
__DIR__
);

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

app\code\Vendor\Moloni\etc\module.xml

 <?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">
</module>
</config>

app\code\Vendor\Moloni\Libraries\MoloniLibrary\Products.php

 <?php 
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?

asked Feb 24, 2020 at 15:45

2 Answers 2

0

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">
 <sequence>
 <module name="<ThirdPartyVendor_ThirdPartyModulename"/> 
 </sequence>
 </module>
</config>

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..

answered Feb 24, 2020 at 18:26
1

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

<?php
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

answered Feb 24, 2020 at 18:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.