1

我的问题是,当我尝试将产品添加到购物车时,使用 Sitecore SES 时找不到产品:

IShoppingCartManager shoppingCartManager = Context.Entity.Resolve<IShoppingCartManager>();
shoppingCartManager.AddProduct(productCode, q);

我确信我做了以下事情:我已经将索引“产品”添加到 Lucene。我还在业务目录中配置了“产品链接”。我还在每个网站的配置中添加了 ordersDatabase="orders" actionLogDatabase="logging"。我没有安装«电子商务示例»。你能帮我解决这个问题吗?如果您需要更多说明,我准备好了。

4

1 回答 1

0

我可以给你一些我知道我曾经工作过的代码。希望这将帮助您朝着正确的方向前进。希望这可以帮助!

丽兹

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);
    }
}
}
于 2014-04-30T19:31:09.063 回答