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:
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.
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.
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(); }
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;
}
}
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:
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.
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.
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
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:
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.6Basically only removed the code for order lines, since calling ClearBasket() does this for you.
Hi Stuart, thanks for posting the solution that's really awesome :)
Hi Stuart,
Great that you found the right solution on your question and for sharing the solution.
#h5yr
Regards Mads Due
is working on a reply...