0

how can I change the cart summary custom field value at the checkout cart page?

I got a reference from this Link

Test/Module/view/frontend/web/template/checkout/cart/totals/fee.html

<!-- ko if: isDisplayed() -->
<!-- ko if: isTaxEnabled() -->
<!-- ko if: isDisplayBoth() -->
<tr class="totals fee excl" >
    <th class="mark" colspan="1" scope="row" data-bind="text: getFeeLabel() + ' ' + getExFeeLabel()"></th>
    <td class="amount">
        <span class="price" data-bind="text: getFormattedPrice()"></span>
    </td>
</tr>
<tr class="totals fee incl">
    <th class="mark" colspan="1" scope="row" data-bind="text: getFeeLabel() + ' ' + getInFeeLabel()"></th>
    <td class="amount">
        <span class="price" data-bind="text: getInFormattedPrice()"></span>
    </td>
</tr>
<!-- /ko -->
<!-- ko ifnot: isDisplayBoth() -->
<!-- ko if: displayExclTax() -->
<tr class="totals fee excl" >
    <th class="mark" colspan="1" scope="row" data-bind="text: getFeeLabel()"></th>
    <td class="amount">
        <span class="price" data-bind="text: getFormattedPrice()"></span>
    </td>
</tr>
<!-- /ko -->
<!-- ko if: displayInclTax() -->
<tr class="totals fee incl">
    <th class="mark" colspan="1" scope="row" data-bind="text: getFeeLabel()"></th>
    <td class="amount">
        <span class="price" data-bind="text: getInFormattedPrice()"></span>
    </td>
</tr>
<!-- /ko -->
<!-- /ko -->
<!-- /ko -->
<!-- ko ifnot: isTaxEnabled() -->
<tr class="totals fee excl" >
    <th class="mark" colspan="1" scope="row" data-bind="text: getFeeLabel()"></th>
    <td class="amount">
        <span class="price" data-bind="text: getFormattedPrice()"></span>
    </td>
</tr>   
<!-- /ko -->
<!-- /ko -->

Test/Module/Model/Quote/Total/Fee.php

<?php
namespace Test\Module\Model\Quote\Total;

use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Address\Total;

