Skip to Content
Author's profile photo Priti Mittal

SAP Hybris Commerce : Promotion | Same Promotion code for catalogs

Objective : There are the instances  when there is a need to support same promo code across the catalogs. Since Promotion ‘code’ is unique identifier within promotion module. This article has the details to handle such use case – “Same Promotion code for different catalog.”

Description : One of the possible solution is to deploy multiple promotion modules in the Promotion Engine. Associating catalog to different promotion module will resolve the problem. After this , same promotion could be published in all the necessary promotion modules. This way we can have same promo code for different catalogs.

Steps are as follows ,

step -1 Create Promotion module 

  • Below are the required impex files to create new promotion module with name “promotions-ca-module” and map it to testCAProductCatalog Online version.
INSERT_UPDATE DroolsKIEModule ; name[unique=true]    ; mvnGroupId   ; mvnArtifactId ; mvnVersion ; ruleType(code) ; active ; version ;  
                              ; promotions-ca-module ; hybris-rules ; promotions-ca ; 1.0.0      ; PROMOTION      ; true   ; -1      ;  

INSERT_UPDATE DroolsKIEBase ; name[unique=true]  ; kieModule(name)[unique=true] ; equalityBehavior(code) ; eventProcessingMode(code)
                            ; promotions-ca-base ; promotions-ca-module         ; EQUALITY               ; STREAM                   

INSERT_UPDATE DroolsKIESession ; name[unique=true]     ; kieBase(name)[unique=true] ; sessionType(code)
                               ; promotions-ca-session ; promotions-ca-base         ; STATELESS        

INSERT_UPDATE DroolsRuleEngineContext ; name[unique=true]     ; kieSession(name)      ; ruleFiringLimit
                                      ; promotions-ca-context ; promotions-ca-session ; 200            ;  

UPDATE DroolsKIEBase ; name[unique=true]  ; defaultKIESession(name)
                     ; promotions-ca-base ; promotions-ca-session 
UPDATE DroolsKIEModule ; name[unique=true]    ; defaultKIEBase(name)
"#% afterEach: de.hybris.platform.core.Registry.getApplicationContext().getBean(""ruleEngineSystemSetup"").initializeModule(impex.getLastImportedItem());"
                       ; promotions-ca-module ; promotions-ca-base  

INSERT_UPDATE CatalogVersionToRuleEngineContextMapping ; context(name)[unique=true] ; catalogVersion(catalog(id),version)[unique=true]
                                                       ; promotions-ca-context      ; testCAProductCatalog:Online 

 

  • Promotion Module – Backoffice view : with new promo module

 

step-2 :  Publishing promotion from backoffice 

  • Navigate to Marketing -> Promotion rules -> Show All
  • Select promotion for example : target_customer_order_threshold_fixed_discount
  • click on publish rule , choose module let say promotions-module to publish.

  • After this promotion would be available for USOnline catalog to storefront .
  • Edit promotion Rule. Change website from one group to other . Here we have taken the example to change the group from USPromoGroup to CAPromoGroup
  • Now re-publish the promotion to different module let say ,promotions-ca-module.

  • This way promotion with same code could be deployed and would be  available for both the modules.

 

step-3 :  Publishing promotion via impex 

The use case here is to publish promotion rule in promotions-module and promotions-ca-module with the help of impex

To deploy the promotion rule with same code into more than one promotion module , will introduce translator . This translator will take care of promotion version so that correct version could be picked up to insert/update the values such as website, condition action etc.

Translator : Sample Code

package com.test.core.impex.translators;
import de.hybris.platform.core.Registry;
import de.hybris.platform.impex.jalo.translators.AbstractValueTranslator;
import de.hybris.platform.jalo.Item;
import de.hybris.platform.jalo.JaloInvalidParameterException;
import de.hybris.platform.jalo.enumeration.EnumerationValue;
import de.hybris.platform.promotionengineservices.jalo.PromotionSourceRule;
import de.hybris.platform.promotionengineservices.model.PromotionSourceRuleModel;
import de.hybris.platform.ruleengineservices.enums.RuleStatus;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;

@SuppressWarnings("deprecation")
public class PromotionVersionTranslator extends AbstractValueTranslator {
	
	private static final String PROMOTION_QUERY_PART1 = "select {pk}  from {PromotionSourceRule} where {code} = '";
	private static final String PROMOTION_QUERY_PART2 = "' AND  {status} IN ({{select {pk}  from {EnumerationValue} where {code} IN  ('PUBLISHED','UNPUBLISHED','INACTIVE') }}) order by {version} desc";


	@Override
	public String exportValue(Object var1) throws JaloInvalidParameterException {
		if(var1 instanceof PromotionSourceRule ) {
			return ((PromotionSourceRule)var1).getCode();
		}
		return var1.toString();
	}

	@Override
		public final Object importValue(String valueExpr, Item toItem) throws JaloInvalidParameterException {
		this.clearStatus();
		Object ret=findVersionValue( valueExpr);
		return ret;
	}

	private Long findVersionValue(String code) {
		
		
		final FlexibleSearchQuery query = new FlexibleSearchQuery(PROMOTION_QUERY_PART1+code+PROMOTION_QUERY_PART2 );
		List<PromotionSourceRuleModel> results =  getFlexibleSearchService().<PromotionSourceRuleModel> search(query).getResult();

		Long finalVersion = Long.valueOf(0);
		if(CollectionUtils.isNotEmpty(results)) {
			PromotionSourceRuleModel promotionSourceRuleModel = results.get(0);
			PromotionSourceRule promotionSourceRule = getModelService().getSource(promotionSourceRuleModel);
			EnumerationValue status = promotionSourceRule.getStatus();
			Long version = promotionSourceRule.getVersion();
			if(status.getCode().equals(RuleStatus.UNPUBLISHED.getCode())) {
				finalVersion= version;
			}
			else {
				finalVersion= version+1;
			}
			
		}
		return finalVersion;
			}

	private FlexibleSearchService getFlexibleSearchService() {
		return (FlexibleSearchService) Registry.getApplicationContext().getBean("flexibleSearchService",
				FlexibleSearchService.class);
	}
	
	private ModelService getModelService() {
		return (ModelService) Registry.getApplicationContext().getBean("modelService",
				ModelService.class);
	}
}

 

  • Below impex header with groovy script will handle the deployment of promotion (same code) to promotion module promotions-module and promotions-ca-module simultaneously .
$defaultPromoGrp=CAPromoGrp
$defaultStatus=PUBLISHED 


INSERT_UPDATE PromotionSourceRule ; code[unique=true] ; version[unique=true][translator=com.test.core.impex.translators.PromotionVersionTranslator]; name[lang=en]                                                                          ; status(code) ; priority ; maxAllowedRuns ; stackable[default=false] ; ; actions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ; description[lang=en]                                   
"#% afterEach: de.hybris.platform.core.Registry.getApplicationContext().getBean(""ruleCompilerService"").compile( de.hybris.platform.core.Registry.getApplicationContext().getBean(""modelService"").get(impex.getLastImportedItem()),""promotions-ca-module"");  de.hybris.platform.core.Registry.getApplicationContext().getBean(""platformRuleEngineService"").initializeAllRulesModules();"
                                  
 #Test promotion                 
; test; test; test promotion;PUBLISHED; 40; 1; true                    ; orderPromotionRuleGroup; <replace with condition>; "[{""definitionId"":""y_trigger_message"",""parameters"":{}}]"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ;; This is test promo. ; This is test promo.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                



$defaultPromoGrp=USPromoGrp
$defaultStatus=PUBLISHED 

INSERT_UPDATE PromotionSourceRule ; code[unique=true] ; version[unique=true][translator=com.test.core.impex.translators.PromotionVersionTranslator]; name[lang=en]                                                                          ; status(code) ; priority ; maxAllowedRuns ; stackable[default=false] ; ; actions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ; description[lang=en]                                   
"#% afterEach: de.hybris.platform.core.Registry.getApplicationContext().getBean(""ruleCompilerService"").compile( de.hybris.platform.core.Registry.getApplicationContext().getBean(""modelService"").get(impex.getLastImportedItem()),""promotions-module"");  de.hybris.platform.core.Registry.getApplicationContext().getBean(""platformRuleEngineService"").initializeAllRulesModules();"
                                  
 #Test promotion                 
; test; test; test promotion;PUBLISHED; 40; 1; true                    ; orderPromotionRuleGroup; <replace with condition>; "[{""definitionId"":""y_trigger_message"",""parameters"":{}}]"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ;; This is test promo. ; This is test promo. 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.