6

In magento2 - I want to use custom price for one of my shipping method per quote.

I want to show custom amount for only current quote. how to do it. Like there is a shipping method. "CUSTOM FLAT RATE"

Now if

customer A is buying 2 product i want to show amount shipment method amount for him $22,

customer A is buying 2 product i want to show amount shipment method amount for him $35,

customer A is buying 2 product i want to show amount shippment method amount for him $87

I want to update it pragmatically on checkout page before load shipping method for current quote only.

0

I have shown you here only with one custom price. But you can set your dynamic logic with the use of conditions according to your need.

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.