SaaS CMS has officially launched! Learn more now.

Apply Discounts to Cart


Commerce 11.8.2

Currently using out of the box promotions. We have an "Apply Promotion Code" input at the basket level.

Looking at the code in Quicksilver we are doing this:

            var cart = _orderRepository.LoadOrCreateCart(_contactId(), Basket.DefaultName, _currentMarket);
            if (cart == null)
                return _commandResultFactory.Create(CreateValidationResult("Error Creating Basket"));

            var couponCodes = cart.GetFirstForm().CouponCodes;
            if (couponCodes.Contains(command.Code))
                return _commandResultFactory.Create(CreateValidationResult("Error Adding Promo Code to Basket"));

            var rewardDescriptions = cart.ApplyDiscounts(_promotionEngine, new PromotionEngineSettings());
            var appliedCoupons = rewardDescriptions.Where(r => r.Status == FulfillmentStatus.Fulfilled).Select(c => c.Promotion.Coupon.Code);
            var couponApplied = false;

                couponApplied = appliedCoupons.Any(c => c.Equals(command.Code, StringComparison.OrdinalIgnoreCase));

            if (!couponApplied)
                return _commandResultFactory.Create(CreateValidationResult("Failed to apply promotion"));

            var orderReference = _orderRepository.Save(cart);

We get no exceptions when doing this and it runs through, but what we're not seeing is any change in lineitems price or total cart price. I can't see what is different between what we're doing and whats in the reference project.

Can anyone shed any light on how to apply promotions on a cart and show the relevent discounted price.

Oct 19, 2018 12:28

Not sure which "lineitem price" that you means, because lineitem has PlacedPrice which is basically the price per unit before discounts, and ExtendedPrice which is price for all unit (for example you are buying 3) after discounts. ExtendedPrice should be changed. If you want to get the new total, you would need to calculate it again because the total might have changed (therefore taxes etc).

Oct 19, 2018 13:02

Hi Quan,

When adding a coupon, applying it and saving the cart no discount is being applied. Looking at the seralized cart lineitems only shows PlacedPrice (price per unit before discount), there is no extended price.

Furthermore running cart.GetOrderDiscountTotal brings back zero too. And running cart.UpdatePlacedPriceOrRemoveLineItems also makes no difference.

Before the new promotions engine all we had to do was:


And then run the workflows and everything would be handled for us. Now this doesn't seem possible.

Oct 19, 2018 13:16

When you run the workflows it is equivalent to .ApplyDiscount + Calculate, so to get the same result you need to Calculate as well.

Do you make sure that the promotion is ready and all other conditions have been statisfied ? 

Oct 19, 2018 13:22

Hi Quan,

As you can see in the code we do actually check if the promotion has been fulfilled and this comes back as true.

The Promotion is active and gets applied. What is interesting is on cart.GetFirstForm() you can see that the coupon code has been added, but within the Promotions collection there is nothing.

Oct 19, 2018 13:30
* 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.