November Happy Hour will be moved to Thursday December 5th.

How to apply an order discount in ICart cart

Vote:
 

Hi guys!

I 'm new to the development of Episerver Commerce.

I have a question about ApplyDiscount function in commerce. I created a sale campaign with built in order discount (Spend for discount order) in commerce markering, Spend over 1000kr get 10% discount with a coupon code.

In my code, I would like to add the coupon code to the cart and check if the coupon code is valid. If so, showing the coupon code is applied successfully and the save amount in the checkout page. 

In order to archive this, I tried to do the same solution as Quicksliver. 
var rewardDescriptions = ApplyDiscounts(cart);

public IEnumerable ApplyDiscounts(ICart cart) 
{ 
return cart.ApplyDiscounts(_promotionEngine, new PromotionEngineSettings()); 
}

Unfortunately I could not get any rewardDescriptions(null) when I ran the ApplyDiscount(cart) method. 
My question is do I have to create a purchase order and run the Applydiscount(purchseorder)? Since the campaign that I have created is an order discount? 

In the current solution, the purchase order will be created when the "Place order" button is chosen, I would like to show the coupon code is applied successfully and the save amount before that. 

Please provide me some suggestions

Thanks!

#196925
Sep 17, 2018 13:17
Vote:
 

Hey! 

You don't need to create the purchaseOrder in order to get discounts. They work the same way (ish) on carts as on finished orders. 

The only thing you should need to do is

  1. Add the coupon code to the cart
  2. Run the promotion engine
  3. Save the cart

It looks like you haven't done step 1. After you've added it to the cart and the promotion is set up correctly, the promotion engine should kick in and give you new prices and redemptions. Here's how quicksilver does step 1, https://github.com/episerver/Quicksilver/blob/master/Sources/EPiServer.Reference.Commerce.Site/Features/Checkout/Controllers/CheckoutController.cs#L117.

Always good to sanity check that it's nothing else in the setup of the campaign/promotion that prevents it from showing, you can verify versus a checklist here: https://www.avensia.com/joel-yourstone/why-isnt-my-promotion-showing/

#196926
Sep 17, 2018 13:49
Vote:
 

As Joel said, IPromotionEngine (and its extension methods) works on IOrderGroup, which means that you can use them on carts, purchase orders or payment plans (and technically any implementation of IOrderGroup that you have, like, "Quote")

In your post you mention that customers need a coupon to be entitled to the promotion. Did you add that to your cart?

cart.GetFirstForm().Coupons.Add(myCoupon); ?

#196928
Sep 17, 2018 14:49
Vote:
 

I just checked Joel suggestion and dubbelchecked my code, https://github.com/episerver/Quicksilver/blob/master/Sources/EPiServer.Reference.Commerce.Site/Features/Checkout/Controllers/CheckoutController.cs#L117.

I did the first step cart.GetFirstForm().Coupons.Add(myCoupon), add couponcode to my car first form but I forgot to save _orderRepository.Save(cart) before I ran the ApplyDiscount. 

Thanks Joel and Quan for the quick response! I appreciated that :)

#196929
Sep 17, 2018 15:08
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.