Extend a Commerce view to show additional information in the Business Tools

Abstract

How to extend a view in the Business Tools to show additional information about an entity.

You can extend a Commerce view to show additional information in the Sitecore XC Business Tools user interface. The following is an example of how you can extend an Entity View to display the summary of points that have been earned on the Order Details page. We extend the view by adding a new EntityView block.

To extend a commerce view to show additional information:

  1. Copy and paste the sample block and name it GetOrderSummaryViewBlock.

  2. The GetEntityView blocks take an EntityView as a parameter and return an EntityView. Because this extension must see an order, you must add a reference to the Orders plugin, for example:

    Sitecore.Commerce.Plugin.Orders": "1.0.2301
  3. Add the new block:

    if (request.ViewName != context.GetPolicy<KnownOrderViewsPolicy>().Summary 
                    && request.ViewName != context.GetPolicy<KnownOrderViewsPolicy>().Master)
                {
                    // Do nothing if this request is for a different view
                    return Task.FromResult(entityView);
                }
                if (request.Entity == null)
                {
                    // Do nothing if there is no entity loaded
                    return Task.FromResult(entityView);
                }
                // Only do something if the Entity is an order
                if (!(request.Entity is Order))
                {
                    return Task.FromResult(entityView);
                }
                var order = request.Entity as Order;
                EntityView entityViewToProcess;
                if (request.ViewName == context.GetPolicy<KnownOrderViewsPolicy>().Master)
                {
                    entityViewToProcess = entityView.ChildViews.FirstOrDefault(p=>p.Name == "Summary") as EntityView;
                }
                else
                {
                    entityViewToProcess = entityView;
                }
                int pointsEarned = 0;
                foreach(var line in order.Lines.Where(p=>p.HasComponent<LoyaltyComponent>()))
                {
                    pointsEarned = pointsEarned + line.GetComponent<LoyaltyComponent>().Points;
                }
                entityViewToProcess.Properties.Add(new ViewProperty { Name = "Points Earned", IsReadOnly = true, RawValue = pointsEarned });
                return Task.FromResult(entityView);