2

I have overridden the Magento/Ups/Model/Carrier.php to Custom/Module/Model/Carrier.php successfully. I need a cart quote from it, so I have inject the class in constructor.

When I add to cart a product and go to cart page the error:

Type Error occurred when creating object: Custom\Module\Model\Carrier, Argument 16 passed to Magento\Ups\Model\Carrier::__construct() must implement interface Magento\Framework\Locale\FormatInterface, array given, called in /var/www/html/xxx/app/code/Custom/Module/Model/Carrier.php on line 56

Adding the __construct() doesn't work in my custom module.

Custom/Module/Model/Carrier.php

<?php
namespace Custom\Module\Model;

use Magento\Quote\Model\Quote\Address\RateResult\Error;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Carrier\AbstractCarrierOnline;
use Magento\Shipping\Model\Carrier\CarrierInterface;
use Magento\Shipping\Model\Rate\Result;
use Magento\Shipping\Model\Simplexml\Element;
use Magento\Ups\Helper\Config;
use Magento\Framework\Xml\Security;

class Carrier extends \Magento\Ups\Model\Carrier
{
    protected $_cart;

    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\Framework\Locale\FormatInterface $localeFormat,
    array $data = [],
    \Magento\Checkout\Model\Cart $cart
    )
    {
    $this->_cart = $cart;
    parent::__construct(
        $scopeConfig,
        $rateErrorFactory,
        $logger,
        $xmlSecurity,
        $xmlElFactory,
        $rateFactory,
        $rateMethodFactory,
        $trackFactory,
        $trackErrorFactory,
        $trackStatusFactory,
        $regionFactory,
        $countryFactory,
        $currencyFactory,
        $directoryData,
        $stockRegistry,
        $data
    );
    $this->_localeFormat = $localeFormat;
    }


    protected function _parseCgiResponse($response)
    {
    //my custom logic goes here
    //get cart quote here
    return $result;
    }
}
12
  • What is the error you saw? Did you perform a di:compile (required when changing/adding constructor parameters)
    – Barry
    Jan 6, 2020 at 12:05
  • all commands run correctly but when I add to cart a product and go to cart page it give error like this:Type Error occurred when creating object: Custom\Module\Model\Carrier, Argument 16 passed to Magento\Ups\Model\Carrier::__construct() must implement interface Magento\Framework\Locale\FormatInterface, array given, called in /var/www/html/xxx/app/code/Custom/Module/Model/Carrier.php on line 56 [] [] Jan 6, 2020 at 12:06
  • I also need to ask, is it good practice to add Object Manager directly ,as the original class has objectmanager already defined in construct ? Jan 6, 2020 at 12:11
  • did you try to clear generated/code and generated/metadata?
    – Marius
    Jan 6, 2020 at 12:15
  • @DineshRajput you can "get" the object manager at any time regardless of if it being passed in a constructor - $objectManager = \Magento\Framework\App\ObjectManager::getInstance();. And no in general it is not the best practice, using the constructor parameters is.
    – Barry
    Jan 6, 2020 at 12:20

1 Answer 1

0

That sounds like you are not implementing the interface 100% or that setup:di:compile was not run after you did. I can check with Magento 2.3.3 and I see that you are missing the last 4 parameters:

Config $configHelper,
ClientFactory $httpClientFactory,
array $data = [],
?AsyncClientInterface $asyncHttpClient = null,
?ProxyDeferredFactory $proxyDeferredFactory

Please check your version of magento, if 2.3.3 then changing the last parameters of the construct (i.e. after \Magento\Framework\Locale\FormatInterface $localeFormat,) should fix it...

Config $configHelper,
ClientFactory $httpClientFactory,
array $data = [],
?AsyncClientInterface $asyncHttpClient = null,
?ProxyDeferredFactory $proxyDeferredFactory,
\Magento\Checkout\Model\Cart $cart 
4
  • I haven't used because in main original class the methods are private methods. e.g. private $asyncHttpClient; Jan 6, 2020 at 12:24
  • Can you pls post full code ? its still not working Jan 6, 2020 at 12:29
  • please confirm exact version of magento
    – Barry
    Jan 6, 2020 at 12:34
  • My version of Magento is 2.3.3 Jan 6, 2020 at 12:35

Your Answer

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

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