Skip to content

Instantly share code, notes, and snippets.

@astorm
Created May 2, 2012 01:03
Show Gist options
  • Save astorm/2572772 to your computer and use it in GitHub Desktop.
Save astorm/2572772 to your computer and use it in GitHub Desktop.
Magento's Tax Calculation Based On setting

Info here is based on a Magento CE 1.6.1 install. Your version may vary. Also, there's lots of conjecture on my part near the end, so DANGER WILL ROBINSON.

Configuration setting is located at

System -> Configuration -> Tax -> Calculation Settings

Label is "Tax Calculation Method Based On"

Possible Settings are

  • Total
  • Row Total
  • Unit Price

Magento accesses this value through the getAlgorithm method on the Mage_Tax Config model

app/code/core/Mage/Tax/Model/Config.php

The same module also has a getCalculationAgorithm method on its data helper class

app/code/core/Mage/Tax/Helper/Data.php

But there's no code that references this method. This, combined with the misspelling in it's name makes me smell bit rot.

This value is referenced in two tax module models

app/code/core/Mage/Tax/Model/Sales/Total/Quote/Tax.php
app/code/core/Mage/Tax/Model/Sales/Total/Quote/Subtotal.php

In both cases this value is used in a switch to run different methods during tax calculation

#File: app/code/core/Mage/Tax/Model/Sales/Total/Quote/Tax.php
    switch ($this->_config->getAlgorithm($this->_store)) {
        case Mage_Tax_Model_Calculation::CALC_UNIT_BASE:
            $this->_unitBaseCalculation($address, $request);
            break;
        case Mage_Tax_Model_Calculation::CALC_ROW_BASE:
            $this->_rowBaseCalculation($address, $request);
            break;
        case Mage_Tax_Model_Calculation::CALC_TOTAL_BASE:
            $this->_totalBaseCalculation($address, $request);
            break;
        default:
            break;
    }


#File: app/code/core/Mage/Tax/Model/Sales/Total/Quote/Subtotal.php
    switch ($this->_config->getAlgorithm($this->_store)) {
        case Mage_Tax_Model_Calculation::CALC_UNIT_BASE:
            $this->_unitBaseCalculation($address, $request);
            break;
        case Mage_Tax_Model_Calculation::CALC_ROW_BASE:
            $this->_rowBaseCalculation($address, $request);
            break;
        case Mage_Tax_Model_Calculation::CALC_TOTAL_BASE:
            $this->_totalBaseCalculation($address, $request);
            break;
        default:
            break;
    }

Based on this, my general guess as to the functionality would be

  1. Unit based calculation goes through each individual item purchased, calculates a tax rate, adds everything together to get the tax rate

  2. Row based calculation goes through each individual line item, calculates a tax for the line item total, add everything together and applies the tax. This differs from #1 in that a line item with a quantity of "7" is calculated seven times in #1, but only once here in #2. That is, the rate is applied to the total after qty * price

  3. Total based calculation applies the tax calculation to the already calculated totals, ignoring individual items

This feature was probably motivated by individual local rules (or client requests) as to how taxes should be calculated, as fractional amounts could add up differently depending on when adding and rounding occurs. THat said, I haven't run through the code to see if my guesses are right so YMMV. Other areas to consider for future research.

  • How does each mode play with shopping cart price rules and store wide discounts

  • Are both of the above classes actually used? If so it seems like the pricing logic is in two places, which could lead to bugs if one is updated and the other is not.

@nadeeek
Copy link

nadeeek commented Jun 21, 2013

great tutorial

@Xebi4i
Copy link

Xebi4i commented Mar 30, 2016

Thanks a lot.
I have decided for myself to use since in this case I didn't face with a .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment