Custom promotion policies
The promotion engine is configured with a set of default promotion policies that enforce rules related to promotion exclusivity, combining promotions, redemption limits, and so on. If the default set of promotion policies do not satisfy your business needs, you can implement custom promotion policies.
The set of default policies are listed in promotion organization. Introducing custom promotion policies is a two phase effort. Phase one is the implementation of your new policies. Phase two is either assigning the policy to one of your promotion groups or marking it as a global policy. To assign a policy to a group, refer to customizing promotion organization. If you create a policy and mark it as active, but do not assign it to any group, the policy is considered to be global.
All custom policies must implement the com.ibm.commerce.marketing.promotion.policy.PromotionPolicy interface. This interface is a subclass of XMLizable, which means that you must implement the toXML and fromXML methods, and design a serialized XML form for your policy.
The key method of a promotion policy is the apply method. It takes two parameters: PromotionContext and PromotionExecutionRecord. A promotion policy is called when a promotion's conditions are satisfied, and the adjustments of the promotion have been "tentatively" applied to the PromotionContext. The responsibility of a promotion policy, fulfilled by the apply method, is to check the state of the PromotionContext to determine whether there are any violations. A promotion policy must read the un-committed PromotionContext to determine the effect of applying the current promotion to the order. The details of the PromotionContext are found in the API documentation. In particular, note the methods that work with the un-committed content on the PromotionContext. The apply method must return a Boolean value indicating whether a violation is found (true) or not (false).
The following sample code is a sample policy:
package com.ibm.commerce.marketing.promotion.policy;
import com.ibm.commerce.marketing.promotion.reward.MonetaryAdjustment;
import com.ibm.commerce.marketing.promotion.runtime.PromotionContext;
import com.ibm.commerce.marketing.promotion.runtime.PromotionExecutionRecord;
/**
* Checks whether applying a promotion drops the order running total to zero or not.
*/
public class NoneZeroOrderTotalPolicy extends PromotionPolicyBase {
/**
* IBM copyright notice field.
*/
public static final String COPYRIGHT =
com.ibm.commerce.copyright.IBMCopyright.SHORT_COPYRIGHT;
/**
* Constructor.
*
*/
public NoneZeroOrderTotalPolicy(){
super();
}
/**
* This method checks if the order total will drop to zero or not
* after a promotion is applied.
* @return true if the order total remains positive,
* false if the order total is zero or negative.
* @see com.ibm.commerce.marketing.promotion.policy.
* PromotionPolicy#apply(
* com.ibm.commerce.marketing.promotion.runtime.PromotionContext
* com.ibm.commerce.marketing.promotion.runtime.PromotionExecutionRecord)
*/
public boolean apply(
PromotionContext context,
PromotionExecutionRecord record)
throws PromotionPolicyApplicationException{
return (
context.getOrderRunningTotal(MonetaryAdjustment.PRICE).signum()>0);
}
}
Note that the PromotionPolicyBase class provides the basic toXML and fromXML methods for promotion policies that do not have a serialized XML form beyond the simplest form illustrated below:
<PromotionPolicy impl="fully qualified implementation class name">
<PromotionPolicyKey>
<PolicyName>Unique Name for the policy</PolicyName>
<StoreKey>
<DN>o=root organization<DN>
<Identifier>BlueStore 202</Identifier>
</StoreKey>
</PromotionPolicyKey>
<Status>Active</Status>
</PromotionPolicy>