Quantcast
Viewing all articles
Browse latest Browse all 9076

Compare between prices for an item with applicable promotions

Hi,

I am using Commerce 12.15, I have a requirment where I need to compare between the sale price and the promotion price (All lineitem promotions only). The logic is as follows:

An item 001 has a Regular price $10.00, the sale price is $6.00. Now they want me to apply the discount on the regular price and see if it is even lower than the current sale price.

I am able to check if the line item has an applicable promotion lower than the sale price, but I am not sure where I need to add the logic to find the lowest price should be inserted. 

My implementation so far is as follows:

I am overriding UpdatePlacedPrice(ILineItem lineItem, CustomerContact customerContact, MarketId market, Currency currency, Action<ILineItem, ValidationIssue> onValidationError) from DefaultPlacedPriceProcessor class.

There I get the current shopping cart and use the lineitems in the cart to create a InMemomryOrderGroup and apply the coupon. This gives me the collection of rewards, which I can use to compare against the sale price. I have created 3 metafields on the line item level as well, Original Price ex: $10.00, SalePrice: $6.00 and $PromoPrice $xx.xx. Now I want to check the lowest price between SalePrice and PromoPrice. But the promotions engine is always discounting on the placed price, so this leads to a double discount $6.00 being further discounted. I know this is the default behaviour of EPi, but I am looking for ideas or suggestions to overcome this. 

Below is my sample code :

                        if (cart.GetFirstForm().Promotions.Any())
                        {
                            PromotionInformation promo = cart.GetFirstForm().Promotions.Where(x => x.DiscountType == DiscountType.LineItem).FirstOrDefault();
                            if (promo != null)
                            {
                                var inMemoryOrderGroup = new InMemoryOrderGroup(currentMarket, ServiceLocator.Current.GetInstance<CurrencyService>().GetCurrentCurrency().CurrencyCode);
                                inMemoryOrderGroup.AddLineItem(lineItem);
                                if (!string.IsNullOrEmpty(promo.CouponCode))
                                {
                                    inMemoryOrderGroup.GetFirstForm().CouponCodes.Add(promo.CouponCode);      
                                }
                                var rewards = ServiceLocator.Current.GetInstance<IPromotionEngine>().Run(inMemoryOrderGroup);
                                if (rewards.Any())
                                {
                                    RewardDescription reward = rewards.OrderByDescending(x => x.SavedAmount).FirstOrDefault();
                                    decimal promoFinalPrice = reward.Redemptions.FirstOrDefault().AffectedEntries.PriceEntries.FirstOrDefault().Price;
                                    lineItem.Properties[Constants.Metadata.LineItem.PromoPrice] = promoFinalPrice;
                                    lineItem.Properties[Constants.Metadata.LineItem.SalePrice] = placedPrice.Value.Amount;
                                    //If promo price is greater than sale price, pass the regular price to be used by the promotions engine
                                    if (promoFinalPrice < highestDiscount)
                                    {
                                        lineItem.PlacedPrice = regPrice.UnitPrice.Amount;
                                    }
                                    else
                                    {
                                        lineItem.PlacedPrice = placedPrice.Value.Amount;
                                    }
                                  }
                               }
                           }

Viewing all articles
Browse latest Browse all 9076

Trending Articles