如何检查 Context.Item 是 uCommerce 项目还是 Sitecore 项目

How to check if Context.Item is uCommerce item or Sitecore item

我正在为 Sitecore-uCommerce 解决方案开发语言切换,并使用默认的 uCommerce 项目解析器和 UrlService 来获得不错的 URLs。所以我有像 http://sitename/catalogname/productname/c-XX/p-YY.

这样的 URL

然后对于每个 sitecore 'page' 项目,我使用 LinkManager.GetItemUrl(Context.Item, 选项) 和 LanguageEmbedding.Always 选项。

现在,如果我的 Context.Item 是一个 uCommerce 项目,那么我的 URL 是 http://sitename/nl/sitecore/ucommerce/products/3/34/productname/,这将不会被 uCommerce Item Resolver 捕获并且我将找不到文档错误。

所以我需要先检查 Context.Item 是否是 uCommerce 项目并使用 URLService 而不是 LinkManager。

问题:uCommerce中有没有类似Sitecore.TemplateIDs的class模板ID?这样我就可以检查我的商品是否来自基本 ucommerce 产品模板。

相关问题:如何在 uCommerce nice URLs 中嵌入语言?

这是 HttpRequestBegin 管道

<httpRequestBegin>
    <processor type="Sitecore.Pipelines.PreprocessRequest.CheckIgnoreFlag, Sitecore.Kernel"/>
    <processor type="Sitecore.Pipelines.HttpRequest.EnsureServerUrl, Sitecore.Kernel"/>
    <processor type="Sitecore.Pipelines.HttpRequest.StartMeasurements, Sitecore.Kernel"/>
    <processor type="Sitecore.Analytics.Pipelines.HttpRequest.StartDiagnostics,Sitecore.Analytics" patch:source="Sitecore.Analytics.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.IgnoreList, Sitecore.Kernel"/>
    <!--
         Ensure registered bundles are ignored by http request pipeline 
        -->
    <processor type="Sitecore.FXM.Pipelines.HttpRequest.IgnoreBundleEntries, Sitecore.FXM" patch:source="Sitecore.FXM.Bundle.config"/>
    <!--  Register bundles on their first request  -->
    <processor type="Sitecore.FXM.Pipelines.HttpRequest.OnRequestBundleRegistrarProcessor, Sitecore.FXM" patch:source="Sitecore.FXM.Bundle.config">
        <bundles hint="list:AddBundle">
            <!--
         The FXM client javascript bundle - see registration pipeline below.  It requires the current Sitecore instance host name
                         when creating the bundle, so must be generated as part of a Sitecore request. 
        -->
            <bundle>beacon</bundle>
        </bundles>
    </processor>
    <processor type="Sitecore.Mvc.Pipelines.HttpRequest.DisplayFatalErrors, Sitecore.Mvc" patch:source="Sitecore.Mvc.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.SiteResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.ItemWebApi.Pipelines.HttpRequest.SetRuntimeSettings, Sitecore.ItemWebApi" patch:source="Sitecore.ItemWebApi.config"/>
    <processor type="Sitecore.ItemWebApi.Pipelines.HttpRequest.CheckMode, Sitecore.ItemWebApi" patch:source="Sitecore.ItemWebApi.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.UserResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.ItemWebApi.Pipelines.HttpRequest.ResolveUserFromRequest, Sitecore.ItemWebApi" patch:source="Sitecore.ItemWebApi.config"/>
    <processor type="Sitecore.ExperienceExplorer.Business.Pipelines.HttpRequest.EnableExperienceModePipeline, Sitecore.ExperienceExplorer.Business" patch:source="Sitecore.ExperienceExplorer.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.DatabaseResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.Pipelines.HttpRequest.BeginDiagnostics, Sitecore.Kernel"/>
    <processor type="Sitecore.Pipelines.HttpRequest.DeviceResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.ExperienceExplorer.Business.Pipelines.HttpRequest.PresetResolverPipeline, Sitecore.ExperienceExplorer.Business" patch:source="Sitecore.ExperienceExplorer.config"/>
    <processor type="Sitecore.ExperienceExplorer.Business.Pipelines.HttpRequest.DevicePipeline, Sitecore.ExperienceExplorer.Business" patch:source="Sitecore.ExperienceExplorer.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.LanguageResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.ItemWebApi.Pipelines.HttpRequest.ResolveLanguage, Sitecore.ItemWebApi" patch:source="Sitecore.ItemWebApi.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.CustomHandlers, Sitecore.Kernel"/>
    <processor type="UCommerce.Sitecore.Pipelines.SetCurrentThreadCultureToCurrentLanguage, UCommerce.Sitecore" patch:source="Sitecore.uCommerce.Pipelines.HttpRequestBegin.config"/>
    <processor type="UCommerce.Sitecore.Pipelines.BreakHttpProcessor, UCommerce.Sitecore">
        <breakMatches hint="raw:AddBreakOnThisPattern" patch:source="Sitecore.uCommerce.Pipelines.HttpRequestBegin.config">
            <breakMatch text="/ucommerceapi"/>
            <breakMatch text="/paymentprocessor"/>
            <breakMatch text="/paymentrequest"/>
            <breakMatch text="/clientdependency"/>
        </breakMatches>
    </processor>
    <processor type="UCommerce.Sitecore.Pipelines.BreakAxdExtensionProcessor, UCommerce.Sitecore" patch:source="Sitecore.uCommerce.Pipelines.HttpRequestBegin.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.FilterUrlExtensions, Sitecore.Kernel">
        <param desc="Allowed extensions (comma separated)">aspx</param>
        <param desc="Blocked extensions (comma separated)">*</param>
        <param desc="Blocked extensions that stream files (comma separated)">css,js</param>
        <param desc="Blocked extensions that do not stream files (comma separated)">*</param>
    </processor>
    <processor type="Sitecore.ItemWebApi.Pipelines.HttpRequest.HandleActionRequest, Sitecore.ItemWebApi" patch:source="Sitecore.ItemWebApi.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.QueryStringResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.Pipelines.HttpRequest.DynamicLinkResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.Pipelines.HttpRequest.AliasResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.Pipelines.HttpRequest.DefaultResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.Pipelines.HttpRequest.FileResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.ExperienceExplorer.Business.Pipelines.HttpRequest.PreviewResolverPipeline, Sitecore.ExperienceExplorer.Business" patch:source="Sitecore.ExperienceExplorer.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel"/>
    <processor type="UCommerce.Sitecore.Pipelines.ItemResolver, UCommerce.Sitecore">
        <rewriteRules hint="raw:AddRules">
            <rule for="categoryProduct" match="(.*)/c-(?'productCatalog'[0-9]+)/c-(?'productCategory'[0-9]+)/p-(?'product'[0-9]+)"/>
            <rule for="product" match="(.*)/c-(?'productCatalog'[0-9]+)/p-(?'product'[0-9]+)"/>
            <rule for="category" match="(.*)/c-(?'productCatalog'[0-9]+)/c-(?'productCategory'[0-9]+)"/>
            <rule for="catalog" match="(.*)/c-(?'productCatalog'[0-9]+)"/>
        </rewriteRules>
    </processor>
    <processor type="Sitecore.Support.Pipelines.HttpRequest.PreviewSiteResolver, Sitecore.Support.321270.321271" patch:source="Sitecore.Support.321270.321271.config"/>
    <processor type="Sitecore.Mvc.DeviceSimulator.Pipelines.HttpRequest.HandleSimulationRequest, Sitecore.Mvc.DeviceSimulator" patch:source="Sitecore.MvcSimulator.config"/>
    <processor type="Sitecore.Mvc.Pipelines.HttpRequest.TransferRoutedRequest, Sitecore.Mvc" patch:source="Sitecore.Mvc.config"/>
    <processor type="Sitecore.ItemWebApi.Pipelines.HttpRequest.LaunchRequest, Sitecore.ItemWebApi" patch:source="Sitecore.ItemWebApi.config"/>
    <processor type="Sitecore.ExperienceExplorer.Business.Pipelines.HttpRequest.ContextItemPipeline, Sitecore.ExperienceExplorer.Business" patch:source="Sitecore.ExperienceExplorer.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.DeviceSimulatorResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.Pipelines.HttpRequest.LayoutResolver, Sitecore.Kernel"/>
    <processor type="Sitecore.Mvc.Pipelines.HttpRequest.TransferMvcLayout, Sitecore.Mvc" patch:source="Sitecore.Mvc.config"/>
    <processor type="Sitecore.Mvc.Pipelines.HttpRequest.TransferControllerRequest, Sitecore.Mvc" patch:source="Sitecore.Mvc.config"/>
    <processor type="Sitecore.ExperienceEditor.Pipelines.HttpRequest.CheckDevice, Sitecore.ExperienceEditor" patch:source="Sitecore.ExperienceEditor.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.PageEditorHandleNoLayout, Sitecore.ExperienceEditor" patch:source="Sitecore.ExperienceEditor.config"/>
    <processor type="Sitecore.ExperienceExplorer.Business.Pipelines.HttpRequest.ExecuteRequest, Sitecore.ExperienceExplorer.Business" patch:source="Sitecore.ExperienceExplorer.config"/>
    <processor type="Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel"/>
    <processor type="Sitecore.ContentTesting.Pipelines.HttpRequest.SetReadOnlySessionStateForScreenshot, Sitecore.ContentTesting" patch:source="Sitecore.ContentTesting.config"/>
</httpRequestBegin>

如果没有更好的解决方案,我是这样做的:

public static class TemplateIDs
{
    // sitecore/ucommerce item's template id       
    public static ID UCommerce => new ID("{AABC1CFA-9CDB-4AE5-8257-799D84A8EE23}");
}

public static class ItemExtensions
{
    public static bool IsUCommerceItem(this Item item)
    {
        var items = item.Axes.GetAncestors();
        return items.Any(x => x.TemplateID.Equals(TemplateIDs.UCommerce));
    }
}

//How to use
if(Sitecore.Context.Item.IsUCommerceItem()) 
{
    //Do your stuff 
}