Question

I want to override "proccessAdditionalValidation" function of app/code/Magento/Shipping/Model/Carrier/AbstractCarrierOnline.php file in custom module.

Please help me and thanks in advance.

Was it helpful?

Solution

In this case, you can use Plugin as proccessAdditionalValidation is plugin method. You can use before or after or around as per as requirement.

Create di.xml at app/code/{Vendor}/{Modulename}/etc and where you define plugin class.

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Shipping\Model\Carrier\AbstractCarrierOnline">
        <plugin disabled="false" name="StackExchange_MagentoTest_Plugin_Magento_Shipping_Model_Carrier_AbstractCarrierOnline" sortOrder="10" type="StackExchange\MagentoTest\Plugin\Magento\Shipping\Model\Carrier\AbstractCarrierOnline"/>
    </type>
</config>

And Plugin class located at app/code/{Vendor}/{Modulename}/Plugin/Magento/Shipping/Model/Carrier/AbstractCarrierOnline.php

<?php
declare(strict_types=1);

namespace StackExchange\MagentoTest\Plugin\Magento\Shipping\Model\Carrier;

class AbstractCarrierOnline
{

    public function afterProccessAdditionalValidation(
        \Magento\Shipping\Model\Carrier\AbstractCarrierOnline $subject,
        $result,
        $request
    ) {
        // write your logic
        return $result;
    }
}

OTHER TIPS

To override the proccessAdditionalValidation function of /app/code/Magento/Shipping/Model/Carrier/AbstractCarrierOnline.php

In your custom module, add below code in your di.xml file to override the model class.

<?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\Shipping\Model\Carrier\AbstractCarrierOnline" type="Vendor\Module\Model\Carrier\AbstractCarrierOnline" />
</config>

where <preference for=""> is which Model to override and <preference type=""> for where to override.

Now, create the AbstractCarrierOnline.php file under Vendor\Module\Model\Carrier\ in your module.

<?php

namespace Vendor\Module\Model\Carrier;

class AbstractCarrierOnline extends \Magento\Shipping\Model\Carrier\AbstractCarrierOnline
{
    public function proccessAdditionalValidation()
    {
        //Add your code
    }
}

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top