【发布时间】: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