Thursday, May 7, 2015

Customization Shipping Charge in Websphere Commerce

1) Shipping Charge Customization scenario:

In some cases you may need to customize your shipping calculation. For Example If you are getting the Item Level Shipping charge from third party server, Please use the following code snippets to achieve that.

a)Extends the OOTB ApplyShippingCmdImpl and add the cmdreg entry

b)Override the setItems and performExecute Method.


    // The order items of current order
private Item iItems[];

    public void setItems(Item aItems[]) {
String methodName = "setItems";
LOGGER.entering(CLASSNAME, methodName);

LOGGER.logp(Level.FINE, CLASSNAME, methodName, "setItems -" + aItems);
iItems = aItems;
super.setItems(aItems);

LOGGER.exiting(CLASSNAME, methodName);
}

public void performExecute() throws ECException {
String methodName = "performExecute";
LOGGER.entering(CLASSNAME, methodName);
super.performExecute();
if (iItems != null && iItems.length > 0) {
try {
for (int index = 0; index < iItems.length; index++) {
Item iItem = iItems[index];
iItem.setShippingTotal(<Price value>);
iItem.commit();
LOGGER.logp(Level.FINE, CLASSNAME, methodName, "Shipping charge is updated for - " + iItem.getOrderItem().getOrderItemId());
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CreateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FinderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
LOGGER.logp(Level.FINE, CLASSNAME, methodName, "No items found.");
}
LOGGER.exiting(CLASSNAME, methodName);
}

c)Based on the Item Shipping charge, the command will calculate the Order level shipping amount.

2) Shipping Promotion Customization:

a) Read the order total value from custom field for  apply the shipping promotion

By using the items object calculate the order total,

private BigDecimal getOrderTotal(Item[] items)
throws RemoteException, CreateException, FinderException,
NamingException {
String methodName = "getOrderTotal";
LOGGER.entering(CLASSNAME, methodName);
BigDecimal totalOrder = BigDecimal.ZERO;
for (int index = 0; index < items.length; index++) {
OrderItemAccessBean orderItemAccessBean = items[index].getOrderItem();
totalOrder = totalOrder.add(orderItemAccessBean.getTotalProductInEJBType().add(orderItemAccessBean.getTotalAdjustmentInEJBType()));
LOGGER.logp(Level.FINEST, CLASSNAME, methodName, "Order Total - " + totalOrder);
}
LOGGER.exiting(CLASSNAME, methodName, methodName);
return totalOrder;
}

If the order total over than maximum value for promotion, 

if(OrderTotal >= maximum amount){
for (int index = 0; index < iItems.length; index++) {
Item iItem = iItems[index];
iItem.setShippingTotal(0.00);
iItem.commit();
LOGGER.logp(Level.FINE, CLASSNAME, methodName, "Shipping charge is updated for - " + iItem.getOrderItem().getOrderItemId());
}
}



No comments:

Post a Comment