0

I am not getting here that how can we add additional rate in any shipping method. Do anyone have solution for it?

I have tried to do it with below link's logic but it is not working. Referred link - Override Shipping Price in Magento 2

0

You can do it by overriding address rate file in your custom module. Here is the way that how can you add it?

PackageName/CustomShip/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\Quote\Model\Quote\Address\Rate" type="PackageName\CustomShip\Model\Rewrite\Quote\Address\Rate" />
</config>

Please create Rate.php file in path - PackageName/CustomShip/Model/rewrite/Quote/Address/Rate.php

<?php
 
namespace PackageName\CustomShip\Model\Rewrite\Quote\Address;

use \Magento\Checkout\Model\Session as CheckoutSession;

class Rate extends \Magento\Quote\Model\Quote\Address\Rate
{
    public function importShippingRate(\Magento\Quote\Model\Quote\Address\RateResult\AbstractResult $rate)
    {
        $addtionalPrice = 10;
        $finalPrice = $rate->getPrice() + $addtionalPrice;

        if ($rate instanceof \Magento\Quote\Model\Quote\Address\RateResult\Error) {
            $this->setCode(
                $rate->getCarrier() . '_error'
            )->setCarrier(
                $rate->getCarrier()
            )->setCarrierTitle(
                $rate->getCarrierTitle()
            )->setErrorMessage(
                $rate->getErrorMessage()
            );
        } elseif ($rate instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method) {
            $this->setCode(
                $rate->getCarrier() . '_' . $rate->getMethod()
            )->setCarrier(
                $rate->getCarrier()
            )->setCarrierTitle(
                $rate->getCarrierTitle()
            )->setMethod(
                $rate->getMethod()
            )->setMethodTitle(
                $rate->getMethodTitle()
            )->setMethodDescription(
                $rate->getMethodDescription()
            )->setPrice(
                $finalPrice
            );
        }
        return $this;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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