Kentico 9 购物车 ECommerceContext 数量在宏和布局/.NET 之间不一致

Kentico 9 shopping cart ECommerceContext amounts insconsistent between macro and layout/.NET

我有一个 Kentico 9 电子商务网站。在结帐过程中的一个页面上,我有一个静态 HTML Web 部件。其内容字段包含:

<ul class="cart-total-list text-center mb0">
    <li style="text-align: left;"><span>Products</span><span>{% FormatPrice(ECommerceContext.CurrentShoppingCart.TotalItemsPrice - ECommerceContext.CurrentShoppingCart.TotalItemsTax) #%}</span></li>
    <li style="text-align: left;"><span>Tax</span><span>{% FormatPrice(ECommerceContext.CurrentShoppingCart.TotalTax) #%}</span></li>
    <li style="text-align: left;"><span>Shipping</span><span>{% FormatPrice(ECommerceContext.CurrentShoppingCart.Shipping) #%}%}</span></li>
    <li style="text-align: left;"><span>Shipping Tax</span><span>{% FormatPrice( ECommerceContext.CurrentShoppingCart.TotalShipping - ECommerceContext.CurrentShoppingCart.Shipping) #%}</span></li>
    <li style="text-align: left;"><span>Total</span><span>{% FormatPrice(ECommerceContext.CurrentShoppingCart.TotalPrice) #%}</span></li>
</ul>

例如,产生以下输出:

Products .30
Tax .30
Shipping [=11=]
Shipping Tax .19
Total .49

这是不正确的。产品总额应为 54 美元(不知何故包含 4.30 美元的税。) 运费应该是 15 美元。 运费应为 1.19 美元。 (因为运费为零,所以显示全额。) 此外,有时税金显示为零,但包含在我的产品行中的金额中。

现在,如果我改为在 Web 部件的布局中使用 C# 呈现这些值,如下所示:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="CMSWebParts_Text_staticHTML"  Codebehind="~/CMSWebParts/Text/staticHTML.ascx.cs" %>
<%
  var cart = CMS.Ecommerce.ECommerceContext.CurrentShoppingCart;
%>
<asp:Literal ID="ltlText" runat="server" EnableViewState="false" />
<ul class="cart-total-list text-center mb0">
    <li style="text-align: left;"><span>Products</span><span><% Response.Write(cart.GetFormattedPrice(cart.TotalItemsPrice - cart.TotalItemsTax,false)); %></span></li>
    <li style="text-align: left;"><span>Tax</span><span><% Response.Write(cart.GetFormattedPrice(cart.TotalTax,false)); %></span></li>
    <li style="text-align: left;"><span>Shipping</span><span><% Response.Write(cart.GetFormattedPrice(cart.Shipping,false)); %></span></li>
    <li style="text-align: left;"><span>Shipping Tax</span><span><% Response.Write(cart.GetFormattedPrice(cart-TotalShipping - cart.Shipping,false)); %></span></li>
    <li style="text-align: left;"><span>Total</span><span><% Response.Write(cart.GetFormattedPrice(cart.TotalPrice,false)); %></span></li>
</ul>

我得到以下值:

Products .00
Tax .30
Shipping .00
Shipping Tax .19
Total .49

这些正是我所期望的。

为什么不同?我当然可以坚持使用后一种方法,但我担心有些东西坏了,可能会有未来的副作用。


更新: 我使用 custom macro field 而不是自定义宏方法。我刚刚将以下 class 添加到我的 old_app_code 文件夹中。 (因为我有一个预编译的 Web 应用程序)它似乎按预期工作。

using System;
using CMS.Base;
using CMS.MacroEngine;
using CMS.EventLog;

[MacroLoader]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Attribute class for registering custom macro extensions.
    /// </summary>
    private class MacroLoaderAttribute : CMSLoaderAttribute
    {
        private const string EVENT_SOURCE = "MacroLoaderAttribute";
        private const string EVENT_CODE = "EXCEPTION";

        /// <summary>
        /// Called automatically when the application starts.
        /// </summary>
        public override void Init()
        {
            MacroContext.GlobalResolver.SetNamedSourceDataCallback("CartShipping", CartShipping);
            MacroContext.GlobalResolver.SetNamedSourceDataCallback("CartTotalItemsTax", CartTotalItemsTax);
        }

        private object CartShipping(EvaluationContext context)
        {
            double retVal = 0d;
            try
            {
                retVal = CMS.Ecommerce.ECommerceContext.CurrentShoppingCart.Shipping;
            }
            catch (Exception ex)
            {
                EventLogProvider.LogException(EVENT_SOURCE, EVENT_CODE, ex);
            }
            return retVal;
        }
        public static object CartTotalItemsTax(EvaluationContext context)
        {
            double retVal = 0d;
            try
            {
                retVal = CMS.Ecommerce.ECommerceContext.CurrentShoppingCart.TotalItemsTax;
            }
            catch (Exception ex)
            {
                EventLogProvider.LogException(EVENT_SOURCE, EVENT_CODE, ex);
            }
            return retVal;
        }
    }
}

现在 CartShipping 和 CartTotalItemsTax 可直接用于宏表达式。

并非 CurrentShoppingCart 项目的所有属性都存在于 K# 宏对象中。例如,TotalItemsTax 不存在,因此它是 returns 0。与航运相同 属性。这就是为什么你在那里有差异。您可以在系统应用程序 -> 宏 -> 控制台中看到的所有可用属性

我可以确认 TotalItemsTaxShipping 属性没有为 ShoppingCartInfo 中的宏注册对象,因此宏引擎将为它们 return 0。这会导致宏中的计算不正确,而在标准 ascx 代码中它可以工作。
为宏额外注册系统对象类型属性并不容易,因此如果您想使用 K# 宏,我建议您创建一个自定义宏方法(参见 https://docs.kentico.com/k9/macro-expressions/extending-the-macro-engine/registering-custom-macro-methods),您可以在其中调用C# 中的标准 API(例如 ECommerceContext.CurrentShoppingCart 的属性)具有所有可用属性。您甚至可以直接 return 结果值(从总额中减去税收),而不是在标记或转换中进行。

希望这对您有所帮助...