Apply Discounts to Cart

Vote:
 

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"));
            }

            couponCodes.Add(command.Code);
            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.

#198058
Oct 19, 2018 12:28
Vote:
 

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).

#198061
Oct 19, 2018 13:02
Vote:
 

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:

MarketingContext.Current.AddCouponToMarketingContext(couponCode);

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

#198063
Oct 19, 2018 13:16
Vote:
 

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 ? 

#198065
Oct 19, 2018 13:22
Vote:
 

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.

#198067
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.