我可以给你一些我知道我曾经工作过的代码。希望这将帮助您朝着正确的方向前进。希望这可以帮助!
丽兹
using Microsoft.Practices.Unity;
using Sitecore.Analytics;
using Sitecore.Diagnostics;
using Sitecore.Ecommerce;
using Sitecore.Ecommerce.Analytics.Components;
using Sitecore.Ecommerce.DomainModel.Carts;
using System;
using System.Collections.Generic;
using System.Linq;
namespace sharedsource_verndale._Classes.Helpers
{
public class ShoppingCartWebHelper
{
public static void AddToShoppingCart(string productCode, string quantity)
{
Assert.ArgumentNotNullOrEmpty(productCode, "productCode");
Assert.ArgumentNotNullOrEmpty(quantity, "quantity");
IShoppingCartManager shoppingCartManager = IoCContainerExtensions.Resolve<IShoppingCartManager>(Context.Entity, new ResolverOverride[0]);
uint result;
if (string.IsNullOrEmpty(quantity) || !uint.TryParse(quantity, out result))
shoppingCartManager.AddProduct(productCode, 1U);
else
shoppingCartManager.AddProduct(productCode, result);
ShoppingCartLine shoppingCartLine = Enumerable.FirstOrDefault<ShoppingCartLine>((IEnumerable<ShoppingCartLine>)Context.Entity.GetInstance<ShoppingCart>().ShoppingCartLines, (Func<ShoppingCartLine, bool>)(p => p.Product.Code.Equals(productCode)));
try
{
Tracker.StartTracking();
AnalyticsUtil.AddToShoppingCart(shoppingCartLine.Product.Code, shoppingCartLine.Product.Title, 1U, shoppingCartLine.Totals.PriceExVat);
}
catch (Exception ex)
{
ShoppingCartWebHelper.LogException(ex);
}
}
public static void UpdateInShoppingCart(string productCode, string quantity)
{
Assert.ArgumentNotNullOrEmpty(productCode, "productCode");
Assert.ArgumentNotNullOrEmpty(quantity, "quantity");
IShoppingCartManager shoppingCartManager = IoCContainerExtensions.Resolve<IShoppingCartManager>(Context.Entity, new ResolverOverride[0]);
uint result;
if (string.IsNullOrEmpty(quantity) || !uint.TryParse(quantity, out result))
shoppingCartManager.UpdateProductQuantity(productCode, 1U);
else
shoppingCartManager.UpdateProductQuantity(productCode, result);
ShoppingCartLine shoppingCartLine = Enumerable.FirstOrDefault<ShoppingCartLine>((IEnumerable<ShoppingCartLine>)Context.Entity.GetInstance<ShoppingCart>().ShoppingCartLines, (Func<ShoppingCartLine, bool>)(p => p.Product.Code.Equals(productCode)));
try
{
Tracker.StartTracking();
AnalyticsUtil.ShoppingCartItemUpdated(shoppingCartLine.Product.Code, shoppingCartLine.Product.Title, 1U);
}
catch (Exception ex)
{
ShoppingCartWebHelper.LogException(ex);
}
}
public static void DeleteFromShoppingCart(string productCode)
{
Assert.ArgumentNotNullOrEmpty(productCode, "productCode");
ShoppingCartLine shoppingCartLine = Enumerable.FirstOrDefault<ShoppingCartLine>((IEnumerable<ShoppingCartLine>)Context.Entity.GetInstance<ShoppingCart>().ShoppingCartLines, (Func<ShoppingCartLine, bool>)(p => p.Product.Code.Equals(productCode)));
try
{
if (shoppingCartLine != null)
{
Tracker.StartTracking();
AnalyticsUtil.ShoppingCartProductRemoved(shoppingCartLine.Product.Code, shoppingCartLine.Product.Title, shoppingCartLine.Quantity);
}
}
catch (Exception ex)
{
ShoppingCartWebHelper.LogException(ex);
}
IoCContainerExtensions.Resolve<IShoppingCartManager>(Context.Entity, new ResolverOverride[0]).RemoveProduct(productCode);
}
public static void DeleteProductLineFromShoppingCart(string productCode)
{
Assert.ArgumentNotNullOrEmpty(productCode, "productCode");
ShoppingCartLine shoppingCartLine = Enumerable.FirstOrDefault<ShoppingCartLine>((IEnumerable<ShoppingCartLine>)Context.Entity.GetInstance<ShoppingCart>().ShoppingCartLines, (Func<ShoppingCartLine, bool>)(p => p.Product.Code.Equals(productCode)));
if (shoppingCartLine != null)
{
try
{
Tracker.StartTracking();
AnalyticsUtil.ShoppingCartItemRemoved(shoppingCartLine.Product.Code, shoppingCartLine.Product.Title, shoppingCartLine.Quantity);
}
catch (Exception ex)
{
ShoppingCartWebHelper.LogException(ex);
}
}
IoCContainerExtensions.Resolve<IShoppingCartManager>(Context.Entity, new ResolverOverride[0]).RemoveProductLine(productCode);
}
private static void LogException(Exception ex)
{
Log.Error("Analytics error:", (object)ex);
}
}
}