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;
}
}
1 Answer 1
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
-
I haven't used because in main original class the methods are private methods. e.g. private $asyncHttpClient;Dinesh Rajput– Dinesh Rajput2020年01月06日 12:24:16 +00:00Commented Jan 6, 2020 at 12:24
-
Can you pls post full code ? its still not workingDinesh Rajput– Dinesh Rajput2020年01月06日 12:29:47 +00:00Commented Jan 6, 2020 at 12:29
-
please confirm exact version of magentoBarry– Barry2020年01月06日 12:34:19 +00:00Commented Jan 6, 2020 at 12:34
-
My version of Magento is 2.3.3Dinesh Rajput– Dinesh Rajput2020年01月06日 12:35:02 +00:00Commented Jan 6, 2020 at 12:35
di:compile(required when changing/adding constructor parameters)generated/codeandgenerated/metadata?$objectManager = \Magento\Framework\App\ObjectManager::getInstance();. And no in general it is not the best practice, using the constructor parameters is.