class Fee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
    protected $_productCollectionFactory;
    protected $helperData;
    protected $_priceCurrency;
    protected $taxHelper;
    private   $taxCalculator;

    /**
     * Collect grand total address amount
     *
     * @param \Magento\Quote\Model\Quote $quote
     * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
     * @param \Magento\Quote\Model\Quote\Address\Total $total
     * @return $this
     */
    protected $quoteValidator = null;

    public function __construct(
        \Magento\Quote\Model\QuoteValidator $quoteValidator,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        \Ncode\GSTtax\Helper\Data $helperData,
        \Ncode\GSTtax\Helper\Tax $helperTax,
        \Magento\Tax\Model\Calculation $taxCalculator,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
    )
    {
        $this->quoteValidator            = $quoteValidator;
        $this->_priceCurrency            = $priceCurrency;
        $this->helperData                = $helperData;
        $this->taxHelper                 = $helperTax;
        $this->taxCalculator             = $taxCalculator;
        $this->_productCollectionFactory = $productCollectionFactory;
    }

    public function collect(
        \Magento\Quote\Model\Quote $quote,
        \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $total
    )
    {
        parent::collect($quote, $shippingAssignment, $total);
        if (!count($shippingAssignment->getItems())) {
            return $this;
        }

        $enabled            = $this->helperData->isModuleEnabled();
        $GstIn              = $this->helperData->getGstIn();        
        $minimumOrderAmount = $this->helperData->getMinimumOrderAmount();
        $GstRegion          = $this->helperData->getGstRegion();
            
        if($Cartinfo        = $this->helperData->getCart()->getQuote()){
            $items          = $Cartinfo->getAllItems();
            foreach($items as $item) {
               $product = $this->helperData->getLoadProduct($item->getProductId());       
               $hsnCode = $product->getHsnCode(); 
               //echo $hsnCode;               
            }            
        }        

        $subtotal = $total->getTotalAmount('subtotal'); 
            if($hsnCode){
                if ($enabled && $GstIn && $minimumOrderAmount <= $subtotal){        
                $fee = $this->helperData->getGSTtax(); //Get GST Rate from Backend
                if(is_numeric($fee)){            
                    $fee = $subtotal * $fee / 100;                   
                }
                
                $total->setTotalAmount('fee', $fee);
                $total->setBaseTotalAmount('fee', $fee);
                $total->setFee($fee);
                $quote->setFee($fee);            

                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
                $version = (float)$productMetadata->getVersion();

                if($version > 2.1)
                {
                    //$total->setGrandTotal($total->getGrandTotal() + $fee);
                }
                else
                {
                    $total->setGrandTotal($total->getGrandTotal() + $fee);
                }

                if ($this->taxHelper->isTaxEnabled()) {
                    $address = $this->_getAddressFromQuote($quote);
                    $this->_calculateTax($address, $total);

                    $extraTaxables = $address->getAssociatedTaxables();
                    $extraTaxables[] = [
                        'code' => 'fee',
                        'type' => 'fee',
                        'quantity' => 1,
                        'tax_class_id' => $this->taxHelper->getTaxClassId(),
                        'unit_price' => $fee,
                        'base_unit_price' => $fee,
                        'price_includes_tax' => false,
                        'associated_item_code' => false
                    ];

                    $address->setAssociatedTaxables($extraTaxables);
                }

            }
        }else{

        }
        return $this;
    }

    /**
     * @param \Magento\Quote\Model\Quote $quote
     * @param \Magento\Quote\Model\Quote\Address\Total $total
     * @return array
     */
    public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
    {

        $enabled = $this->helperData->isModuleEnabled();
        $minimumOrderAmount = $this->helperData->getMinimumOrderAmount();
        $subtotal = $quote->getSubtotal();
        $fee = $quote->getFee();
        $address = $this->_getAddressFromQuote($quote);

        $result = [];
        if ($enabled && ($minimumOrderAmount <= $subtotal) && $fee) {            
            //echo $fee; die();
            $result = [
                'code' => 'fee',
                'title' => $this->helperData->getFeeLabel(),
                'value' => $fee
            ];

            if ($this->taxHelper->isTaxEnabled() && $this->taxHelper->displayInclTax()) {
                 $result [] = [
                    'code' => 'fee',
                    'value' => $fee + $address->getFeeTax(),
                    'title' => __($this->helperData->getFeeLabel()),
                ];
            }
            if ($this->taxHelper->isTaxEnabled() && $this->taxHelper->displayBothTax()) {
                $result [] = [
                    'code' => 'fee',
                    'value' => $fee + $address->getFeeTax(),
                    'title' => __($this->helperData->getFeeLabel()),
                ];
            }
        }

        return $result;
    }

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

    /**
     * @param \Magento\Quote\Model\Quote\Address\Total $total
     */
    protected function clearValues(\Magento\Quote\Model\Quote\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);

    }
    protected function _getAddressFromQuote(Quote $quote)
    {
        return $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
    }

    protected function _calculateTax(Address $address, Total $total)
    {
        $taxClassId = $this->taxHelper->getTaxClassId();
        if (!$taxClassId) {
            return $this;
        }

        $taxRateRequest = $this->_getAddressTaxRequest($address);
        $taxRateRequest->setProductClassId($taxClassId);

        $rate = $this->taxCalculator->getRate($taxRateRequest);



        $baseTax = $this->taxCalculator->calcTaxAmount(
            $total->getBaseTotalAmount('fee'),
            $rate,
            false,
            true
        );
        $tax = $this->taxCalculator->calcTaxAmount(
            $total->getTotalAmount('fee'),
            $rate,
            false,
            true
        );



        //$total->setBaseMcPaymentfeeTaxAmount($baseTax);
        $total->setFeeTax($tax);

        $appliedRates = $this->taxCalculator->getAppliedRates($taxRateRequest);
        $this->_saveAppliedTaxes($address, $appliedRates, $tax, $baseTax, $rate);

        $total->addBaseTotalAmount('tax', $baseTax);
        $total->addTotalAmount('tax', $tax);

        return $this;
    }

    protected function _getAddressTaxRequest($address)
    {
        $addressTaxRequest = $this->taxCalculator->getRateRequest(
            $address,
            $address->getQuote()->getBillingAddress(),
            $address->getQuote()->getCustomerTaxClassId(),
            $address->getQuote()->getStore()
        );
        return $addressTaxRequest;
    }

    protected function _saveAppliedTaxes(
        Address $address,
        $applied,
        $amount,
        $baseAmount,
        $rate
    ) {
        $previouslyAppliedTaxes = $address->getAppliedTaxes();
        $process = 0;
        if(is_array($previouslyAppliedTaxes)) {
            $process = count($previouslyAppliedTaxes);
        }
        foreach ($applied as $row) {
            if ($row['percent'] == 0) {
                continue;
            }
            if (!isset($previouslyAppliedTaxes[$row['id']])) {
                $row['process'] = $process;
                $row['amount'] = 0;
                $row['base_amount'] = 0;
                $previouslyAppliedTaxes[$row['id']] = $row;
            }

            if ($row['percent'] !== null) {
                $row['percent'] = $row['percent'] ? $row['percent'] : 1;
                $rate = $rate ? $rate : 1;

                $appliedAmount = $amount / $rate * $row['percent'];
                $baseAppliedAmount = $baseAmount / $rate * $row['percent'];
            } else {
                $appliedAmount = 0;
                $baseAppliedAmount = 0;
                foreach ($row['rates'] as $rate) {
                    $appliedAmount += $rate['amount'];
                    $baseAppliedAmount += $rate['base_amount'];
                }
            }

            if ($appliedAmount || $previouslyAppliedTaxes[$row['id']]['amount']) {
                $previouslyAppliedTaxes[$row['id']]['amount'] += $appliedAmount;
                $previouslyAppliedTaxes[$row['id']]['base_amount'] += $baseAppliedAmount;
            } else {
                unset($previouslyAppliedTaxes[$row['id']]);
            }
        }
        $address->setAppliedTaxes($previouslyAppliedTaxes);
    }
}

Test/Module/view/frontend/web/template/checkout/summary/fee.html

<!-- ko -->

  <tr class="totals fee excl">
        <th class="mark" scope="row">
            <span class="label" data-bind="text: title"></span>
            <span class="value" data-bind="text: getValue()"></span>
        </th>
        <td class="amount">

            <span class="price"
                  data-bind="text: getValue(), attr: {'data-th': title}"></span>


        </td>
    </tr>   

<!-- /ko -->

enter image description here

1
  • what is the flow of get this value? how can i debug that exactly from where this value comes?
    – rocky9310
    Commented Dec 22, 2021 at 9:01

1 Answer 1

0

the value are come from return fee variable in fetch function

here :

            $result = [
                'code' => 'fee',
                'title' => $this->helperData->getFeeLabel(),
                'value' => $fee // here the variable
            ];

and you may debug that variable with the logger

is that what you looking for?

2
  • yes value is coming from here you are right
    – rocky9310
    Commented Dec 22, 2021 at 9:50
  • in js also this value are comes?
    – rocky9310
    Commented Dec 22, 2021 at 10:23

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.