2

I have a module which is rewriting core module (module-fedex), the constructor is the following:

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,
    \Magento\Checkout\Model\Cart $cart
) {
    $this->_storeManager = $storeManager;
    $this->_productCollectionFactory = $productCollectionFactory;
    $this_cart = $cart;
    parent::__construct(
        $scopeConfig,
        $rateErrorFactory,
        $logger,
        $xmlSecurity,
        $xmlElFactory,
        $rateFactory,
        $rateMethodFactory,
        $trackFactory,
        $trackErrorFactory,
        $trackStatusFactory,
        $regionFactory,
        $countryFactory,
        $currencyFactory,
        $directoryData,
        $stockRegistry,
        $storeManager,
        $configReader,
        $productCollectionFactory,
        $data = [],
        $serializer,
        $cart
    );



}

After i added \Magento\Checkout\Model\Cart $cart, there is an error

Exception #0 (BadMethodCallException): Missing required argument $data

anyone know what is the problem?

  • All constructor arguments with default values must be at the end of the list of arguments. – Shawn Abramson Aug 22 '18 at 5:48
5

Your construct 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,
\Magento\Checkout\Model\Cart $cart,
array $data = [],
Json $serializer = null

) {
$this->_storeManager = $storeManager;
$this->_productCollectionFactory = $productCollectionFactory;
$this_cart = $cart;
parent::__construct(
    $scopeConfig,
    $rateErrorFactory,
    $logger,
    $xmlSecurity,
    $xmlElFactory,
    $rateFactory,
    $rateMethodFactory,
    $trackFactory,
    $trackErrorFactory,
    $trackStatusFactory,
    $regionFactory,
    $countryFactory,
    $currencyFactory,
    $directoryData,
    $stockRegistry,
    $storeManager,
    $configReader,
    $productCollectionFactory,
    $data,
    $serializer
);

Replace your __construct with this code and check after rm -rf generated/* and php bin/magento cache:clean

  • if remove array and serializer in parent::construct, there is an error PHP Fatal error: Uncaught TypeError: Argument 19 passed to Magento\\Fedex\\Model\\Carrier::__construct() must be of the type array, object given, – hkguile Aug 22 '18 at 4:46
  • @hkguile i have updated my answer try now. – Rutvee Sojitra Aug 22 '18 at 4:49
  • @hkguile, she is wright you just need to check your DOC comment which we use before method. – kunj Aug 22 '18 at 5:15

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.