1. チュートリアルとサンプル シナリオ

Commerceビューをカスタマイズして、Business Toolsで情報を表示または非表示にする

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

Commerce Viewsプラグインを使用すると、Business Toolsアプリケーション自体に変更を加えることなく、Business Toolsユーザーインターフェイスの特定の側面をカスタマイズできます。たとえば、既存のビューに新しいビュー プロパティを追加するカスタム プラグインを作成できます。または、Business Tools UIから要素 (子ビューやプロパティなど) を削除する必要がある場合があります。これらのタイプのカスタマイズは、適切なCommerce Engineプラグインを拡張することで実現できます。

メモ

Business Toolsのユーザー インターフェイスを使用して 、カスタムの子ビュー とプロパティを追加または削除することもできますが、既定の子ビューとプロパティを削除することはできません。

Commerceビューをカスタマイズするには (たとえば、カスタムの子ビューやプロパティを追加または削除するなど)、カスタム パイプライン ブロックを作成し、変更したデフォルト ブロックの後に実行するように設定する必要があります。次の例では、デフォルトの「顧客権利」ビュー (entitlementView) を拡張して、ギフトカードのコードを新しいプロパティとして表示します。新しいパイプライン ブロックの名前はGetCustomerGiftCardCodeViewBlockです。

Commerceビューをカスタマイズして情報を表示または非表示にするには:

  1. カスタムプラグインとブロックを作成します。拡張機能には、Sitecore.Commerce.Plugin.GiftCardsプラグインへの参照が含まれている必要があります。これは、カスタマイズの意図がギフトカードデータを取得することであるためです。

  2. 新しいGetCustomerGiftCardCodeViewBlockブロックは、EntityViewをパラメーターとして受け取り、EntityViewを返す必要があります。

    public class GetCustomerGiftCardCodeViewBlock : PipelineBlock<EntityView, EntityView, CommercePipelineExecutionContext>
  3. runメソッドをカスタム ブロックに追加します。カスタム ブロックは、最初に、変更するCommerceエンティティ ビューを識別する条件を指定する必要があります。

    以下は、カスタムブロックを示しています。

        {
            private readonly FindEntityCommand _findEntityCommand;
    
            public GetCustomerGiftCardCodeViewBlock(FindEntityCommand command)
            {
                _findEntityCommand = command;
            }
            public override async Task<EntityView> RunAsync(EntityView arg, CommercePipelineExecutionContext context)
            {
                Condition.Requires(arg).IsNotNull($"{this.Name}: The argument cannot be null.");
                var request = context.CommerceContext.GetObject<EntityViewArgument>();
    
                // Do nothing if:
                // -the EntityViewArgument object cannot be found in the context
                // -or the name of the requested view is not Commented 
                // -or the requested entity is not a Customer
                if (string.IsNullOrEmpty(request?.ViewName) ||
                    (!request.ViewName.Equals(context.GetPolicy<KnownEntitlementsViewsPolicy>().CustomerEntitlements, StringComparison.OrdinalIgnoreCase)
                        && !request.ViewName.Equals(context.GetPolicy<KnownCustomerViewsPolicy>().Master, StringComparison.OrdinalIgnoreCase))
                    || !(request.Entity is Customer))
                {
                    return arg;
                }
    
                var customer = (Customer)request.Entity;
    
                // Check if the Customer has entitlements
                if (!customer.HasComponent<EntitlementsComponent>())
                {
                    return arg;
                }
    
                // Find all the entitlements view that were already added by another block
                var entitlementsView = request.ViewName.Equals(context.GetPolicy<KnownCustomerViewsPolicy>().Master, StringComparison.OrdinalIgnoreCase) ? arg.ChildViews.OfType<EntityView>().FirstOrDefault(ev => ev.Name.Equals(context.GetPolicy<KnownEntitlementsViewsPolicy>().CustomerEntitlements, StringComparison.OrdinalIgnoreCase)) : arg;
                var entitlementViews = entitlementsView?.ChildViews.Where(cv => cv.Name.Equals(context.GetPolicy<KnownEntitlementsViewsPolicy>().CustomerEntitlementDetails, StringComparison.OrdinalIgnoreCase)).OfType<EntityView>().ToList();
                if (entitlementViews == null)
                {
                    return arg;
                }
    
                foreach (var entitlementView in entitlementViews)
                {
                    var entitlement = await _findEntityCommand.Process(context.CommerceContext, typeof(Entitlement), entitlementView.ItemId).ConfigureAwait(false);
                    if (entitlement == null)
                    {
                        continue;
                    }
    
                    var card = entitlement as GiftCard;
                    if (card == null)
                    {
                        continue;
                    }
    
                    entitlementView.Properties.Add(new ViewProperty { Name = "Code", IsReadOnly = true, RawValue = card.GiftCardCode });
                }
    
                return arg;
            }
        }
    }
  4. カスタム パイプラインとブロックをCommerce Engineに登録します。カスタム プラグイン プロジェクトで、ConfigureSitecore.csファイルを開き、カスタムGetCustomerGiftCardCodeViewBlockプラグイン ブロックをIGetEntityViewPipelineに追加し、Sitecore.Commerce.Plugin.GiftCardsプラグイン ブロックGetCustomerGiftCardEntitlementDetailsViewBlockを追加します。

  5. Sitecore XCソリューションをデプロイします

カスタム子ビューの削除

カスタムCommerceエンティティを削除するには:

  • 次の例に示すように、RemoveChildViewメソッドを使用します。

     public static void RemoveChildView(EntityView entityView, string viewNameToRemove)
            {
                // Remove a child view
                var viewToRemove = entityView.ChildViews.FirstOrDefault(p => p.Name.Equals(viewNameToRemove, StringComparison.OrdinalIgnoreCase)) as EntityView;
                entityView.ChildViews.Remove(viewToRemove);
            }

カスタムビュープロパティの削除

カスタムコマースビュープロパティを削除するには:

  • 次の例に示すように、RemoveViewPropertyFromViewメソッドを使用します。

     public static void RemoveViewPropertyFromView(EntityView entityView, string viewPropertyName)
            {
                // Remove a property
                var propertyToRemove = entityView.Properties.FirstOrDefault(p => p.Name.Equals(viewPropertyName, StringComparison.OrdinalIgnoreCase));
                entityView.Properties.Remove(propertyToRemove);
            }
この記事を改善するための提案がある場合は、 お知らせください!