0

I am trying to override UPS shipping Carrier. But it is not override.

etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <preference for="Magento\Ups\Model\Carrier" type="Sarvesh\Tiwari\Model\Carrier" />    
</config>

Sarvesh/Tiwari/Model/Carrier

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

namespace Sarvesh\Tiwari\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;

/**
 * UPS shipping implementation
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Carrier extends \Magento\Ups\Model\Carrier
{
    public function collectRates(RateRequest $request)
{
    $this->setRequest($request);
    if (!$this->canCollectRates()) {
        return $this->getErrorMessage();
    }

    $this->setRequest($request);
    $this->_result = $this->_getQuotes();
    $this->_updateFreeMethodQuote($request);
   return $this->getResult();
}
}

but its not working anyone can help

6
  • which method you want to override form the carrier class? May 16, 2019 at 5:58
  • protected function _getCgiQuotes() its returning the shipping rate... May 16, 2019 at 6:14
  • so what are you facing the issue? May 16, 2019 at 6:17
  • its overriding successfully when i am calling this model in another model in that case checkout page is not loading May 16, 2019 at 6:43
  • I am creating a custom shipping method and in the custom shipping model try to use method of UPS carrier model May 16, 2019 at 6:44

1 Answer 1

1

vendor/module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Ups\Model\Carrier">
        <plugin name="change-Ups" type="Vendor\Module\Model\Carrier\Ups" sortOrder="1" />
    </type>
</config>

vendor/module/Model/Carrier/Ups.php

<?php
namespace Vendor\Module\Model\Carrier;
class Ups
{
    protected $_scopeConfig;
    protected $_customerSession;
    protected $_rateResultFactory;
    protected $_resultMethodFactory;
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\OfflineShipping\Model\Carrier\Tablerate $tablerate,
        \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $resultMethodFactory,
        \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->_storeManager = $storeManager;
        $this->_scopeConfig = $scopeConfig;
        $this->tablerate = $tablerate;
        $this->_rateResultFactory = $rateResultFactory;
        $this->productFactory = $productFactory;
        $this->_resultMethodFactory = $resultMethodFactory;
        $this->_customerSession = $customerSession;
    }

    public function afterCollectRates(\Magento\Ups\Model\Carrier $subject, $result, $request)
    {
        $shippingPrice = 50;
        foreach ($result->getAllRates() as $method) {
            $newPrice = $method->getPrice() + 23;
            $method->setPrice($newPrice);
        }
        //return false; //if you want to disable it
        return $result;
    }
}
?>

Your Answer

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

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