Magento 2 How to get shipping method amount from Checkout/cart/index page Sidebar radio button selection

934 Views Asked by At

Query- I need to develop a functionality in which custom fee charge on order based upon shipping amount.

I got correct amount in fetch function when select different shipping method. But incorrect amount get in collect function when switch shipping method. So how can we get correct shipping method amount based upon shipping method selected.

<?php

namespace Nikunj\CustomFee\Model\Quote;

use Magento\Quote\Api\Data\ShippingAssignmentInterface as ShippingAssignmentInterfaceAlias;

class Customfee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
    /**
     * Collect grand total address amount
     *
     * @param \Magento\Quote\Model\Quote $quote
     * @param ShippingAssignmentInterfaceAlias $shippingAssignment
     * @param \Magento\Quote\Model\Quote\Address\Total $total
     * @return $this
     */
    protected $quoteValidator = null;

    public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator)
    {
        $this->quoteValidator = $quoteValidator;
    }

    public function collect(
        \Magento\Quote\Model\Quote               $quote,
        ShippingAssignmentInterfaceAlias         $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $total
    ) {
        parent::collect($quote, $shippingAssignment, $total);


        $exist_amount = 0; //$quote->getCustomfee();
        $customfee = ($quote->getShippingAddress()->getShippingAmount()+15);
        $balance = $customfee - $exist_amount;//final amount

        $total->setTotalAmount('customfee', $balance);
        $total->setBaseTotalAmount('customfee', $balance);

        $total->setCustomfee($balance);
        $total->setBaseCustomfee($balance);

        $total->setGrandTotal($total->getGrandTotal() + $balance);
        $total->setBaseGrandTotal($total->getBaseGrandTotal() + $balance);

        return $this;
    }

    protected function clearValues(Address\Total $total)
    {
        $total->setTotalAmount('subtotal', 0);
        $total->setBaseTotalAmount('subtotal', 0);
        $total->setTotalAmount('tax', 0);
        $total->setBaseTotalAmount('tax', 0);
        $total->setTotalAmount('discount_tax_compensation', 0);
        $total->setBaseTotalAmount('discount_tax_compensation', 0);
        $total->setTotalAmount('shipping_discount_tax_compensation', 0);
        $total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
        $total->setSubtotalInclTax(0);
        $total->setBaseSubtotalInclTax(0);
    }
    /**
     * @param \Magento\Quote\Model\Quote $quote
     * @param Address\Total $total
     * @return array|null
     */
    /**
     * Assign subtotal amount and label to address object
     *
     * @param \Magento\Quote\Model\Quote $quote
     * @param Address\Total $total
     * @return array
     */
    public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
    {
        $fee = ($quote->getShippingAddress()->getShippingAmount()+15);
        return [
            'code' => 'customfee',
            'title' => 'Custom Fee',
            'value' => $fee
        ];
    }

    /**
     * Get Subtotal label
     *
     * @return \Magento\Framework\Phrase
     */
    public function getLabel()
    {
        return __('Custom Fee');
    }
}
1

There are 1 best solutions below

2
Can Özdemir On

To can recalculate the fee value on the front-end side asynchronously you have to implement some changes on checkout as described here: https://magento.stackexchange.com/a/93349 also, you can check this module: https://github.com/sivajik34/Custom-Fee-Magento2