Extending default cart calculators in Apex
Key Takeaway
Extend the base class, call super.calculate(request), change the in-memory cart, then register the class and link it to a store that has the Cart Calculate API enabled.
The Cart Calculate API runs a chain of Apex calculators for pricing, promotions, inventory, shipping and taxes whenever a buyer changes the cart or moves through checkout. You extend a default calculator by writing an Apex class that extends that calculator's base class in the CartExtension namespace, then registering it for the matching extension point. Your class can keep the standard calculation and add its own work around it.
Three levels of control
The Cart Calculate API has three levels of control:
- Orchestrators decide which calculators run and when.
- Calculators do the cart math, such as shipping and tax.
- Services do specific calculations that can reach beyond the cart. For example, the pricing service also affects the store's product detail pages.