Ecommerce Checkout Issue

Posted by Community Admin on 05-Aug-2018 17:46

Ecommerce Checkout Issue

All Replies

Posted by Community Admin on 05-Sep-2014 00:00


Having a problem with checkout returning this error.
 ----------------------------------------

Timestamp: 9/5/2014 3:37:22 PM



Message: System.Reflection.TargetInvocationException: Exception has been
thrown by the target of an invocation. --->
System.InvalidOperationException: Query execution found more than one
element.

   at
Telerik.OpenAccess.Query.ExpressionExecution.PerformQuerySingle[T,TResult](ExpressionCutter
cutter, MethodCallExpression mce, ChainedContext piece, QueryOptions
options)

   at Telerik.OpenAccess.Query.Piece`1.ExecuteSingle[TResult](Expression expression)

   --- End of inner exception stack trace ---

   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)

   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)

   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture)

   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)

   at Telerik.Sitefinity.Data.Linq.OpenAccess.OpenAccessQueryProvider`2.Execute(Expression expression)

   at
Telerik.Sitefinity.Data.Linq.OpenAccess.OpenAccessQueryProvider`2.System.Linq.IQueryProvider.Execute[TResult](Expression
expression)

   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)

   at Telerik.Sitefinity.Modules.Ecommerce.Orders.Web.UI.CheckoutViews.Preview.Checkout()



Category: ErrorLog



Priority: -1



EventId: 1



Severity: Information



Title:



Machine: HFWINWEB02



App Domain: /LM/W3SVC/1/ROOT-1-130539467060220794



ProcessId: 1616



Process Name: c:\windows\system32\inetsrv\w3wp.exe



Thread Name:



Win32 ThreadId:3408



Extended Properties:

----------------------------------------
 We believe this is occurring because we created a custom control to
add items to the cart.  If we use the standard cart items and checkout
the error does not occur.  Here is the code we are using for the custom
add to cart.

 OrdersManager orderManager = OrdersManager.GetManager();

        CatalogManager catManager = CatalogManager.GetManager();

        CartOrder shoppingCart = this.TryGetShoppingCartForUser(orderManager);

        Product liveProduct = catManager.GetProduct(selectedProductID, ContentLifecycleStatus.Live);



        if (shoppingCart == null)

       

            shoppingCart = orderManager.CreateCartOrder();



            HttpCookie shoppingCartCookie = new
HttpCookie(EcommerceConstants.OrdersConstants.ShoppingCartIdCookieName);

            shoppingCartCookie.Value = shoppingCart.Id.ToString();

            shoppingCartCookie.Expires = DateTime.Now.AddMonths(1);



            HttpContext.Current.Response.Cookies.Add(shoppingCartCookie);

       



        shoppingCart.Currency = Config.Get<EcommerceConfig>().DefaultCurrency;



        CartDetail cartDetail = shoppingCart.Details.Where(d => d.ProductId == liveProduct.Id).SingleOrDefault();



        if (cartDetail != null)

       

            orderManager.SetQuantity(cartDetail, cartDetail.Quantity + 1);

       

        else

       

            OptionsDetails optionDetails = new OptionsDetails();

            orderManager.AddToCart(shoppingCart, liveProduct, optionDetails, 1);

       



        orderManager.SaveChanges();
 The selectedProductID represents the proper sku for the selected
product.  Everything adds to the cart without error and you can go
through the entire checkout process but the moment you select place
order on the preview page this error occurs.  Any thoughts on what is
wrong here??


Posted by Community Admin on 10-Sep-2014 00:00

This solution worked for me.  Hopefully this will help someone else who stumbles on this issue.

 public bool AddToCart(string sku, int qty)
   
        OrdersManager OrdersMgr = OrdersManager.GetManager();
        CatalogManager CatalogMgr = CatalogManager.GetManager();

        if (string.IsNullOrWhiteSpace(sku) || qty < 0) return false;

        /* Get Product */

        //var product = CatalogManager.GetProducts().FirstOrDefault(c => c.Sku == sku);
        var product = CatalogMgr.GetProduct(sku, ContentLifecycleStatus.Live);

        /* Add to cart */

        if (product != null)
       
            // Get CartOrder
            var cartOrder = GetCartOrder();

            // If still null create cart
            if (cartOrder == null)
           
                // Create and save order
                cartOrder = OrdersMgr.CreateCartOrder();

                // Increment order number
                var on = OrdersMgr.GetNextOrderNumber();
                cartOrder.OrderNumber = on.Next;
                OrdersMgr.IncrementNextOrderNumber();

                // Save
                OrdersMgr.SaveChanges();

                // Save new order to session
                SetCartIdInSession(cartOrder.Id);

                // Save new order to cookie
                SetCartIdInCookie(cartOrder.Id);
           

            IProductImagePopulator ImgPopulator = new ProductImagePopulator();
            IProductVatTaxPopulator VatTaxPopulator = new ProductVatTaxPopulator();

            // Populate Images and Tax
            ImgPopulator.SetProductImages(product);
            VatTaxPopulator.SetProductVatTax(product);

            if (cartOrder.Details.Any(c => c.ProductId == product.Id)) // existing item in cart, bump qty
           
                var detail = cartOrder.Details.First(c => c.ProductId == product.Id);
                OrdersMgr.IncreaseQuantity(detail.Id, qty);
           
            else // no existing item in cart, addtocart
           
                // Create OptionsDetails - we're not handling variations at this point
                // Note - if you populate SKU or TotalDetailPrice it will concat the default product values
                var options = new OptionsDetails();

                OrdersMgr.AddToCart(cartOrder, product, options, qty);
           

            // Populate CartOrder/Details info
            var calc = new EcommerceOrderCalculator();
            calc.CalculateAndSaveChanges(cartOrder);

            // Final Save
            OrdersMgr.SaveChanges();

            return true;
       

        return false;
   

    public CartOrder GetCartOrder()
   
        CartOrder r = null;

        var mgr = OrdersManager.GetManager();

        // Get cart by cookie
        var cookieId = GetCartIdFromCookie();
        if (cookieId != null && cookieId != Guid.Empty)
       
            var exists = mgr.GetCartOrders().Any(c => c.Id == cookieId);
            if (exists) r = mgr.TryGetCartOrder((Guid)cookieId);
       

        return r;
   

    public static Guid? GetCartIdFromSession()
   
        var session = HttpContext.Current.Session["cartOrderId"];
        if (session != null)
       
            var r = Guid.Empty;
            Guid.TryParse(session.ToString(), out r);
            if (r != Guid.Empty) return r;
       

        return null;
   

    public static Guid? GetCartIdFromCookie()
   
        var cookie = HttpContext.Current.Request.Cookies["shoppingCartId"];
        if (cookie != null)
       
            var r = Guid.Empty;
            Guid.TryParse(cookie.Value, out r);
            if (r != Guid.Empty) return r;
       

        return null;
   

    public static void SetCartIdInSession(Guid id)
   
        HttpContext.Current.Session["cartOrderId"] = id;
   

    public static void SetCartIdInCookie(Guid id)
   
        var _id = id.ToString();

        // Remove any existing cookie
        RemoveCartCookie(id);

        var cookie = HttpContext.Current.Response.Cookies["shoppingCartId"];
        if (cookie == null)
       
            cookie = new HttpCookie("shoppingCartId") Expires = DateTime.Now.AddMonths(6) ;

            cookie.Value = _id;

            HttpContext.Current.Response.Cookies.Add(cookie);
       
        else
       
            cookie.Value = _id;
       
   

    public static void RemoveCartIdFromSession(Guid id)
   
        HttpContext.Current.Session["cartOrderId"] = null;
   

    public static void RemoveCartCookie(Guid id)
   
        // Remove any existing request cookie
        if (HttpContext.Current.Request.Cookies["shoppingCartId"] != null)
       
            HttpContext.Current.Request.Cookies.Remove("shoppingCartId");
       

        // Remove any existing response cookie
        if (HttpContext.Current.Response.Cookies["shoppingCartId"] != null)
       
            HttpContext.Current.Response.Cookies.Remove("shoppingCartId");
       
   

Posted by Community Admin on 10-Sep-2014 00:00

Hi,

Thank you for sharing the solution with the community.

Regards,
Atanas Valchev
Telerik

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed