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-&gt;quoteValidator            = $quoteValidator;
    $this-&gt;_priceCurrency            = $priceCurrency;
    $this-&gt;helperData                = $helperData;
    $this-&gt;taxHelper                 = $helperTax;
    $this-&gt;taxCalculator             = $taxCalculator;
    $this-&gt;_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-&gt;getItems())) {
        return $this;
    }

    $enabled            = $this-&gt;helperData-&gt;isModuleEnabled();
    $GstIn              = $this-&gt;helperData-&gt;getGstIn();        
    $minimumOrderAmount = $this-&gt;helperData-&gt;getMinimumOrderAmount();
    $GstRegion          = $this-&gt;helperData-&gt;getGstRegion();

    if($Cartinfo        = $this-&gt;helperData-&gt;getCart()-&gt;getQuote()){
        $items          = $Cartinfo-&gt;getAllItems();
        foreach($items as $item) {
           $product = $this-&gt;helperData-&gt;getLoadProduct($item-&gt;getProductId());       
           $hsnCode = $product-&gt;getHsnCode(); 
           //echo $hsnCode;               
        }            
    }        

    $subtotal = $total-&gt;getTotalAmount('subtotal'); 
        if($hsnCode){
            if ($enabled &amp;&amp; $GstIn &amp;&amp; $minimumOrderAmount &lt;= $subtotal){        
            $fee = $this-&gt;helperData-&gt;getGSTtax(); //Get GST Rate from Backend
            if(is_numeric($fee)){            
                $fee = $subtotal * $fee / 100;                   
            }

            $total-&gt;setTotalAmount('fee', $fee);
            $total-&gt;setBaseTotalAmount('fee', $fee);
            $total-&gt;setFee($fee);
            $quote-&gt;setFee($fee);            

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

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

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

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

                $address-&gt;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-&gt;helperData-&gt;isModuleEnabled();
    $minimumOrderAmount = $this-&gt;helperData-&gt;getMinimumOrderAmount();
    $subtotal = $quote-&gt;getSubtotal();
    $fee = $quote-&gt;getFee();
    $address = $this-&gt;_getAddressFromQuote($quote);

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

        if ($this-&gt;taxHelper-&gt;isTaxEnabled() &amp;&amp; $this-&gt;taxHelper-&gt;displayInclTax()) {
             $result [] = [
                'code' =&gt; 'fee',
                'value' =&gt; $fee + $address-&gt;getFeeTax(),
                'title' =&gt; __($this-&gt;helperData-&gt;getFeeLabel()),
            ];
        }
        if ($this-&gt;taxHelper-&gt;isTaxEnabled() &amp;&amp; $this-&gt;taxHelper-&gt;displayBothTax()) {
            $result [] = [
                'code' =&gt; 'fee',
                'value' =&gt; $fee + $address-&gt;getFeeTax(),
                'title' =&gt; __($this-&gt;helperData-&gt;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-&gt;setTotalAmount('subtotal', 0);
    $total-&gt;setBaseTotalAmount('subtotal', 0);
    $total-&gt;setTotalAmount('tax', 0);
    $total-&gt;setBaseTotalAmount('tax', 0);
    $total-&gt;setTotalAmount('discount_tax_compensation', 0);
    $total-&gt;setBaseTotalAmount('discount_tax_compensation', 0);
    $total-&gt;setTotalAmount('shipping_discount_tax_compensation', 0);
    $total-&gt;setBaseTotalAmount('shipping_discount_tax_compensation', 0);
    $total-&gt;setSubtotalInclTax(0);
    $total-&gt;setBaseSubtotalInclTax(0);

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

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

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

    $rate = $this-&gt;taxCalculator-&gt;getRate($taxRateRequest);



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



    //$total-&gt;setBaseMcPaymentfeeTaxAmount($baseTax);
    $total-&gt;setFeeTax($tax);

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

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

    return $this;
}

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

protected function _saveAppliedTaxes(
    Address $address,
    $applied,
    $amount,
    $baseAmount,
    $rate
) {
    $previouslyAppliedTaxes = $address-&gt;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-&gt;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">

        &lt;span class=&quot;price&quot;
              data-bind=&quot;text: getValue(), attr: {'data-th': title}&quot;&gt;&lt;/span&gt;


    &lt;/td&gt;
&lt;/tr&gt;   

<!-- /ko -->

enter image description here

rocky9310
  • 115
  • 1
  • 12

1 Answers1

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?

EshaRamady
  • 43
  • 1
  • 8