Get the List Price and Price Group Price of a Product Variant in Episerver Commerce

On a recent client project, a requirement was to be able to set a list price and a member price for each product variant. To accomplish this, I utilized Episerver Commerce price groups. The first challenge was to locate where in Commerce Manager to setup price groups. 

After some searching, I found what I needed. In Commerce Manager, click on the Customer Management tab, then click Contacts. In the list of contacts, choose one and click the pencil and paper icon to the left of the name. This takes you to the overview tab, which has a line for Customer Group, that, by default, is set to [No value]. When you click the icon to the right of the dropdown (see below), it brings up a list of current Customer Groups, and a link to create a new one. 

So, now that we know where to create the Member price group, let's write some code to be able to retrieve both the list price and the member price for a variant. I like to create an abstract base class for my product variant's and inside this class, I have 2 extension methods to pull the correct price.

 
   private Price _ListPrice;
   private Price _MemberPrice;

  [Ignore]
  public virtual Price ListPrice
  {
       get
       {
            if (_ListPrice == null)
            {
                 var entry = this.LoadEntry(CatalogEntryResponseGroup.ResponseGroup.Variations);

                 if (entry == null)
                        return null;

                  var price = entry.PriceValues.PriceValue                     
                        .OrderByDescending<PriceValue, decimal>((PriceValue p) => p.UnitPrice.Amount).FirstOrDefault();

                    if (price == null)
                        return null;

                    _ListPrice = new Price(price.UnitPrice);
            }

                return _ListPrice;
        }
   }

   [Ignore]
   public virtual Price MemberPrice
   {
        get
        {
             if (_MemberPrice == null)
             {
                  var entry = this.LoadEntry(CatalogEntryResponseGroup.ResponseGroup.Variations);

                  if (entry == null)
                      return null;

                  var price = entry.PriceValues.PriceValue                             
                              .Where(x => x.CustomerPricing.PriceCode.Equals("Member"))
                              .OrderByDescending((PriceValue p) => p.UnitPrice.Amount).FirstOrDefault();   
if (price == null) return null; _MemberPrice = new Price(price.UnitPrice); } return _MemberPrice; } }

That is all there is to it. Now that we have our extension methods, all we have to do in our view is call @product.ListPrice and @product.MemberPrice to display the correct pricing for each product variant. The key to pulling the correct price group is PriceCode.Equals("YourCustomPriceGroup").

If you are interested in seeing the code in the context of an application, you can check out my Episerver Commerce Sandbox in Github. I used this recently for a talk I gave at an Episerver meetup in Chicago at Nansen earlier this summer. I am going to continue to update this project and write more articles with tips and tricks for Episerver CMS and Commerce. Please comment below if you have any questions or comments.

0 comment(s) in response to Get the List Price and Custom Price Group Price of a Product Variant in Episerver Commerce

Have a comment?