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;
}
}
di:compile
(required when changing/adding constructor parameters)generated/code
andgenerated/metadata
?$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
. And no in general it is not the best practice, using the constructor parameters is.