Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bjarne Fyrstenborg 1149 posts 3275 karma points MVP 3x c-trib
    4 days ago
    Bjarne Fyrstenborg
    0

    Get discounts which are target on a product

    In Vendr you can get active discounts within a store using the following method:

    var discounts = Vendr.Core.Web.Api.VendrApi.Instance.GetActiveDiscounts(store.Id);
    

    However is there a way to get discounts directly giving reward on specific products when using Order Line (with Product) Amount Reward?

    enter image description here

    For this case it doesn't need to look at complex rule, but just if the reward it target specific on the product and the type is a percentage reward.

    On the product page I was able to do something like the following, but is there a better way to handle this?

    var store = Model.GetStore();
    
    var udi = new GuidUdi("document", Model.Key);
    decimal? percentageDiscount = null;
    
    var discounts = Vendr.Core.Web.Api.VendrApi.Instance.GetActiveDiscounts(store.Id);
    discounts = discounts.Where(x => x.Rules.Children.Count == 0 && x.Rewards.Count == 1);
    
    var discountsTargetOnProduct = discounts.FirstOrDefault(x => x.Rewards.Any(r => r.Settings.Any(y => y.Key == "nodeId" && y.Value == udi.ToString())));
    
    if (discountsTargetOnProduct != null)
    {
        var percentageReward = discountsTargetOnProduct.Rewards.FirstOrDefault(x => x.Settings.Any(y => y.Key == "adjustmentType" && y.Value == "Percentage"));
        if (percentageReward != null)
        {
            var pctDiscount = percentageReward.Settings["percentage"];
    
            var attempt = pctDiscount.TryConvertTo<decimal>();
            if (attempt.Success)
            {
                percentageDiscount = attempt.Result;
            }
        }
    }
    

    /Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft