Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Stuart Hill 2 posts 25 karma points
    Apr 23, 2013 @ 17:35
    Stuart Hill
    0

    Unable to clear basket

    Our users can select an option "Clear Basket" to remove everything in their basket. Up until now we have been updating the line items with a zero quantity:

    TransactionLibrary.UpdateLineItem(orderLineId, quantity);  

     and then executing the basket pipeline.

    However, this is insufficient as, for example, discounts remain in the basket. Reading other posts I have seen that the advice is to call ClearBasket or ClearBasketInformation. These calls have not cleared the basket for me.

    UCommerce.Xslt.Library.ClearBasket();

    What we are trying to achieve is an empty basket - can anyone give me the .Net code to do this? We are using Umbraco 4.11.4 and UCommerce 3.5.0. Thanks.

     

     

     

  • Martin 181 posts 740 karma points
    Apr 23, 2013 @ 19:26
    Martin
    0

    Hi Stuart,

    There are several ways to do this. One way is to clear the cookie which contains the uCommerce basketid. This way a user will have a whole new basket. Though this can overtime be problematic due to a basket will remain in database.

    If you get the basket with .NET code (or razor) you can on the basket say:

    basket.PurchaseOrder.Discounts.Clear();

    You can also clear other information that suits your purpose. 

    Regards Martin

  • Stuart Hill 2 posts 25 karma points
    Apr 25, 2013 @ 11:18
    Stuart Hill
    3

    Thanks Martin. Some more investigation has revealed that this is more complex than we first thought. Now that we understand more about the problem we have made a new post here

    And here's how we have ended up clearing our basket of order lines and vouchers:

            public void ClearBasket()
            {
                // Get rid of the lines
                foreach (var orderLineId in PurchaseOrder.OrderLines.Select(x => x.OrderLineId).ToList())
                {
                   UpdateLineItem(orderLineId, 0);
                }

                // Remove all the discounts
                while (PurchaseOrder.Discounts.Any())
                {
                    PurchaseOrder.RemoveDiscount(PurchaseOrder.Discounts.FirstOrDefault());
                }

                // Vouchers are also represented as properties, and if not removed from properties they
    // will be re-generated on the order via the pipeline
                var prop = PurchaseOrder.OrderProperties.FirstOrDefault(v => v.Key == "voucherCodes");
                if (prop != null)
                {
                    prop.Value = string.Empty;
                    prop.Save();
                }

                PurchaseOrder.Save();     
               
                ExecuteBasketPipeline();
            }

     

     

  • Rob Scott 41 posts 94 karma points
    Aug 08, 2015 @ 16:36
    Rob Scott
    0

    Nice job on this - I was not understanding why vouchers were not being cleared from the basket once I called TransactionLibrary.ClearBasket(). I modified the code a little, this reflects v6.6

    Basically only removed the code for order lines, since calling ClearBasket() does this for you.

        /// <summary>
        /// Clears the user's current cart (basket)
        /// </summary>
        [HttpPost]
        public void ClearBasket()
        {
            try
            {
                var basket = TransactionLibrary.GetBasket().PurchaseOrder;
    
                // remove all the discounts
                basket.Discounts.ToList().ForEach(x => basket.RemoveDiscount(x));
    
                // vouchers are also represented as properties, and if not removed from properties they 
                // will be re-generated on the order via the pipeline
                var prop = basket.OrderProperties.FirstOrDefault(v => v.Key == "voucherCodes");
                if (prop != null)
                {
                    prop.Value = string.Empty;
                    prop.Save();
                }
    
                basket.Save();
    
                TransactionLibrary.ExecuteBasketPipeline();
                TransactionLibrary.ClearBasket();
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
        }
    
  • Kim Søjborg Pedersen 71 posts 275 karma points
    Dec 02, 2014 @ 16:35
    Kim SĂžjborg Pedersen
    0

    Hi Stuart, thanks for posting the solution that's really awesome :)

  • Mads Due 9 posts 29 karma points
    Dec 04, 2014 @ 14:39
    Mads Due
    0

    Hi Stuart,


    Great that you found the right solution on your question and for sharing the solution.

    #h5yr


    Regards Mads Due 

Please Sign in or register to post replies

Write your reply to:

Draft