1

I have installed the WebShopApps module with composer hence it is installed in the Vendor folder. Now I want to override model in my custom theme. As we know we can't directly modify in vendor folder since changes will be gone once when we upgrade module.

Please let us know how to override the Model folder.

The file path is that want to override in my theme is

vendor/webshopapps/module-matrixrate/src/Model/Carrier/Matrixrate.php

Please help me.

2 Answers 2

3
  1. You could create another module to override the current one

app/code/Vendor/Module/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="WebShopApps\MatrixRate\Model\Carrier\Matrixrate" type="Vendor\Module\Model\Carrier\Matrixrate" />
</config>

app/code/Vendor/Module/Model/Carrier/Matrixrate.php

<?php

namespace Vendor\Module\Model\Carrier;

use Magento\Quote\Model\Quote\Address\RateRequest;

class Matrixrate extends \WebShopApps\MatrixRate\Model\Carrier\Matrixrate
    implements \Magento\Shipping\Model\Carrier\CarrierInterface
{
    protected $helper;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
        \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $resultMethodFactory,
        \WebShopApps\MatrixRate\Model\ResourceModel\Carrier\MatrixrateFactory $matrixrateFactory,
        array $data = []
    )
    {
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $rateResultFactory, $resultMethodFactory, $matrixrateFactory, $data);
    }

    /**
     * @param \Magento\Quote\Model\Quote\Address\RateRequest $request
     * @return bool|\Magento\Shipping\Model\Rate\Result
     */
    public function collectRates(RateRequest $request)
    {

        // Do some stuff here
        return parent::collectRates($request);
    }

}

app/code/Vendor/MatrixRate/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_MatrixRate" setup_version="1.0.0">
        <sequence>
            <module name="WebShopApps_MatrixRate"/>
        </sequence>
    </module>
</config>

app/code/Vendor/MatrixRate/composer.json

{
    "name": "vendor/matrixrate",
    "description": "",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "webshopapps/module-matrixrate": "null",

        "magento/magento-composer-installer": "*"
    },
    "suggest": {

    },
    "type": "magento2-module",
    "version": "1.0.0",
    "license": [

    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Vendor\\MatrixRate\\": ""
        }
    },
    "extra": {
        "map": [
            [
                "*",
                "Vendor/MatrixRate"
            ]
        ]
    }
}

app/code/Vendor/MatrixRate/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_MatrixRate',
    __DIR__
);
  1. You could use composer patcher to patch directly to vendor folder

Reference link

1

As I could understand from your question, you want to make some customizations in the Model class functions. The file you have mentioned has all public functions, so if you want, you can easily create plugins for the class functions to modify the functionality of the class file you have mentioned.

If you want to modify other functions which are not public, you can use preference to override the functionality.

You can check the difference between a plugin and class rewrite at Magento2: what is the basic difference between plugin and preference?

Please let me know if it helped.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.