/// <summary> /// Reset data required for checkout /// </summary> /// <param name="customer">Customer</param> /// <param name="storeId">Store identifier</param> /// <param name="clearCouponCodes">A value indicating whether to clear coupon code</param> /// <param name="clearCheckoutAttributes">A value indicating whether to clear selected checkout attributes</param> /// <param name="clearRewardPoints">A value indicating whether to clear "Use reward points" flag</param> /// <param name="clearShippingMethod">A value indicating whether to clear selected shipping method</param> /// <param name="clearPaymentMethod">A value indicating whether to clear selected payment method</param> public virtual void ResetCheckoutData(User customer, int storeId, bool clearCouponCodes = false, bool clearCheckoutAttributes = false, bool clearRewardPoints = true, bool clearShippingMethod = true, bool clearPaymentMethod = true) { if (customer == null) { throw new ArgumentNullException(); } //clear entered coupon codes if (clearCouponCodes) { _genericAttributeService.SaveAttribute <ShippingOption>(customer, SystemCustomerAttributeNames.DiscountCouponCode, null); _genericAttributeService.SaveAttribute <ShippingOption>(customer, SystemCustomerAttributeNames.GiftCardCouponCodes, null); } //clear checkout attributes if (clearCheckoutAttributes) { _genericAttributeService.SaveAttribute <ShippingOption>(customer, SystemCustomerAttributeNames.CheckoutAttributes, null, storeId); } //clear reward points flag if (clearRewardPoints) { _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.UseRewardPointsDuringCheckout, false, storeId); } //clear selected shipping method if (clearShippingMethod) { _genericAttributeService.SaveAttribute <ShippingOption>(customer, SystemCustomerAttributeNames.SelectedShippingOption, null, storeId); _genericAttributeService.SaveAttribute <ShippingOption>(customer, SystemCustomerAttributeNames.OfferedShippingOptions, null, storeId); _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.SelectedPickUpInStore, false, storeId); } //clear selected payment method if (clearPaymentMethod) { _genericAttributeService.SaveAttribute <string>(customer, SystemCustomerAttributeNames.SelectedPaymentMethod, null, storeId); } UpdateCustomer(customer); }
/// <summary> /// Adds a coupon code /// </summary> /// <param name="customer">Customer</param> /// <param name="couponCode">Coupon code</param> /// <returns>New coupon codes document</returns> public static void ApplyGiftCardCouponCode(this User customer, string couponCode, GenericAttributeDomianService genericAttributeService) { if (customer == null) { throw new ArgumentNullException("customer"); } //var genericAttributeService = EngineContext.Current.Resolve<IGenericAttributeService>(); string result = string.Empty; try { var existingGiftCartCouponCodes = customer.GetAttribute <string>(SystemCustomerAttributeNames.GiftCardCouponCodes, genericAttributeService); couponCode = couponCode.Trim().ToLower(); var xmlDoc = new XmlDocument(); if (String.IsNullOrEmpty(existingGiftCartCouponCodes)) { var element1 = xmlDoc.CreateElement("GiftCardCouponCodes"); xmlDoc.AppendChild(element1); } else { xmlDoc.LoadXml(existingGiftCartCouponCodes); } var rootElement = (XmlElement)xmlDoc.SelectSingleNode(@"//GiftCardCouponCodes"); XmlElement gcElement = null; //find existing var nodeList1 = xmlDoc.SelectNodes(@"//GiftCardCouponCodes/CouponCode"); foreach (XmlNode node1 in nodeList1) { if (node1.Attributes != null && node1.Attributes["Code"] != null) { string couponCodeAttribute = node1.Attributes["Code"].InnerText.Trim(); if (couponCodeAttribute.ToLower() == couponCode.ToLower()) { gcElement = (XmlElement)node1; break; } } } //create new one if not found if (gcElement == null) { gcElement = xmlDoc.CreateElement("CouponCode"); gcElement.SetAttribute("Code", couponCode); rootElement.AppendChild(gcElement); } result = xmlDoc.OuterXml; } catch (Exception exc) { Debug.Write(exc.ToString()); } //apply new value genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.GiftCardCouponCodes, result); }