SAP Hybris suite , Hybris tutorial, Java, J2ee Stuff Online-

Core concept of calculate() and recalculate() of calculationService in Hybris | explain calculate and recalculate in Hybris

Open 2 Answers 1259 Views

Hello Hybris Developers ,

In my every Hybris interview or during customizing order and cart there is used calculation which is very important for customizing as well as in interview . SO today i decided to write my outcomes realted to calculation

As you know both Cart and Order is subtype of AbstractOrder so more or less every attributed is same for both item

FYI - Finally Cart in converted to Order means Copy all data from Cart to Order

Both Carts and Orders have an attribute called calculated of type boolean that states whether they are calculated, or not . Now next what will calculate ?

In every cart or an order have price , tax , discount etc so this need to calculate

So on basis of boolean attribute calculated there is call of CalulationService's method calulate and recalculate

So Whenever a price total related action is performed on a Cart or an Order, the Cart or Order is marked to be FALSE

1. Product is be added to the cart

2. Product is be removed from the cart

3. User is changed

4. Currency is changed


So two importatnt method call on basis of calculated=FALSE  which is CalculationService.calculate() or CalculationService.recalculate() method

Before going in deep lets see below


<itemtype code="Cart"





            <deployment table="Carts" typecode="43"/>


                <attribute autocreate="true" redeclare="true" qualifier="entries" type="CartEntryCollection"/>

                <attribute type="java.lang.String" qualifier="sessionId">

                    <persistence type="property"/>

                    <modifiers read="true" write="true"/>







<itemtype code="Order"





            <deployment table="Orders" typecode="45" propertytable="OrderProps"/>


                <attribute autocreate="true" redeclare="true" qualifier="entries" type="OrderEntryCollection"/>




Form above item definition every cart has cartEntry and every order has OrderEntry 

FYI- Every cartEntry and orderEntry is also has boolean attribute calculated

So below point you should keep in your mind

So for every cartEntry's boolean attribute value it call CalculationService.calculate() method to calculate there price, tax, discount etc

Concept: If there is something change in cartEntry then the calculated atribute become FALSE and then there will also cart's calculated attribute become set to FALSE means

Final Conclusion

1. If you adding ant product into cart then

   CartEntry's calculated attribute = FALSE

   Cart's calculated attribute = FALSE

  So there will call CalculationService.calculate after finish this call CartEntry's calculated attribute will set to TRUE then after will call CalculationService.recalculate() and cart' calculated attribute will set to TRUE


2. If again any product is add to same cart then

   CartEntry's calculated attribute = FALSE. If it is FALSE then

   Cart's calculated attribute = FALSE

   So there will call CalculationService.calculate but these it calculate for these cartEntry only because previous entered cartEntry has allready calculated and finally again it set to= TRUE after calculate for these newly enterted cartEntry and after again call CalculationService.recalculate()


If you have any query then please write as comments

2 Answers

answered Mar 20 by anonymous

As per Information available on Hybris Wiki,

  • CalculationService.calculate(): Calculates only those Cart/Order elements that are marked to be not calculated (calculated=false)
  • CalculationService.recalculate(): Recalculates the entire Cart/Order regardless of the value of the calculated attribute

A calculated attribute exists for the individual entries of Cart and Order and also indicates whether the cart entry or order entry is calculated, or not. This allows the CalculateService.calculate() method to calculate only those entries which are marked as not calculated and does not require calculating all entries.

When one of the entries of Cart or Order has its calculated attribute set to false (for example, because the number of products referenced by the entry has changed), then Cart/Order is also set to be not calculated.

answered Aug 21 by Avneesh Saini
edited Aug 21