【问题标题】:Kentico 9 shopping cart ECommerceContext amounts insconsistent between macro and layout/.NETKentico 9 购物车 ECommerceContext 数量在宏和布局/.NET 之间不一致
【发布时间】:2017-01-11 06:20:08
【问题描述】:

我有一个 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 $58.30
Tax $4.30
Shipping $0
Shipping Tax $16.19
Total $74.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 $54.00
Tax $4.30
Shipping $15.00
Shipping Tax $1.19
Total $74.49

这些正是我所期望的。

为什么会有差异?我当然可以坚持后一种方法,但我担心某些东西会损坏并且可能会产生未来的副作用。


更新: 我使用了custom macro field 而不是自定义宏方法。我刚刚将以下类添加到我的 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 可直接用于宏表达式。

【问题讨论】:

    标签: asp.net layout macros cart kentico


    【解决方案1】:

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

    【讨论】:

    • 谢谢!这是一个有用的答案,但我选择了 Zdenek 的答案,因为他指导我找到一种解决方案,使数据可用于宏。
    【解决方案2】:

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

    希望这会有所帮助...

    【讨论】:

    • 我使用了 custom macro field 而不是自定义宏方法。它似乎可以按需要工作。
    猜你喜欢
    • 2011-03-15
    • 2018-12-17
    • 2012-07-22
    • 2014-10-24
    • 2019-08-15
    • 1970-01-01
    • 2021-03-17
    • 2021-04-19
    • 2015-06-01
    相关资源
    最近更新 更多