0

I need to calculate the SubTotal for each tax rate in my cart summary.. How can I do this? I found calculation start in module_tax under vendor / magento folder.. but I did not found where to put my hand to change the way subtotals are calculated. I tried copying the folders magento_tax module / design / frontend / web / js in my child theme, then I modified some functions related to knockout framework.. but I cannot find a way to extract Tax Subtotals for each Tax Rate.. Any help will be appreciated.
I'm using Magento 2.3.5.

Follow an example of what I need:

In my Cart I have 3 products with 2 tax rates:
Product 1
Product 2
VAT 22%

Product 3
MyTax 10%

In my cart summary I need to show something like this:

TAX SUBTOTALS

tax: VAT 22% | subtotal excl tax: 88€ (subtot excl tax of both product 1 and product 2)
tax: MyTax 10% | subtotal excl tax: 90€ (subtot excl tax for product 3)

         GrandTotal Excl. TAX    178€ (Totals excluding taxes)
         GrandTotal Incl. TAX    200€ (Total including taxes)
1

Hay yesss, I found a solution so I'm answering to myself, maybe this will be usefull to someone else with the same needing. N.B. Sorry if the code is not too much pretty, since I'm not a developer... any suggestion or contribution in this way will be appreciated, I'll take care of keeping this post updated.

The files edited are: vendor/magento/module_tax/view/frontend/web/js/view/checkout/cart/totals/tax.js vendor/magento/module_tax/view/frontend/web/template/checkout/cart/totals/tax.html

The above files are copied in my child theme under "//Magento_tax/web/" folder, and then modified:

  • in tax.js added this function at the bottom

     /** 
      * return subtotalByRate
      */
     getSubTaxTotal: function (parent, percentage) {
         // get current rate
         var currentRate = percentage;
         // declare subtotalByRate var
         var subtotalByRate = 0;
         // target items in chart measuring array
         var itemsSegment = this.totals()["items"];
         // get qty of items in chart
         var ItemsQty = Object.keys(itemsSegment).length;
         // declare rates var
         var rates = 0;
         rates = parent.rates;
         _.each(rates, function (rate) {
             // extract total for tax rate function
             // start loop for chart items
             for(var i=0; i < ItemsQty ; i++) {
                 // check if the item have the same taxRate,  then add rowPrice tax escluded
                 if (itemsSegment[i].tax_percent == currentRate) {
                     subtotalByRate = subtotalByRate + itemsSegment[i].base_row_total;
                 }
             }
         });
         // the loop is twice (don't know why..) so divide by 2
         subtotalByRate = subtotalByRate/2;
         return this.getFormattedPrice(subtotalByRate);
     }
    
  • in tax.html added this HTML right after "ko forEach: rates" statement

      <tr class="tax-subtotal-summary">
          <th class="mark" scope="row" colspan="1" data-bind="text: 'Subtotale soggetto a ' + title + ' (' + percent + '%)'"></th>
          <td class="amount" rowspan="1">
          <span class="price"
                    data-bind="text: $parents[1].getSubTaxTotal($parents[0], percent), attr: {'data-th': title, 'rowspan': $parents[0].rates.length }"></span>
         </td>
    

This is the result on my cart

mycart

1
  • I am looking to do the same thing. I need to write PHP to get the subtotal of two sets of tax rules before tax. Did you find any more guidance for this? – Chris Oliver Mar 18 at 16:03

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.