Spryker uses the Calculation module to calculate the cart totals that are displayed in the cart/checkout or when the order is placed. Its also used to recalculate order totals after refund.
The calculation module extensively uses plugins to inject calculation algorithms.
How Calculation Works
The quote transfer object is used to store data and plugins that calculate the amounts.
There is already a list of plugins which populate quote transfer with corresponding data. Calculations are executed every time the content of the cart is updated.
For more details see Cart Data Flow.
If manual cart recalculation is required, CalculationFacade::recalculate can be called from Zed or CalculationClient::recalculate from Yves with prepared Quote Transfer . When the recalculation operation is called, the calculator runs the calculator plugin stack and each plugin modifies the QuoteTransfer (calculates discounts, adds sum gross prices, calculates taxes, etc.). Most plugins require the unitPrice and the quantity to be provided.
Each amount is calculated and stored in cents.
Its important to understand how the Calculator works internally with the CalculableObjectTransfer generic data structure. This data structure allows us to work with different object types e.g. OrderTransfer and QuoteTransfer.
Therefor, before passing a Quote object to the calculators, the object is then converted to a calculable object.
After calculation, the original object that was passed is converted back to the object's recalculated state.
Each calculable object holds reference to the CalculableObjectTransfer::originalQuote and CalculableObjectTransfer::originalOrder objects, in order for you to use this for more context when working with a custom calculator.
Do not modify CalculableObjectTransfer::originalQuote and CalculableObjectTransfer::originalOrder as changes will not be propagated.
Download our Calculation Spreadsheet to see how calculations are made.
You can switch price mode calculators using by setting QuoteTransfer::priceMode property, this will change tax calculation and values for unitPrice and sumPrice
will be read from unitNetPrice and sumNetPrice respectively. This value is set in cart Expander CartItemPricePlugin. Price mode is also persisted when order is placed.