on 2024 Aug 29 12:18 PM
Here's what they want to do: “Writing a custom action for promotions that gives a discount to the most expensive or cheapest product in the basket according to the selection of the business units, again in the amount selected by the business units.”
The code for this task is as follows.
private final String SELECT_OPERATOR = "selectOperator";
@Override
public void performAction(RuleActionContext context) {
performActionInternal(context);
}
@Override
protected boolean performActionInternal(RuleActionContext context) {
CartRAO cart = context.getValue(CartRAO.class);
Set<OrderEntryRAO> orderEntries = cart.getEntries();
if(CollectionUtils.isEmpty(orderEntries)) return false;
Task9Operator discountType = (Task9Operator) context.getParameter(SELECT_OPERATOR);
BigDecimal discountAmount = (BigDecimal) context.getParameter("discountAmount");
if (discountType.equals(Task9Operator.LOWEST)){
applyLowestPriceDiscount(cart, orderEntries, discountAmount, context);
} else if (discountType.equals(Task9Operator.HIGHEST)){
applyHighestPriceDiscount(cart, orderEntries, discountAmount, context);
}
return true;
}
private void applyLowestPriceDiscount(CartRAO cart, Set<OrderEntryRAO> orderEntries, BigDecimal discountValue, RuleActionContext context){
orderEntries.stream().min(Comparator.comparing(OrderEntryRAO::getTotalPrice)).ifPresent(orderEntryRAO -> applyDiscountToEntry(cart, orderEntryRAO, discountValue, context));
}
private void applyHighestPriceDiscount(CartRAO cart, Set<OrderEntryRAO> orderEntries, BigDecimal discountValue, RuleActionContext context){
orderEntries.stream().max(Comparator.comparing(OrderEntryRAO::getTotalPrice)).ifPresent(orderEntryRAO -> applyDiscountToEntry(cart, orderEntryRAO, discountValue, context));
}
private void applyDiscountToEntry(CartRAO cart, OrderEntryRAO orderEntryRAO, BigDecimal discountValue, RuleActionContext context) {
DiscountRAO discount = getRuleEngineCalculationService().addOrderEntryLevelDiscount(orderEntryRAO, true, discountValue);
addDiscount(context, orderEntryRAO, orderEntryRAO.getQuantity(), discount);
}
protected void addDiscount(RuleActionContext context, OrderEntryRAO orderEntryRao, int quantity, DiscountRAO discount) {
RuleEngineResultRAO result = context.getRuleEngineResultRao();
result.getActions().add(discount);
this.setRAOMetaData(context, discount);
context.scheduleForUpdate(orderEntryRao, orderEntryRao.getOrder(), result);
context.insertFacts(discount);
context.insertFacts(discount.getConsumedEntries());
}
But this code does not work exactly as intended. It makes a discount as a certain multiple of the entered discount amount (I still haven't figured out where this multiple rate comes from). According to my examination as a result of debug, the context structure containing the same information more than once comes with different references. Therefore, a discount with a new reference is created for each context reference and then added to the orderEntry as an action. Therefore, there is a discount more than desired.
Since I am a newcomer in Commerce Cloud, I have tried everything I know and I could not solve it. I wonder if there is someone among you who can help?
Request clarification before answering.
User | Count |
---|---|
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.