TransWikia.com

how to override AbstractCarrierOnline class function in Magento 2

Magento Asked by bhagwandernase on October 1, 2021

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.

2 Answers

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="MagentoShippingModelCarrierAbstractCarrierOnline">
        <plugin disabled="false" name="StackExchange_MagentoTest_Plugin_Magento_Shipping_Model_Carrier_AbstractCarrierOnline" sortOrder="10" type="StackExchangeMagentoTestPluginMagentoShippingModelCarrierAbstractCarrierOnline"/>
    </type>
</config>

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

<?php
declare(strict_types=1);

namespace StackExchangeMagentoTestPluginMagentoShippingModelCarrier;

class AbstractCarrierOnline
{

    public function afterProccessAdditionalValidation(
        MagentoShippingModelCarrierAbstractCarrierOnline $subject,
        $result,
        $request
    ) {
        // write your logic
        return $result;
    }
}

Correct answer by Amit Bera on October 1, 2021

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="MagentoShippingModelCarrierAbstractCarrierOnline" type="VendorModuleModelCarrierAbstractCarrierOnline" />
</config>

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

Now, create the AbstractCarrierOnline.php file under VendorModuleModelCarrier in your module.

<?php

namespace VendorModuleModelCarrier;

class AbstractCarrierOnline extends MagentoShippingModelCarrierAbstractCarrierOnline
{
    public function proccessAdditionalValidation()
    {
        //Add your code
    }
}

Hope this helps!

Answered by Bhaumik Upadhyay on October 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP