private static void DeletePreviousRelatedProducts(Product product) { RelatedProductCollection relatedProducts = new RelatedProductCollection(); RelatedProductQuery q = new RelatedProductQuery(); q.Where(q.ProductId == product.Id); relatedProducts.Load(q); relatedProducts.MarkAllAsDeleted(); relatedProducts.Save(); }
protected void btnSave_Click(object sender, EventArgs e) { bool isNew = false; Product toSave = new Product(); if (!toSave.LoadByPrimaryKey(ParamId.GetValueOrDefault(-1))) { isNew = true; toSave.Name = txtName.Text; toSave.StoreId = StoreContext.CurrentStore.Id; } string slug = txtSlug.Text.CreateSlug(); if (toSave.Slug != slug) { if (!SlugFactory.IsSlugAvailable(StoreContext.CurrentStore.Id.Value, slug)) { ShowFlash(string.Format(@"The URL name ""{0}"" is already in use for this store, please choose another.", slug)); return; } } if (toSave.Name.Trim() != txtName.Text.Trim()) { var existingProductByName = Product.GetByName(StoreContext.CurrentStore.Id.Value, txtName.Text.Trim()); if (existingProductByName != null) { ShowFlash(string.Format(@"The product name ""{0}"" already exists in this store, please chose another product name", existingProductByName.Name)); return; } } string sku = txtSku.Text; if (!string.IsNullOrEmpty(sku)) { if (!RegexPatterns.IsValidSku.IsMatch(sku)) { ShowFlash(string.Format(@"The Sku '{0}' is invalid, it must match the pattern '{1}'", sku, RegexPatterns.IsValidSku)); return; } sku = sku.Trim(); } toSave.Name = txtName.Text.Trim(); toSave.Slug = slug; toSave.IsActive = chkIsActive.Checked; toSave.Price = Convert.ToDecimal(txtPrice.Text, CultureInfo.CreateSpecificCulture("en-US")); toSave.Sku = sku; toSave.SpecialNotes = txtSpecialNotes.Text.NewlineToBr(); toSave.IsTaxable = chkIsTaxable.Checked; toSave.IsPriceDisplayed = chkIsPriceDisplayed.Checked; toSave.IsAvailableForPurchase = chkIsAvailableForPurchase.Checked; toSave.DeliveryMethodId = WA.Parser.ToShort(rdoDeliveryMethod.SelectedValue); if (toSave.DeliveryMethodId.Value == (short)ProductDeliveryMethod.Shipped) { toSave.ShippingAdditionalFeePerItem = string.IsNullOrEmpty(txtAdditionalShippingFeePerItem.Text) ? 0 : Convert.ToDecimal(txtAdditionalShippingFeePerItem.Text, CultureInfo.CreateSpecificCulture("en-US")); toSave.Weight = String.IsNullOrEmpty(txtWeight.Text) ? 0 : Convert.ToDecimal(txtWeight.Text, CultureInfo.CreateSpecificCulture("en-US")); toSave.Length = String.IsNullOrEmpty(txtLength.Text) ? 0 : Convert.ToDecimal(txtLength.Text, CultureInfo.CreateSpecificCulture("en-US")); toSave.Width = String.IsNullOrEmpty(txtWidth.Text) ? 0 : Convert.ToDecimal(txtWidth.Text, CultureInfo.CreateSpecificCulture("en-US")); toSave.Height = String.IsNullOrEmpty(txtHeight.Text) ? 0 : Convert.ToDecimal(txtHeight.Text, CultureInfo.CreateSpecificCulture("en-US")); } else if (toSave.DeliveryMethodId.Value == (short)ProductDeliveryMethod.Downloaded) { toSave.Weight = 0; toSave.Length = null; toSave.Width = null; toSave.Height = null; toSave.ShippingAdditionalFeePerItem = 0; } toSave.QuantityWidget = rdoQuantityWidget.SelectedValue; toSave.QuantityOptions = txtQuantityOptions.Text; toSave.InventoryIsEnabled = chkInventoryIsEnabled.Checked; toSave.InventoryAllowNegativeStockLevel = chkInventoryAllowNegativeStockLevel.Checked; toSave.InventoryQtyInStock = WA.Parser.ToInt(txtInventoryQtyInStock.Text); toSave.InventoryQtyLowThreshold = WA.Parser.ToInt(txtInventoryQtyLowThreshold.Text); toSave.SeoTitle = txtSeoTitle.Text; toSave.SeoDescription = txtSeoDescription.Text; toSave.SeoKeywords = txtSeoKeywords.Text; toSave.CheckoutAssignRoleInfoJson = ParseCheckoutRoleInfoFromPost(); toSave.ViewPermissions = ParseViewPermissionInfoFromPost(); toSave.CheckoutPermissions = ParseCheckoutPermissionInfoFromPost(); toSave.Save(); //---- Product Categories List <string> productCategoryIdStrings = new List <string>(Request.Form.GetValues("productCategory") ?? new string[] { }); List <int?> productCategoryIds = productCategoryIdStrings.ConvertAll(s => WA.Parser.ToInt(s)); productCategoryIds.RemoveAll(i => !i.HasValue); Product.SetCategories(toSave.Id.Value, productCategoryIds.ConvertAll(i => i.Value)); //---- Related Products DeletePreviousRelatedProducts(toSave); var relatedlatedProducts = new RelatedProductCollection(); foreach (ListItem p in cblRelatedProducts.Items) { if (!p.Selected) { continue; } RelatedProduct relatedProduct = relatedlatedProducts.AddNew(); relatedProduct.ProductId = toSave.Id; relatedProduct.RelatedProductId = Convert.ToInt32(p.Value); } relatedlatedProducts.Save(); //---- Digital File upload); if (fupDigitalFile.HasFile) { string fileUploadDirectory = StoreUrls.ProductFileFolderFileRoot; string fileExt = Path.GetExtension(fupDigitalFile.PostedFile.FileName); string filenameWithExt = string.Format("{0}_{1}{2}", toSave.Id.Value, Guid.NewGuid(), fileExt); string filePath = fileUploadDirectory + filenameWithExt; //Debug.WriteFormat(@"fileUploadDirectory = ""{0}""", fileUploadDirectory); if (!Directory.Exists(fileUploadDirectory)) { //Debug.WriteFormat(@"creating fileUploadDirectory = ""{0}""", fileUploadDirectory); Directory.CreateDirectory(fileUploadDirectory); } fupDigitalFile.PostedFile.SaveAs(filePath); toSave.DigitalFilename = filenameWithExt; toSave.DigitalFileDisplayName = Path.GetFileNameWithoutExtension(fupDigitalFile.PostedFile.FileName).Left(250); toSave.Save(); } //---- Photos are saved via ajax (uploaded via ajax, separately, no need to save here) //---- Descriptor Fields using (esTransactionScope transaction = new esTransactionScope()) { toSave.ProductDescriptorCollectionByProductId.MarkAllAsDeleted(); toSave.Save(); AddDescriptor(txtDescriptorName1.Text, (txtDescriptorText1 as DotNetNuke.UI.UserControls.TextEditor).Text, 1, toSave); AddDescriptor(txtDescriptorName2.Text, (txtDescriptorText2 as DotNetNuke.UI.UserControls.TextEditor).Text, 2, toSave); AddDescriptor(txtDescriptorName3.Text, (txtDescriptorText3 as DotNetNuke.UI.UserControls.TextEditor).Text, 3, toSave); AddDescriptor(txtDescriptorName4.Text, (txtDescriptorText4 as DotNetNuke.UI.UserControls.TextEditor).Text, 4, toSave); AddDescriptor(txtDescriptorName5.Text, (txtDescriptorText5 as DotNetNuke.UI.UserControls.TextEditor).Text, 5, toSave); toSave.Save(); transaction.Complete(); } Response.Redirect(StoreUrls.AdminEditProduct(toSave.Id.Value, "Product Saved" + (isNew ? ", you can now add Photos, Descriptions, and Custom Attributes" : ""))); }