Magento 2 – Fixing Exception #0 (BadMethodCallException): Missing Required Argument $data

dependency-injectionmagento2

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?

Best Answer

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

Related Topic