0

I'm going to override module-fedex constructor, here is origin at \vendor\magento\module-fedex\Model\Carrier.php

public function __construct(
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
    \Psr\Log\LoggerInterface $logger,
    Security $xmlSecurity,
    \Magento\Shipping\Model\Simplexml\ElementFactory $xmlElFactory,
    \Magento\Shipping\Model\Rate\ResultFactory $rateFactory,
    \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
    \Magento\Shipping\Model\Tracking\ResultFactory $trackFactory,
    \Magento\Shipping\Model\Tracking\Result\ErrorFactory $trackErrorFactory,
    \Magento\Shipping\Model\Tracking\Result\StatusFactory $trackStatusFactory,
    \Magento\Directory\Model\RegionFactory $regionFactory,
    \Magento\Directory\Model\CountryFactory $countryFactory,
    \Magento\Directory\Model\CurrencyFactory $currencyFactory,
    \Magento\Directory\Helper\Data $directoryData,
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\Module\Dir\Reader $configReader,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    array $data = [],
    Json $serializer = null
) {
    $this->_storeManager = $storeManager;
    $this->_productCollectionFactory = $productCollectionFactory;
    parent::__construct(
        $scopeConfig,
        $rateErrorFactory,
        $logger,
        $xmlSecurity,
        $xmlElFactory,
        $rateFactory,
        $rateMethodFactory,
        $trackFactory,
        $trackErrorFactory,
        $trackStatusFactory,
        $regionFactory,
        $countryFactory,
        $currencyFactory,
        $directoryData,
        $stockRegistry,
        $data
    );
    $wsdlBasePath = $configReader->getModuleDir(Dir::MODULE_ETC_DIR, 'Magento_Fedex') . '/wsdl/';
    $this->_shipServiceWsdl = $wsdlBasePath . 'ShipService_v10.wsdl';
    $this->_rateServiceWsdl = $wsdlBasePath . 'RateService_v10.wsdl';
    $this->_trackServiceWsdl = $wsdlBasePath . 'TrackService_v' . self::$trackServiceVersion . '.wsdl';
    $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
}

Here is my custom carrier.php

namespace Custom\Fedex\Model;

use Magento\Framework\Module\Dir;
use Magento\Framework\Serialize\Serializer\Json;

class Carrier extends \Magento\Fedex\Model\Carrier
{
    public function __construct(
        \Magento\Framework\Module\Dir\Reader $configReader,
        Json $serializer = null
    ) {
        parent::__construct();
        $wsdlBasePath = $configReader->getModuleDir(Dir::MODULE_ETC_DIR, 'Magento_Fedex') . '/wsdl/';
        $this->_shipServiceWsdl = $wsdlBasePath . 'ShipService_v10.wsdl';
        $this->_rateServiceWsdl = $wsdlBasePath . 'RateService_v10.wsdl';
        $this->_trackServiceWsdl = $wsdlBasePath . 'TrackService_v' . parent::$trackServiceVersion . '.wsdl';
        $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
    }

}

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function Magento\\Fedex\\Model\\Carrier::__construct(), 0 passed in /home/m22/public_html/app/code/Custom/Fedex/Model/Carrier.php on line 14 and at least 18 expected in /home/m22/public_html/vendor/magento/module-fedex/Model/Carrier.php:172, referer: http://example.com/checkout/

What is the best practice to override a constructor?

0

If you are overriding any class you have to pass values in parent :: construct

like you counstruct should be

 public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
\Psr\Log\LoggerInterface $logger,
Security $xmlSecurity,
\Magento\Shipping\Model\Simplexml\ElementFactory $xmlElFactory,
\Magento\Shipping\Model\Rate\ResultFactory $rateFactory,
\Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
\Magento\Shipping\Model\Tracking\ResultFactory $trackFactory,
\Magento\Shipping\Model\Tracking\Result\ErrorFactory $trackErrorFactory,
\Magento\Shipping\Model\Tracking\Result\StatusFactory $trackStatusFactory,
\Magento\Directory\Model\RegionFactory $regionFactory,
\Magento\Directory\Model\CountryFactory $countryFactory,
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
\Magento\Directory\Helper\Data $directoryData,
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Module\Dir\Reader $configReader,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory 
 $productCollectionFactory,
array $data = [],
Json $serializer = null
  ) {
$this->_storeManager = $storeManager;
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct(
    $scopeConfig,
    $rateErrorFactory,
    $logger,
    $xmlSecurity,
    $xmlElFactory,
    $rateFactory,
    $rateMethodFactory,
    $trackFactory,
    $trackErrorFactory,
    $trackStatusFactory,
    $regionFactory,
    $countryFactory,
    $currencyFactory,
    $directoryData,
    $stockRegistry,
    $data
);
5
  • Uncaught TypeError: Argument 16 passed to Magento\\Fedex\\Model\\Carrier::__construct() must implement interface Magento\\Store\\Model\\StoreManagerInterface, array given, called in /home/m22/public_html/app/code/Custom/Fedex/Model/Carrier.php on line 199 and defined in /home/m22/public_html/vendor/magento/module-fedex/Model/Carrier.php:172, referer: example.com/checkout i have this error if use whole constructor
    – hkguile
    Aug 20 '18 at 8:28
  • Then you dont't need to __construct counstructor in your model Try after removing counstructor Aug 20 '18 at 8:31
  • but i need to rewrite wsdlBasePath which defined in constructor
    – hkguile
    Aug 20 '18 at 8:32
  • you can get it by making function functionName(){ $wsdlBasePath = $this->getModuleDir(Dir::MODULE_ETC_DIR, 'Magento_Fedex') . '/wsdl/'; } Aug 20 '18 at 8:34
  • @RutveeSojitra, could you please have a look at my post on thehttps://magento.stackexchange.com/questions/340018/magento-2-4-2-overriding-carrier-php-of-magento-fedex-module-doesnt-work Jun 20 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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