public virtual async Task <IActionResult> CheckoutAttributeChange(IFormCollection form, [FromServices] ICheckoutAttributeParser checkoutAttributeParser, [FromServices] ICheckoutAttributeFormatter checkoutAttributeFormatter) { var cart = _shoppingCartService.GetShoppingCart(_storeContext.CurrentStore.Id, ShoppingCartType.ShoppingCart, ShoppingCartType.Auctions); var attributeXml = await _mediator.Send(new SaveCheckoutAttributesCommand() { Customer = _workContext.CurrentCustomer, Store = _storeContext.CurrentStore, Cart = cart, Form = form }); var enabledAttributeIds = new List <string>(); var disabledAttributeIds = new List <string>(); var attributes = await _checkoutAttributeService.GetAllCheckoutAttributes(_storeContext.CurrentStore.Id, !cart.RequiresShipping()); foreach (var attribute in attributes) { var conditionMet = await checkoutAttributeParser.IsConditionMet(attribute, attributeXml); if (conditionMet.HasValue) { if (conditionMet.Value) { enabledAttributeIds.Add(attribute.Id); } else { disabledAttributeIds.Add(attribute.Id); } } } var model = await _mediator.Send(new GetOrderTotals() { Cart = cart, IsEditable = true, Store = _storeContext.CurrentStore, Currency = _workContext.WorkingCurrency, Customer = _workContext.CurrentCustomer, Language = _workContext.WorkingLanguage, TaxDisplayType = _workContext.TaxDisplayType }); return(Json(new { enabledattributeids = enabledAttributeIds.ToArray(), disabledattributeids = disabledAttributeIds.ToArray(), htmlordertotal = await RenderPartialViewToString("Components/OrderTotals/Default", model), model = model, checkoutattributeinfo = await checkoutAttributeFormatter.FormatAttributes(attributeXml, _workContext.CurrentCustomer), })); }
public virtual IActionResult CheckoutAttributeChange(IFormCollection form, [FromServices] ICheckoutAttributeParser checkoutAttributeParser, [FromServices] ICheckoutAttributeFormatter checkoutAttributeFormatter) { var cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart || sci.ShoppingCartType == ShoppingCartType.Auctions) .LimitPerStore(_storeContext.CurrentStore.Id) .ToList(); _shoppingCartViewModelService.ParseAndSaveCheckoutAttributes(cart, form); var attributeXml = _workContext.CurrentCustomer.GetAttribute <string>(SystemCustomerAttributeNames.CheckoutAttributes, _storeContext.CurrentStore.Id); var enabledAttributeIds = new List <string>(); var disabledAttributeIds = new List <string>(); var attributes = _checkoutAttributeService.GetAllCheckoutAttributes(_storeContext.CurrentStore.Id, !cart.RequiresShipping()); foreach (var attribute in attributes) { var conditionMet = checkoutAttributeParser.IsConditionMet(attribute, attributeXml); if (conditionMet.HasValue) { if (conditionMet.Value) { enabledAttributeIds.Add(attribute.Id); } else { disabledAttributeIds.Add(attribute.Id); } } } return(Json(new { enabledattributeids = enabledAttributeIds.ToArray(), disabledattributeids = disabledAttributeIds.ToArray(), htmlordertotal = this.RenderPartialViewToString("Components/OrderTotals/Default", _shoppingCartViewModelService.PrepareOrderTotals(cart, true)), checkoutattributeinfo = checkoutAttributeFormatter.FormatAttributes(attributeXml, _workContext.CurrentCustomer), })); }
public virtual async Task <IActionResult> CheckoutAttributeChange(IFormCollection form, [FromServices] ICheckoutAttributeParser checkoutAttributeParser, [FromServices] ICheckoutAttributeFormatter checkoutAttributeFormatter) { var cart = _shoppingCartService.GetShoppingCart(_storeContext.CurrentStore.Id, ShoppingCartType.ShoppingCart, ShoppingCartType.Auctions); await _shoppingCartViewModelService.ParseAndSaveCheckoutAttributes(cart, form); var attributeXml = await _workContext.CurrentCustomer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.CheckoutAttributes, _storeContext.CurrentStore.Id); var enabledAttributeIds = new List <string>(); var disabledAttributeIds = new List <string>(); var attributes = await _checkoutAttributeService.GetAllCheckoutAttributes(_storeContext.CurrentStore.Id, !cart.RequiresShipping()); foreach (var attribute in attributes) { var conditionMet = await checkoutAttributeParser.IsConditionMet(attribute, attributeXml); if (conditionMet.HasValue) { if (conditionMet.Value) { enabledAttributeIds.Add(attribute.Id); } else { disabledAttributeIds.Add(attribute.Id); } } } return(Json(new { enabledattributeids = enabledAttributeIds.ToArray(), disabledattributeids = disabledAttributeIds.ToArray(), htmlordertotal = RenderPartialViewToString("Components/OrderTotals/Default", await _shoppingCartViewModelService.PrepareOrderTotals(cart, true)), checkoutattributeinfo = await checkoutAttributeFormatter.FormatAttributes(attributeXml, _workContext.CurrentCustomer), })); }
public virtual async Task <IList <string> > GetShoppingCartWarnings(IList <ShoppingCartItem> shoppingCart, IList <CustomAttribute> checkoutAttributes, bool validateCheckoutAttributes) { var warnings = new List <string>(); if (checkoutAttributes == null) { checkoutAttributes = new List <CustomAttribute>(); } bool hasStandartProducts = false; bool hasRecurringProducts = false; foreach (var sci in shoppingCart) { var product = await _productService.GetProductById(sci.ProductId); if (product == null) { warnings.Add(string.Format(_translationService.GetResource("ShoppingCart.CannotLoadProduct"), sci.ProductId)); return(warnings); } if (product.IsRecurring) { hasRecurringProducts = true; } else { hasStandartProducts = true; } } //don't mix standard and recurring products if (hasStandartProducts && hasRecurringProducts) { warnings.Add(_translationService.GetResource("ShoppingCart.CannotMixStandardAndAutoshipProducts")); } //validate checkout attributes if (validateCheckoutAttributes) { //selected attributes var attributes1 = await _checkoutAttributeParser.ParseCheckoutAttributes(checkoutAttributes); //existing checkout attributes var attributes2 = await _checkoutAttributeService.GetAllCheckoutAttributes(_workContext.CurrentStore.Id, !shoppingCart.RequiresShipping()); foreach (var a2 in attributes2) { var conditionMet = await _checkoutAttributeParser.IsConditionMet(a2, checkoutAttributes); if (a2.IsRequired && ((conditionMet.HasValue && conditionMet.Value) || !conditionMet.HasValue)) { bool found = false; //selected checkout attributes foreach (var a1 in attributes1) { if (a1.Id == a2.Id) { var attributeValuesStr = checkoutAttributes.Where(x => x.Key == a1.Id).Select(x => x.Value); foreach (var str1 in attributeValuesStr) { if (!string.IsNullOrEmpty(str1.Trim())) { found = true; break; } } } } //if not found if (!found) { if (!string.IsNullOrEmpty(a2.GetTranslation(a => a.TextPrompt, _workContext.WorkingLanguage.Id))) { warnings.Add(a2.GetTranslation(a => a.TextPrompt, _workContext.WorkingLanguage.Id)); } else { warnings.Add(string.Format(_translationService.GetResource("ShoppingCart.SelectAttribute"), a2.GetTranslation(a => a.Name, _workContext.WorkingLanguage.Id))); } } } } //now validation rules //minimum length foreach (var ca in attributes2) { if (ca.ValidationMinLength.HasValue) { if (ca.AttributeControlTypeId == AttributeControlType.TextBox || ca.AttributeControlTypeId == AttributeControlType.MultilineTextbox) { var valuesStr = checkoutAttributes.Where(x => x.Key == ca.Id).Select(x => x.Value); var enteredText = valuesStr?.FirstOrDefault(); int enteredTextLength = string.IsNullOrEmpty(enteredText) ? 0 : enteredText.Length; if (ca.ValidationMinLength.Value > enteredTextLength) { warnings.Add(string.Format(_translationService.GetResource("ShoppingCart.TextboxMinimumLength"), ca.GetTranslation(a => a.Name, _workContext.WorkingLanguage.Id), ca.ValidationMinLength.Value)); } } } //maximum length if (ca.ValidationMaxLength.HasValue) { if (ca.AttributeControlTypeId == AttributeControlType.TextBox || ca.AttributeControlTypeId == AttributeControlType.MultilineTextbox) { var valuesStr = checkoutAttributes.Where(x => x.Key == ca.Id).Select(x => x.Value); var enteredText = valuesStr?.FirstOrDefault(); int enteredTextLength = string.IsNullOrEmpty(enteredText) ? 0 : enteredText.Length; if (ca.ValidationMaxLength.Value < enteredTextLength) { warnings.Add(string.Format(_translationService.GetResource("ShoppingCart.TextboxMaximumLength"), ca.GetTranslation(a => a.Name, _workContext.WorkingLanguage.Id), ca.ValidationMaxLength.Value)); } } } } } //event notification await _mediator.ShoppingCartWarningsAdd(warnings, shoppingCart, checkoutAttributes, validateCheckoutAttributes); return(warnings); }
public async Task <IList <CustomAttribute> > Handle(SaveCheckoutAttributesCommand request, CancellationToken cancellationToken) { if (request.Cart == null) { throw new ArgumentNullException("cart"); } if (request.Form == null) { throw new ArgumentNullException("form"); } var customAttributes = new List <CustomAttribute>(); var checkoutAttributes = await _checkoutAttributeService.GetAllCheckoutAttributes(request.Store.Id, !request.Cart.RequiresShipping()); foreach (var attribute in checkoutAttributes) { string controlId = string.Format("checkout_attribute_{0}", attribute.Id); switch (attribute.AttributeControlType) { case AttributeControlType.DropdownList: case AttributeControlType.RadioList: case AttributeControlType.ColorSquares: case AttributeControlType.ImageSquares: { var ctrlAttributes = request.Form[controlId]; if (!string.IsNullOrEmpty(ctrlAttributes)) { customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes, attribute, ctrlAttributes).ToList(); } } break; case AttributeControlType.Checkboxes: { var cblAttributes = request.Form[controlId].ToString(); if (!string.IsNullOrEmpty(cblAttributes)) { foreach (var item in cblAttributes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes, attribute, item).ToList(); } } } break; case AttributeControlType.ReadonlyCheckboxes: { //load read-only (already server-side selected) values var attributeValues = attribute.CheckoutAttributeValues; foreach (var selectedAttributeId in attributeValues .Where(v => v.IsPreSelected) .Select(v => v.Id) .ToList()) { customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes, attribute, selectedAttributeId.ToString()).ToList(); } } break; case AttributeControlType.TextBox: case AttributeControlType.MultilineTextbox: { var ctrlAttributes = request.Form[controlId].ToString(); if (!string.IsNullOrEmpty(ctrlAttributes)) { string enteredText = ctrlAttributes.Trim(); customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes, attribute, enteredText).ToList(); } } break; case AttributeControlType.Datepicker: { var date = request.Form[controlId + "_day"]; var month = request.Form[controlId + "_month"]; var year = request.Form[controlId + "_year"]; DateTime?selectedDate = null; try { selectedDate = new DateTime(Int32.Parse(year), Int32.Parse(month), Int32.Parse(date)); } catch { } if (selectedDate.HasValue) { customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes, attribute, selectedDate.Value.ToString("D")).ToList(); } } break; case AttributeControlType.FileUpload: { Guid downloadGuid; Guid.TryParse(request.Form[controlId], out downloadGuid); var download = await _downloadService.GetDownloadByGuid(downloadGuid); if (download != null) { customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes, attribute, download.DownloadGuid.ToString()).ToList(); } } break; default: break; } } //save checkout attributes //validate conditional attributes (if specified) foreach (var attribute in checkoutAttributes) { var conditionMet = await _checkoutAttributeParser.IsConditionMet(attribute, customAttributes); if (conditionMet.HasValue && !conditionMet.Value) { customAttributes = _checkoutAttributeParser.RemoveCheckoutAttribute(customAttributes, attribute).ToList(); } } await _genericAttributeService.SaveAttribute(request.Customer, SystemCustomerAttributeNames.CheckoutAttributes, customAttributes, request.Store.Id); return(customAttributes); }