In KonaKart a promotion is calculated by an OrderTotal module. When an order is created, the promotion module calculates the value of the promotion discount considering all of the data contained in the order and generates a promotion line item (OrderTotal) for the order containing the discount.
Sometimes it is convenient to calculate and display the promotion discount for an array of products without requiring that the customer has added the products to the cart. In this way the customer may see the discount and be tempted to add the products to the cart.
A KonaKart OrderTotal module has a method that may be implemented to calculate a discount for a single product. The method is called:
PromotionResult getPromotionResult(Product product, Promotion promotion)
An example of its implementation may be seen in the ProductDiscount promotion module which can be found under the custom\modules\src\com\konakart\bl\modules\ordertotal\productdiscount directory. The method creates and returns a PromotionResult object which contains information to identify the promotion, the value of the promotion, whether it is a percentage or value discount and other information. The PromotionResult is attached to the product by the KonaKart engine. If the product is configurable (i.e. size, color etc.) each configuration may have it's own array of PromotionResult objects enabling you to discount for example a blue shirt but not a red one.
The API call that evaluates the promotions for an array of products (Enterprise Only) is:
ProductIf[] getPromotionsPerProducts(String sessionId, int customerId, ProductIf[] products, PromotionIf[] promotions, String[] couponCodes, PromotionOptionsIf options)
A full description can be found in the Javadoc. The method receives an array of products and returns an array of products with attached PromotionResult objects that contain the results of one or more of the promotions passed in as a parameter. A list of active promotions can be fetched using the following API call:
PromotionIf[] getAllPromotions()
In the KonaKart demo storefront application, the promotions are cached in order not to retrieve them every time since this would degrade performance. The promotions may also be filtered and only a subset sent to the API call to calculate the discount.
An example of this promotion functionality may be found in CatalogMainPageAction.java . The code (which is commented out) passes the array of new products to the API call in order to determine whether any promotions apply to the products. The JSP called NewProductsWithDetailBody.jsp displays the promotion name next to the product image if the product has a promotion.
So to summarize, to activate the promotion for products functionality in our demo storefront application for the display of the "Latest Products", you need to take the following steps:
Using the Admin App, create a ProductDiscount promotion and activate the ProductDiscount Order Total Module.
In CatalogMainPageAction.java, uncomment the code that makes the getPromotionsPerProducts() API call and compile the action class.
At this point, the Latest Products tile on the home page of the storefront should display the promotion name next to each product if the promotion is active. You may want to modify this to display an image showing the promotion details.
In order to activate this functionality in other parts of the storefront app, you will need to add code in other struts action classes similar to the code in CatalogMainPageAction.java.