Determine Discounts for a Particular SKU

Irina K asked on February 24, 2019 16:59

We have various product discounts including ones for a specific user role. Say, we have Product A which is $50. It has a discount of 20% for all site users and a 40% discount for users in a specific member role.

I need to be able to display the tags for the sale next to the products but if the user is logged in and has the appropriate role (the allowable roles for a SKU are a provided by a custom field on the SKU object) then I need to display "member discount" on the tag.

I'm able to get the discounted price using EcommerceFunctions but I can't find a way to determine whether a "role-based" discount has been applied.

Is there any OOTB function that I can use to get the discounts applicable to a particular SKU so that I can then filter by the role and see if the user is in it?

What I have so far is this:

  var discountParams = new DiscountsParameters();  
  discountParams.User = CurrentUser;
  discountParams.Enabled = true;
  discountParams.SiteID = CurrentSite.SiteID;

  var discountsForRoles = DiscountInfoProvider.GetDiscounts(discountParams)
          .WhereContains("DiscountApplyTo", DiscountApplicationEnum.Products.ToString())
          .WhereEquals("DiscountCustomerRestriction", "SelectedRoles");

Of course now that I get all the discounts list, I need a way to loop through them and evaluate the rule to see if they apply to the particular product. Seems very cumbersome and highly inefficient.

Any easier ways to accomplish this?

   Please, sign in to be able to submit a new answer.