Customize a Commerce view to show or hide information in the Business Tools

Abstract

How to show or hide views in the Business Tools.

By default, child views are generated and added to the GetSellableItemDetailsViewBlock class by the GetSellableItemDetailsViewBlock.AddEntityProperties method in the Catalog plugin. To customize a Commerce view, for example, to remove a child view or extend a view by adding a new property, you create a custom block and configure it to run after the default block. In the custom block, you must first add conditions to identify the correct entity view.

Note

You cannot remove default child views through the Business Tools interface.

To customize a Commerce view to show or hide information:

  1. Add a new EntityView block to the Catalog plugin and name it, for example, CustomViewBlock.

    The new block must take an EntityView as a parameter and return an EntityView.

  2. Add the custom block to the Catalog plugin.

    The following code illustrates a custom block.

    if (request.ViewName != context.GetPolicy<KnownOrderViewsPolicy>().Summary 
                    && request.ViewName != context.GetPolicy<KnownOrderViewsPolicy>().Master)
                {
                    // No action taken if request is for a different view
                    return Task.FromResult(entityView);
                }
                if (request.Entity == null)
                {
                    // No action taken if there is no entity loaded
                    return Task.FromResult(entityView);
                }
                // Continue 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;
                }
                     // Add Points Earned property
                entityViewToProcess.Properties.Add(new ViewProperty { Name = "Points Earned", IsReadOnly = true, RawValue = pointsEarned });
                    // Remove a child view
                var viewToRemove = entityViewToProcess.ChildViews.FirstOrDefault(p=>p.Name == "View To Remove") as EntityView;
                entityViewToProcess.ChildViews.Remove(viewToRemove);
                     // Remove a property
                var propertyToRemove = entityViewToProcess.Properties.FirstOrDefault(p=>p.Name == "Property To Remove") as EntityView;
                entityViewToProcess.Properties.Remove(propertyToRemove);    
                return Task.FromResult(entityView);
  3. Deploy your Sitecore XC solution.