• Welcome to KonaKart Community Forum. Please login or sign up.
 

how to work with carts, products and promotions through KonaKart API

Started by dexequiel.garcia, April 23, 2014, 05:56:45 pm

Previous topic - Next topic

dexequiel.garcia

Hello guys,

We are using a custom portlet that invokes the Java API to interact with KonaKart application. Our objective, for now, is learning how to use the API and test how some features work through it.

One of the tests we are doing consists of generating an order whose set of products applies for a promotion, for instance, "buy X get Y". In the front store application, this works as expected: if the product A has a "buy 3 get 4" promotion, when adding 4 instances of product A to the cart, an order total line is generated in which you get a discount for one of the 4 instances of the product. However, when coding against the API we can't figure out how to achieve this. The promotion is already created and linked to Product A, and the product is already added to the basket with quantity = 4. This is what we have so far:
               

int productId = 1; // HARDCODED
int customerId = 2; // HARDCODED
int languageId = 1; // HARDCODED
EngineConfig engineConfig = new EngineConfig();
KKEngIf engine = new KKEng(engineConfig);
String sessionId = engine.login("admin@konakart.com", "princess");
ProductIf productIf = engine.getProduct(sessionId, productId,
         languageId);
engine.removeBasketItemsPerCustomer(sessionId, customerId);
BasketIf basket = new Basket();
basket.setProduct(productIf);
basket.setProductId(productId);
basket.setQuantity(4);
BasketMgr basketMgr = new BasketMgr(engine);
basketMgr.addToBasket(sessionId, customerId, basket);
BasketIf[] baskets = basketMgr.getBasketItemsPerCustomer(sessionId,
         customerId, languageId);
OrderMgr orderMgr = new OrderMgr(engine);
Order order = orderMgr.createOrder(sessionId, baskets, languageId);
OrderTotalMgr orderTotalMgr = new OrderTotalMgr(engine);
order = orderTotalMgr.getOrderTotals(order, languageId);
OrderTotalIf[] orderTotals = order.getOrderTotals();
System.out.println("TOTALS: ");
for (int i = 0; i < orderTotals.length; i++) {
        System.out.println(orderTotals[i]);
   System.out.println("Promotions: ");
   System.out.println(orderTotals.getPromotions());
}


This code, as a result, prints the items in the basket but no promotions at all.
Our question is: what method in what object (BasketMgr, OrderMgr, OrderTotalMgr) should return the discount? We haven't fount a method that shows the discount either in BasketMgr or in OrderMgr or in OrderTotalMgr and we haven't found any tutorials that explains how to manipulate products, carts or orders.
As per KK docs (http://www.konakart.com/docs/PromotionsForProducts.html) this should be in the OrderTotal module but we haven't found such object containing the expected method in there either.

We would know how this should work and, furthermore, if there are more narrowed walkthrough tutorials or documentation on how to manipulate KK objects
Thanks in advance for your answer
Regards

ryan

You should always use KKEngIf and not the individual managers.

Take a look at InsertOrder.java under java_api_examples\src\com\konakart\apiexamples which shows you how to create an order and get the order totals which include the promotion.

dexequiel.garcia

Thanks, Ryan, for so fast answer!

Whether using individual managers or just the engine was one of my doubts.
I still have one more question... Let us suppose I create an order with

                BasketIf basket = new Basket();
basket.setProduct(productIf);
basket.setProductId(productId);
basket.setQuantity(4);

engine.addToBasket(sessionId, customerId, basket);

BasketIf[] items = engine.getBasketItemsPerCustomer(sessionId,
customerId, languageId);

OrderIf order = engine.createOrder(sessionId, items, languageId);

the object order still does not have the details about the discount. Let's guess we need to save the order to make the discount available (Sounds silly but please let me know if there is any extra requirement to generate the discount and/or OrderTotal lines)

int orderId = engine.saveOrder(sessionId, order, languageId);

Still nothing. Let's now get the order details from the database (it should come with all the corresponding embedded objects it is related to)

order = engine.getOrder(sessionId, orderId, languageId);

If I try to print the order through the system console, nothing is printed related to orderTotals or discounts applied.

So my question is: should I do some extra call to generate the discount from a promotion that already exists and is related to the product with id = 1 (see hardcoded data above) and applies to all the customers (works good in the admin app)?

Thanks again!

ryan

I don't understand why you don't just follow the example all the way to where it gets the order totals for the order which is before it is saved.

       /*
         * Now that the order has been completed with all necessary information we can ask the
         * engine to calculate the order totals. We send it the order and receive back the same
         * order which includes order totals that the customer may check before confirming.
         */
        order = eng.getOrderTotals(order, DEFAULT_LANGUAGE);

dexequiel.garcia

Ryan, thanks for answering again,

I did, actually it's the first option I tried, but it didn't work. If I print the order through the console after executing order = engine.getOrderTotals(order, languageId);, it prints this:

OrderTotal = OrderTotal:
id        = 0
orderId   = 0
title     = Sub-Total:
text      = $80.00
value     = 80.0000
- Attachments and other options
Notify me of replies.
sortOrder = 1
className = ot_subtotal
custom1   = null
custom2   = null
custom3   = null

OrderTotal = OrderTotal:
id        = 0
orderId   = 0
title     = Flat Rate (Shipping):
text      = $5.00
value     = 5.00
sortOrder = 2
className = ot_shipping
custom1   = null
custom2   = null
custom3   = null

OrderTotal = OrderTotal:
id        = 0
orderId   = 0
title     = Total:
text      = <b>$85.00</b>
value     = 85.0000
sortOrder = 40
className = ot_total
custom1   = null
custom2   = null
custom3   = null


whereas an equivalent order in the front app looks like the image attached.

Thanks again