public ActionResult AddAttributeCombinationPopup(string btnId, string formId, int productId, AddProductVariantAttributeCombinationModel model, FormCollection form) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts)) return AccessDeniedView(); var product = _productService.GetProductById(productId); if (product == null) //No product found with the specified id return RedirectToAction("List", "Product"); //a vendor should have access only to his products if (_workContext.CurrentVendor != null&& product.VendorId != _workContext.CurrentVendor.Id) return RedirectToAction("List", "Product"); ViewBag.btnId = btnId; ViewBag.formId = formId; //attributes string attributes = ""; var warnings = new List<string>(); #region Product attributes string selectedAttributes = string.Empty; var productVariantAttributes = _productAttributeService.GetProductVariantAttributesByProductId(product.Id); foreach (var attribute in productVariantAttributes) { string controlId = string.Format("product_attribute_{0}_{1}", attribute.ProductAttributeId, attribute.Id); switch (attribute.AttributeControlType) { case AttributeControlType.DropdownList: case AttributeControlType.RadioList: case AttributeControlType.ColorSquares: { var ctrlAttributes = form[controlId]; if (!String.IsNullOrEmpty(ctrlAttributes)) { int selectedAttributeId = int.Parse(ctrlAttributes); if (selectedAttributeId > 0) selectedAttributes = _productAttributeParser.AddProductAttribute(selectedAttributes, attribute, selectedAttributeId.ToString()); } } break; case AttributeControlType.Checkboxes: { var cblAttributes = form[controlId]; if (!String.IsNullOrEmpty(cblAttributes)) { foreach (var item in cblAttributes.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { int selectedAttributeId = int.Parse(item); if (selectedAttributeId > 0) selectedAttributes = _productAttributeParser.AddProductAttribute(selectedAttributes, attribute, selectedAttributeId.ToString()); } } } break; case AttributeControlType.ReadonlyCheckboxes: { //load read-only (already server-side selected) values var pvaValues = _productAttributeService.GetProductVariantAttributeValues(attribute.Id); foreach (var selectedAttributeId in pvaValues .Where(pvav => pvav.IsPreSelected) .Select(pvav => pvav.Id) .ToList()) { selectedAttributes = _productAttributeParser.AddProductAttribute(selectedAttributes, attribute, selectedAttributeId.ToString()); } } break; case AttributeControlType.TextBox: case AttributeControlType.MultilineTextbox: { var ctrlAttributes = form[controlId]; if (!String.IsNullOrEmpty(ctrlAttributes)) { string enteredText = ctrlAttributes.Trim(); selectedAttributes = _productAttributeParser.AddProductAttribute(selectedAttributes, attribute, enteredText); } } break; case AttributeControlType.Datepicker: { var date = form[controlId + "_day"]; var month = form[controlId + "_month"]; var year = form[controlId + "_year"]; DateTime? selectedDate = null; try { selectedDate = new DateTime(Int32.Parse(year), Int32.Parse(month), Int32.Parse(date)); } catch { } if (selectedDate.HasValue) { selectedAttributes = _productAttributeParser.AddProductAttribute(selectedAttributes, attribute, selectedDate.Value.ToString("D")); } } break; case AttributeControlType.FileUpload: { var httpPostedFile = this.Request.Files[controlId]; if ((httpPostedFile != null) && (!String.IsNullOrEmpty(httpPostedFile.FileName))) { var fileSizeOk = true; if (attribute.ValidationFileMaximumSize.HasValue) { //compare in bytes var maxFileSizeBytes = attribute.ValidationFileMaximumSize.Value * 1024; if (httpPostedFile.ContentLength > maxFileSizeBytes) { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.MaximumUploadedFileSize"), attribute.ValidationFileMaximumSize.Value)); fileSizeOk = false; } } if (fileSizeOk) { //save an uploaded file var download = new Download() { DownloadGuid = Guid.NewGuid(), UseDownloadUrl = false, DownloadUrl = "", DownloadBinary = httpPostedFile.GetDownloadBits(), ContentType = httpPostedFile.ContentType, Filename = System.IO.Path.GetFileNameWithoutExtension(httpPostedFile.FileName), Extension = System.IO.Path.GetExtension(httpPostedFile.FileName), IsNew = true }; _downloadService.InsertDownload(download); //save attribute selectedAttributes = _productAttributeParser.AddProductAttribute(selectedAttributes, attribute, download.DownloadGuid.ToString()); } } } break; default: break; } } attributes = selectedAttributes; #endregion warnings.AddRange(_shoppingCartService.GetShoppingCartItemAttributeWarnings(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart, product, 1, attributes)); if (warnings.Count == 0) { //save combination var combination = new ProductVariantAttributeCombination() { ProductId = product.Id, AttributesXml = attributes, StockQuantity = model.StockQuantity, AllowOutOfStockOrders = model.AllowOutOfStockOrders, Sku = model.Sku, ManufacturerPartNumber = model.ManufacturerPartNumber, Gtin = model.Gtin, OverriddenPrice = model.OverriddenPrice }; _productAttributeService.InsertProductVariantAttributeCombination(combination); ViewBag.RefreshPage = true; return View(model); } else { //If we got this far, something failed, redisplay form PrepareAddProductAttributeCombinationModel(model, product); model.Warnings = warnings; return View(model); } }
protected virtual void PrepareAddProductAttributeCombinationModel(AddProductVariantAttributeCombinationModel model, Product product) { if (model == null) throw new ArgumentNullException("model"); if (product == null) throw new ArgumentNullException("product"); model.ProductId = product.Id; model.StockQuantity = 10000; var productVariantAttributes = _productAttributeService.GetProductVariantAttributesByProductId(product.Id); foreach (var attribute in productVariantAttributes) { var pvaModel = new AddProductVariantAttributeCombinationModel.ProductVariantAttributeModel() { Id = attribute.Id, ProductAttributeId = attribute.ProductAttributeId, Name = attribute.ProductAttribute.Name, TextPrompt = attribute.TextPrompt, IsRequired = attribute.IsRequired, AttributeControlType = attribute.AttributeControlType }; if (attribute.ShouldHaveValues()) { //values var pvaValues = _productAttributeService.GetProductVariantAttributeValues(attribute.Id); foreach (var pvaValue in pvaValues) { var pvaValueModel = new AddProductVariantAttributeCombinationModel.ProductVariantAttributeValueModel() { Id = pvaValue.Id, Name = pvaValue.Name, IsPreSelected = pvaValue.IsPreSelected }; pvaModel.Values.Add(pvaValueModel); } } model.ProductVariantAttributes.Add(pvaModel); } }
//edit public ActionResult AddAttributeCombinationPopup(string btnId, string formId, int productId) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts)) return AccessDeniedView(); var product = _productService.GetProductById(productId); if (product == null) //No product found with the specified id return RedirectToAction("List", "Product"); //a vendor should have access only to his products if (_workContext.CurrentVendor != null && product.VendorId != _workContext.CurrentVendor.Id) return RedirectToAction("List", "Product"); ViewBag.btnId = btnId; ViewBag.formId = formId; var model = new AddProductVariantAttributeCombinationModel(); PrepareAddProductAttributeCombinationModel(model, product); return View(model); }