How to create custom promotion?
Promotion is an effective promotion tool used by companies to promote themselves and market their products. It comes from the word promotion in French. Encouragement in Turkish is encouragement. While products such as watches, agendas, key rings and pens are generally used, personal products can also be offered to customers as promotions in line with their sales targets.
In this context, there are many ways to create promotions on Hybris. Order discount, gift product, coupon identification etc. If you do not know how to use the promotions, you can get some information from this link.
When creating a promotion, we sometimes ask that it take different actions as a result of certain conditions. We will see how we can accomplish this. First, I share the list below to see what we will do step by step.
Follow List for Create a Special Promotion
- We determine the condition of the promotion. (existing conditions may be useless.)
- We determine the action we want to perform as a result of the condition. (existing actions may not be useless)
- We are creating a new class with a contingent name that we will create.
- We implement the RuleConditionTranslator interface class to the created class.
- We define the parameter to be used in the condition as fixed.
- We fill the translate method according to the desired condition.
- We create a new class suitable for the action that will occur as a result of the condition.
- We implement the RuleExecutableAction interface class into the created class.
- We fill the executeAction method according to the desired action.
- Writing impex file.
Let’s create one problem about promotion
The promotion is activated when there is “happy birthday” text in the product description. When the promotion is activated, add a 50 percent discount to the order. So I want create so special condition and action.
We create Condition class
public class RuleQualifyingSearchedWordConditionTranslator implements RuleConditionTranslator {
private final String SEARCHED_WORD = "searchedWord";
@Override
public RuleIrCondition translate(RuleCompilerContext context, RuleConditionData condition, RuleConditionDefinitionData conditionDefinition) throws RuleCompilerException {
try {
RuleParameterData searchedWord = condition.getParameters().get(SEARCHED_WORD);
if (searchedWord != null) {
String word = searchedWord.getValue();
final String cartRAOVariable = context.generateVariable(CartRAO.class);
final RuleIrAttributeCondition ruleIrAttributeCondition = new RuleIrAttributeCondition();
ruleIrAttributeCondition.setVariable(cartRAOVariable);
ruleIrAttributeCondition.setOperator(RuleIrAttributeOperator.CONTAINS);
ruleIrAttributeCondition.setAttribute(SEARCHED_WORD);
ruleIrAttributeCondition.setValue(word);
final RuleIrGroupCondition ruleIrGroupCondition = new RuleIrGroupCondition();
ruleIrGroupCondition.setOperator(RuleIrGroupOperator.AND);
ruleIrGroupCondition.setChildren(new ArrayList<>());
ruleIrGroupCondition.getChildren().add(ruleIrAttributeCondition);
return ruleIrGroupCondition;
}
} catch (Exception ex) {
LOG.error(ex);
throw ex;
}
return new RuleIrFalseCondition();
}
}
We create Action class
public class RuleSearchedWordAddScoreAction implements RuleExecutableAction {
@Override
public void executeAction(final RuleActionContext context, final Map<String, Object> parameters) throws RuleEvaluationException {
final String value = (String) parameters.get("value");
final RuleEngineResultRAO result = context.getValue(RuleEngineResultRAO.class);
final CartRAO cartRAO = context.getValue(CartRAO.class);
Optional<OrderEntryRAO> orderEntry = cartRAO.getEntries()
.stream()
.filter(orderEntryRAO -> orderEntryRAO.getProduct().getDescription().equalsIgnoreCase(value))
.findFirst();
orderEntry.ifPresent(orderEntryRAO -> this.addOrderEntryDiscountRAOAction.addOrderEntryLevelDiscount(orderEntryRAO, false, value, result, context.getDelegate()));
}
public AddOrderEntryDiscountRAOAction getAddOrderEntryDiscountRAOAction() {
return this.addOrderEntryDiscountRAOAction;
}
@Required
public void setAddOrderEntryDiscountRAOAction(final AddOrderEntryDiscountRAOAction addOrderEntryDiscountRAOAction) {
this.addOrderEntryDiscountRAOAction = addOrderEntryDiscountRAOAction;
}
}
We Write Impex File
$lang = en
$trLang = tr
############################# CONDITIONS ##########################
INSERT_UPDATE RuleConditionDefinitionCategory; id[unique = true]; name[lang = $lang]; name[lang = $trLang]; priority
; general ; general ; general ; 1250
INSERT_UPDATE RuleConditionDefinition; id[unique = true] ; name[lang = $lang] ; name[lang = $trLang] ; priority; breadcrumb[lang = $lang] ; breadcrumb[lang = $trLang] ; allowsChildren; translatorId ; translatorParameters; categories(id)
; y_qualifying_searchedWord ; Searched Word ; Searched Word ; 1111 ; Searched Word ; How Many Passenger? ; false ; ruleQualifyingSearchedWordConditionTranslator ; ; ido
INSERT_UPDATE RuleConditionDefinitionParameter; definition(id)[unique = true] ; id[unique = true] ; priority; name[lang = $lang] ; name[lang = $trLang] ; description[lang = $lang] ; description[lang = $trLang] ; type ; value ; required[default = true]; validators;
; y_qualifying_searchedWord ; searchedWord ; 1111 ; Searched Word ; Searched Word ; Searched Word ; Searched Word ; java.lang.Integer ; ; true ;
INSERT_UPDATE RuleConditionDefinitionRuleTypeMapping; definition(id)[unique = true] ; ruleType(code)[unique = true]
; y_qualifying_searchedWord ; PromotionSourceRule
############################## Actions ###########################
INSERT_UPDATE RuleActionDefinition; id[unique = true] ; name[lang = $lang] ; name[lang = $trLang] ; priority; breadcrumb[lang = $lang] ; breadcrumb[lang = $trLang] ; translatorId ; translatorParameters ; categories(id)
; searched_word_percentage_discount ; Percentage discount by the word ; Percentage discount by the word ; 1400 ; Apply {value} discount by the word ; Apply {value} discount by the word ; ruleExecutableActionTranslator ; actionId->ruleSearchedWordAddScoreAction ; general
INSERT_UPDATE RuleActionDefinitionParameter; definition(id)[unique = true] ; id[unique = true]; priority; name[lang = $lang] ; name[lang = $trLang] ; description[lang = $lang] ; description[lang = $trLang] ; type ; value; required[default = true]; validators
; searched_word_percentage_discount ; value ; 1000 ; Percentage discount value ; Percentage discount value ; Percentage discount that will be applied by the word ; Percentage discount that will be applied by the word ; java.lang.String ; ; ; rulePercentageParameterValidator
INSERT_UPDATE RuleActionDefinitionRuleTypeMapping; definition(id)[unique = true] ; ruleType(code)[default = PromotionSourceRule][unique = true]
; searched_word_percentage_discount ;
See you in our next post.