Remove couponcode from cart

Vote:
 

I want to give the visitors the ability to remove a voucher/coupon code from the cart if they added this before.

Codes are added by adding them to the MarketingContext.Current.MarketingProfileContext, but I can't find the codes there to remove them, and when I add a new codeslist with another code the CalculateDiscountsActivity still has the old code in the MarketingProfileContext too.

Where are these codes stored? In the Session somewhere, or via a cookie? or somewhere else?

 

#80454
Jan 23, 2014 10:18
Vote:
 

Hello,

Here's the code from Commerce sample (lvDiscount_ItemCommand in ApplyCoupons.ascx.cs). After apply, the coupon was stored as discount in lineitem/order, then it must be deleted. Note the last line, which just remove the coupon code from session, which does not affect the order:

 

if (!e.CommandName.Equals("DeleteCoupon"))
return;

var couponCode = e.CommandArgument.ToString();

foreach (OrderForm form in _cartHelper.Cart.OrderForms)
{
var formDiscount = form.Discounts.Cast<Discount>().Where(x => couponCode.Equals(x.DiscountCode));
foreach (var discount in formDiscount)
{
discount.Delete();
}

foreach (LineItem lineItem in form.LineItems)
{
var lineItemDiscount = lineItem.Discounts.Cast<Discount>().Where(x => couponCode.Equals(x.DiscountCode));
foreach (var discount in lineItemDiscount)
{
discount.Delete();
}
}

foreach (Shipment shipment in form.Shipments)
{
var shipmentDiscount = shipment.Discounts.Cast<Discount>().Where(x => couponCode.Equals(x.DiscountCode));
foreach (var discount in shipmentDiscount)
{
shipment.ShippingDiscountAmount -= discount.DiscountValue;
// Pending testing, this might need to be changed to summing ShippingDiscountAmount before removing them,
// and then adding that sum back to form.ShippingTotal (since CalculateTotalsActivity simply does
// shipment.ShipmentTotal - shipment.ShippingDiscountAmount without checking for custom discounts)
form.ShippingTotal += discount.DiscountValue;

discount.Delete();
}
}
}
CartHelper.RunWorkflow(Constants.CartValidateWorkflowName);
_cartHelper.Cart.AcceptChanges();

Session.Remove(Constants.LastCouponCode);

Regards.

/Q

#80461
Jan 23, 2014 10:50
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.