Popular Posts

How to evaluate promotion without user adding the item to cart ?

WCS OOB cannot evaluate order level promotions, without adding them to cart. This can be achieved using a workaround.
For catentry level promotions, OOB PromotionalPriceDataBean can be used.

The below are the 2 objects that the promotion engine acts upon.
  • com.ibm.commerce.marketing.promotion.integration.dependency.WCSOrder
  • com.ibm.commerce.marketing.promotion.integration.dependency.WCSOrderItem
Sample Code
WCSOrder order = null;
WCSOrderItem orderItem = null;
List orderItemList = null;

//Populate the OrderItemId, store, catentry information to the WCSOrderItem Object
orderItem.setKey(new OrderItemKey(new Long("1"));
orderItem.setStore(sKey);
orderItem.setCatalogEntry(catKey);
orderItem.setBaseCurrency(getCommandContext().getCurrency());
orderItem.setCurrency(getCommandContext().getCurrency());              
orderItem.setStandardPrice(100);
orderItem.setPrice(itemPrice);
orderItem.setQuantity(new BigDecimal(1));                                  
orderItem.setShippingCharge(new BigDecimal(10));
orderItem.setShippingCarrier("UPS");
orderItem.setShippingCode("Ground");
orderItemList.add(orderItem);
       
// Populate the WCSOrder Object with order related data
order.setKey(new OrderKey(new Long(this.ucOrderDataBean.getWCSorderId())));
order.setStoreId(this.storeId);
order.setCurrency(getCommandContext().getCurrency());
order.setOwner(getCommandContext().getUserId());
order.setLastUpdate(getCommandContext().getTimestamp());                       
order.setTotal(new BigDecimal(110);
 order.setTotalShippingCharge(new BigDecimal(10));
       
 // Populate the order Items information to the wcsOrder
OrderItem[] orderItems = new WCSOrderItem[this.orderItemList.size()];
orderItems = (WCSOrderItem[])this.orderItemList.toArray(orderItems);
order.setItems(orderItems);

DefaultAdvancedPromotionEngine defaultPromotionEngine = null;                        
                               
//Get the default promotion engine
PromotionEngine promotionEngine = PromotionEngineHome.getDefaultEngine();          
                               
if(promotionEngine instanceof DefaultAdvancedPromotionEngine){
    defaultPromotionEngine = (DefaultAdvancedPromotionEngine)promotionEngine;                                          
}
               
PromotionContext promotionContext = defaultPromotionEngine.createPromotionContext(this.order, getCommandContext(), "All Promotions", null, false);
promotionContext.getAgenda().setShippingChargePresent(true);
               
//Invoke the promotion context to apply the promotions to the order
 defaultPromotionEngine.invoke(promotionContext);
                    
//Get the promotion Arugement, which has the applied promotion details
this.promotionArgument = promotionContext.getArgument();

// Iterate over the list of promotion execution records returned by the promotion engine
Iterator aIterator = promoResult.getPromotionExecutionRecords();

while (aIterator.hasNext()) {
    PromotionExecutionRecord aPromotionExecutionRecord = (PromotionExecutionRecord)aIterator.next();
    if (16 == aPromotionExecutionRecord.getState()) {
        BigDecimal currentDiscount = aPromotionExecutionRecord.getMonetaryAdjustmentForOrder(1);
        LineItemSet[]  lineItemSets = aPromotionExecutionRecord.getAffectedItems();
        for(int i=0; i<lineItemSets.length; i++) {
            LineItemSet lineItemSet = lineItemSets[i];
            AssociatedOrderItem[] associatedOrderItems = lineItemSet.getAssociatedOrderItems();
            for(int j=0; j<associatedOrderItems.length; j++) {
                 //AssociatedOrderItem has the list of all order items and the promotions against them
                AssociatedOrderItem associatedOrderItem = associatedOrderItems[j];
            }
        }
    }
}

2 comments: