Sitecore SES issue:Unable to add product to the shopping cart

307 Views Asked by At

My problem is that with Sitecore SES when I try to add a product to the shopping Cart then It does not find the product:

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

I am sure I did the following: I already added the index "product" to Lucene. Also I configured the "Products Link" in the Business Catalog. I also added ordersDatabase="orders" actionLogDatabase="logging" to config for every website. I didn't installed the « E-Commerce Examples » though. can you help me with this issue please? If you need more clarification I am ready.

1

There are 1 best solutions below

0
On

I can give you some code that I know I had working at one point. Hopefully this will help you go in the right direction. Hope this helps!

Liz

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);
    }
}
}