Excluding Certain Products from Total Cart Value Calculation (PromoEngine)


In one of our projects we have a requirement to exclude certain products from the total cart value calculation. So, for example, we would have a custom condition which would trigger a free gift action if the order total > 200 without including product A and product B on the order. In other words, the dollar values of the cart entries which contain product A and B need to be excluded in the cart total calculation.

It appears that this dynamic condition cannot be supported with the promotion engine since when the СartRao is being populated the promotion condition parameters are not known.

The problem is accessing the products at runtime, because in the default implementation all the condition constraints are compiled into static drools rules. It is not possible/very difficult to extract the condition values like a list of the products to be excluded from the calcultation. When the cartRao is being created we don’t even know which promotion could potentially trigger so that we could get the condition parameters from it.


My solution is using two promotions, one is for calculating “a new cart total” if all certain products are present in the cart.  The calculated value is stored in CartRAO and can be used in the next promotions in the same promo calculation session. The second promotion depends on the first one, so it will work only if the certain products are found and “a new cart total” is calculated. Then you can combine them together using stackability so only one promotion fires at a time.



Technical details


public class CalcTotalPriceAction implements RuleExecutableAction {
    private CalculatedTotalsRao calculatedTotalsRao;    
    private ProductService productService;
    private PriceService priceService;
    public void executeAction(RuleActionContext ruleActionContext, Map<String, Object> map) throws RuleEvaluationException {
String orderEntryRAOCode = "de.hybris.platform.ruleengineservices.rao.OrderEntryRAO";
        String cartEntryRAOCode = "de.hybris.platform.ruleengineservices.rao.CartRAO";;
        long totalPrice=calculateTotalPrice( (Set<OrderEntryRAO>) ((DefaultDroolsRuleActionContext) ruleActionContext).getVariables().get(orderEntryRAOCode));
        Set <CartRAO> carts = (Set<CartRAO>) ((DefaultDroolsRuleActionContext) ruleActionContext).getVariables().get(cartEntryRAOCode);
        CartRAO cart = carts.iterator().next();
// I use cart.setTotal in PoC, but I recommend to have a designated attribute for the recalculated cart totals
// However, you will need to extend the cart totals condition to use the recalculated value
// For the demonstration purposes I used an OOTB attribute, "Total"
cart.setTotal(BigDecimal.valueOf(cart.getTotal().longValue() - BigDecimal.valueOf(totalPrice).longValue()));

//for the demonstration purposes I use "long" for the price values
private long calculateTotalPrice(Set<OrderEntryRAO> entries) {
        long totalPrice = 0;
        for (OrderEntryRAO orderEntryRao : entries) {
            ProductModel productModel = productService.getProductForCode(orderEntryRao.getProduct().getCode());
            List<PriceInformation> prices = priceService.getPriceInformationsForProduct(productModel);
            double productPrice = prices.get(0).getPriceValue().getValue();
            //for the demonstration purposes I use "long" for the price values
            totalPrice = totalPrice + Math.round(orderEntryRao.getQuantity() * productPrice) ;
        return totalPrice;



  1. Suhaim · · Reply

    Hi Rauf , i too was trying to implement the similar feature. The subpromotion or the promotion that checks for the qualifying products do work along with the recalculate totals but the updated totals is not passed over to the main promotion.
    For the action i have used ( XXXXaction extends AbstractRuleExecutableSupport implements RAOAction )
    Can you please let me know what values did you set for stackabilty for the main and sub promotion.
    Did you enable or disable the flag when you invoke the sub promotion from the main promotion (Action rule execution allowed)?
    Thank you for sharing the approach


    1. I am not sure that I understood you correctly. Please contact me on Skype, I will ask a couple of simple questions. Booth promotions should be marked as stackable, because they must be in the same promotion group


    2. My Skype is rauf_aliev


  2. vijay DESU · · Reply

    Is it not painful for business users to setup promotions in that manner via Rule Builder? we have similar requirement with our current client and they have around 5 sites on same hybris instance with different catalogs. To setup a single promotion rule for the 5 sites would be 5 promotion rules and we need to create additional 5 cartTotal promotion rules. And in same way we have to setup potential promotion rules for promo messaging (which is weird). So, total would be around 15-20 promotion rules.

    Any other approaches that is feasible both technically & business wise.

    I know this is not a better solution, why cannot we do the similar thing in custom actions and i agree it requires customization in most of actions to accommodate the same.

    Liked by 1 person

  3. sridhar · · Reply

    Hi I want to know the promotion engine flow in 6.5 , means when how the condition triggers and how conditions and actions communicate ,please give us full details from when we are adding a product to the cart to promotion calculation completes .


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: