/// <summary> /// Creates a copy of product with all depended data /// </summary> /// <param name="productId">The product identifier</param> /// <param name="name">The name of product duplicate</param> /// <param name="isPublished">A value indicating whether the product duplicate should be published</param> /// <param name="copyImages">A value indicating whether the product images should be copied</param> /// <returns>Product entity</returns> public Product DuplicateProduct(int productId, string name, bool isPublished, bool copyImages) { var product = GetProductById(productId); if (product == null) return null; Product productCopy = null; //uncomment this line to support transactions //using (var scope = new System.Transactions.TransactionScope()) { // product productCopy = new Product() { Name = name, ShortDescription = product.ShortDescription, FullDescription = product.FullDescription, AdminComment = product.AdminComment, TemplateId = product.TemplateId, ShowOnHomePage = product.ShowOnHomePage, MetaKeywords = product.MetaKeywords, MetaDescription = product.MetaDescription, MetaTitle = product.MetaTitle, SEName = product.SEName, AllowCustomerReviews = product.AllowCustomerReviews, AllowCustomerRatings = product.AllowCustomerRatings, Published = isPublished, Deleted = product.Deleted, CreatedOn = DateTime.UtcNow, UpdatedOn = DateTime.UtcNow }; InsertProduct(productCopy); if (productCopy == null) return null; var languages = IoC.Resolve<ILanguageService>().GetAllLanguages(true); //localization foreach (var lang in languages) { var productLocalized = GetProductLocalizedByProductIdAndLanguageId(product.ProductId, lang.LanguageId); if (productLocalized != null) { var productLocalizedCopy = new ProductLocalized() { ProductId = productCopy.ProductId, LanguageId = productLocalized.LanguageId, Name = productLocalized.Name, ShortDescription = productLocalized.ShortDescription, FullDescription = productLocalized.FullDescription, MetaKeywords = productLocalized.MetaKeywords, MetaDescription = productLocalized.MetaDescription, MetaTitle = productLocalized.MetaTitle, SEName = productLocalized.SEName }; InsertProductLocalized(productLocalizedCopy); } } // product pictures if (copyImages) { foreach (var productPicture in product.ProductPictures) { var picture = productPicture.Picture; var pictureCopy = IoC.Resolve<IPictureService>().InsertPicture(picture.PictureBinary, picture.MimeType, picture.IsNew); InsertProductPicture(new ProductPicture() { ProductId = productCopy.ProductId, PictureId = pictureCopy.PictureId, DisplayOrder = productPicture.DisplayOrder }); } } // product <-> categories mappings foreach (var productCategory in product.ProductCategories) { var productCategoryCopy = new ProductCategory() { ProductId = productCopy.ProductId, CategoryId = productCategory.CategoryId, IsFeaturedProduct = productCategory.IsFeaturedProduct, DisplayOrder = productCategory.DisplayOrder }; IoC.Resolve<ICategoryService>().InsertProductCategory(productCategoryCopy); } // product <-> manufacturers mappings foreach (var productManufacturers in product.ProductManufacturers) { var productManufacturerCopy = new ProductManufacturer() { ProductId = productCopy.ProductId, ManufacturerId = productManufacturers.ManufacturerId, IsFeaturedProduct = productManufacturers.IsFeaturedProduct, DisplayOrder = productManufacturers.DisplayOrder }; IoC.Resolve<IManufacturerService>().InsertProductManufacturer(productManufacturerCopy); } // product <-> releated products mappings foreach (var relatedProduct in product.RelatedProducts) { InsertRelatedProduct( new RelatedProduct() { ProductId1 = productCopy.ProductId, ProductId2 = relatedProduct.ProductId2, DisplayOrder = relatedProduct.DisplayOrder }); } // product specifications foreach (var productSpecificationAttribute in IoC.Resolve<ISpecificationAttributeService>().GetProductSpecificationAttributesByProductId(product.ProductId)) { var psaCopy = new ProductSpecificationAttribute() { ProductId = productCopy.ProductId, SpecificationAttributeOptionId = productSpecificationAttribute.SpecificationAttributeOptionId, AllowFiltering = productSpecificationAttribute.AllowFiltering, ShowOnProductPage = productSpecificationAttribute.ShowOnProductPage, DisplayOrder = productSpecificationAttribute.DisplayOrder }; IoC.Resolve<ISpecificationAttributeService>().InsertProductSpecificationAttribute(psaCopy); } // product variants var productVariants = GetProductVariantsByProductId(product.ProductId, true); foreach (var productVariant in productVariants) { // product variant picture int pictureId = 0; if (copyImages) { var picture = productVariant.Picture; if (picture != null) { var pictureCopy = IoC.Resolve<IPictureService>().InsertPicture(picture.PictureBinary, picture.MimeType, picture.IsNew); pictureId = pictureCopy.PictureId; } } // product variant download & sample download int downloadId = productVariant.DownloadId; int sampleDownloadId = productVariant.SampleDownloadId; if (productVariant.IsDownload) { var download = productVariant.Download; if (download != null) { var downloadCopy = new Download() { UseDownloadUrl = download.UseDownloadUrl, DownloadUrl = download.DownloadUrl, DownloadBinary = download.DownloadBinary, ContentType = download.ContentType, Filename = download.Filename, Extension = download.Extension, IsNew = download.IsNew }; IoC.Resolve<IDownloadService>().InsertDownload(downloadCopy); downloadId = downloadCopy.DownloadId; } if (productVariant.HasSampleDownload) { var sampleDownload = productVariant.SampleDownload; if (sampleDownload != null) { var sampleDownloadCopy = new Download() { UseDownloadUrl = sampleDownload.UseDownloadUrl, DownloadUrl = sampleDownload.DownloadUrl, DownloadBinary = sampleDownload.DownloadBinary, ContentType = sampleDownload.ContentType, Filename = sampleDownload.Filename, Extension = sampleDownload.Extension, IsNew = sampleDownload.IsNew }; IoC.Resolve<IDownloadService>().InsertDownload(sampleDownloadCopy); sampleDownloadId = sampleDownloadCopy.DownloadId; } } } // product variant var productVariantCopy = new ProductVariant() { ProductId = productCopy.ProductId, Name = productVariant.Name, SKU = productVariant.SKU, Description = productVariant.Description, AdminComment = productVariant.AdminComment, ManufacturerPartNumber = productVariant.ManufacturerPartNumber, IsGiftCard = productVariant.IsGiftCard, GiftCardType = productVariant.GiftCardType, IsDownload = productVariant.IsDownload, DownloadId = downloadId, UnlimitedDownloads = productVariant.UnlimitedDownloads, MaxNumberOfDownloads = productVariant.MaxNumberOfDownloads, DownloadExpirationDays = productVariant.DownloadExpirationDays, DownloadActivationType = productVariant.DownloadActivationType, HasSampleDownload = productVariant.HasSampleDownload, SampleDownloadId = sampleDownloadId, HasUserAgreement = productVariant.HasUserAgreement, UserAgreementText = productVariant.UserAgreementText, IsRecurring = productVariant.IsRecurring, CycleLength = productVariant.CycleLength, CyclePeriod = productVariant.CyclePeriod, TotalCycles = productVariant.TotalCycles, IsShipEnabled = productVariant.IsShipEnabled, IsFreeShipping = productVariant.IsFreeShipping, AdditionalShippingCharge = productVariant.AdditionalShippingCharge, IsTaxExempt = productVariant.IsTaxExempt, TaxCategoryId = productVariant.TaxCategoryId, ManageInventory = productVariant.ManageInventory, StockQuantity = productVariant.StockQuantity, DisplayStockAvailability = productVariant.DisplayStockAvailability, DisplayStockQuantity = productVariant.DisplayStockQuantity, MinStockQuantity = productVariant.MinStockQuantity, LowStockActivityId = productVariant.LowStockActivityId, NotifyAdminForQuantityBelow = productVariant.NotifyAdminForQuantityBelow, Backorders = productVariant.Backorders, OrderMinimumQuantity = productVariant.OrderMinimumQuantity, OrderMaximumQuantity = productVariant.OrderMaximumQuantity, WarehouseId = productVariant.WarehouseId, DisableBuyButton = productVariant.DisableBuyButton, CallForPrice = productVariant.CallForPrice, Price = productVariant.Price, OldPrice = productVariant.OldPrice, ProductCost = productVariant.ProductCost, CustomerEntersPrice = productVariant.CustomerEntersPrice, MinimumCustomerEnteredPrice = productVariant.MinimumCustomerEnteredPrice, MaximumCustomerEnteredPrice = productVariant.MaximumCustomerEnteredPrice, Weight = productVariant.Weight, Length = productVariant.Length, Width = productVariant.Width, Height = productVariant.Height, PictureId = pictureId, AvailableStartDateTime = productVariant.AvailableStartDateTime, AvailableEndDateTime = productVariant.AvailableEndDateTime, Published = productVariant.Published, Deleted = productVariant.Deleted, DisplayOrder = productVariant.DisplayOrder, CreatedOn = DateTime.UtcNow, UpdatedOn = DateTime.UtcNow }; InsertProductVariant(productVariantCopy); //localization foreach (var lang in languages) { var productVariantLocalized = GetProductVariantLocalizedByProductVariantIdAndLanguageId(productVariant.ProductVariantId, lang.LanguageId); if (productVariantLocalized != null) { var productVariantLocalizedCopy = new ProductVariantLocalized() { ProductVariantId = productVariantCopy.ProductVariantId, LanguageId = productVariantLocalized.LanguageId, Name = productVariantLocalized.Name, Description = productVariantLocalized.Description }; InsertProductVariantLocalized(productVariantLocalizedCopy); } } // product variant <-> attributes mappings foreach (var productVariantAttribute in IoC.Resolve<IProductAttributeService>().GetProductVariantAttributesByProductVariantId(productVariant.ProductVariantId)) { var productVariantAttributeCopy = new ProductVariantAttribute() { ProductVariantId = productVariantCopy.ProductVariantId, ProductAttributeId = productVariantAttribute.ProductAttributeId, TextPrompt = productVariantAttribute.TextPrompt, IsRequired = productVariantAttribute.IsRequired, AttributeControlTypeId = productVariantAttribute.AttributeControlTypeId, DisplayOrder = productVariantAttribute.DisplayOrder }; IoC.Resolve<IProductAttributeService>().InsertProductVariantAttribute(productVariantAttributeCopy); // product variant attribute values var productVariantAttributeValues = IoC.Resolve<IProductAttributeService>().GetProductVariantAttributeValues(productVariantAttribute.ProductVariantAttributeId); foreach (var productVariantAttributeValue in productVariantAttributeValues) { var pvavCopy = new ProductVariantAttributeValue() { ProductVariantAttributeId = productVariantAttributeCopy.ProductVariantAttributeId, Name = productVariantAttributeValue.Name, PriceAdjustment = productVariantAttributeValue.PriceAdjustment, WeightAdjustment = productVariantAttributeValue.WeightAdjustment, IsPreSelected = productVariantAttributeValue.IsPreSelected, DisplayOrder = productVariantAttributeValue.DisplayOrder }; IoC.Resolve<IProductAttributeService>().InsertProductVariantAttributeValue(pvavCopy); //localization foreach (var lang in languages) { var pvavLocalized = IoC.Resolve<IProductAttributeService>().GetProductVariantAttributeValueLocalizedByProductVariantAttributeValueIdAndLanguageId(productVariantAttributeValue.ProductVariantAttributeValueId, lang.LanguageId); if (pvavLocalized != null) { var pvavLocalizedCopy = new ProductVariantAttributeValueLocalized() { ProductVariantAttributeValueId = pvavCopy.ProductVariantAttributeValueId, LanguageId = pvavLocalized.LanguageId, Name = pvavLocalized.Name }; IoC.Resolve<IProductAttributeService>().InsertProductVariantAttributeValueLocalized(pvavLocalizedCopy); } } } } foreach (var combination in IoC.Resolve<IProductAttributeService>().GetAllProductVariantAttributeCombinations(productVariant.ProductVariantId)) { var combinationCopy = new ProductVariantAttributeCombination() { ProductVariantId = productVariantCopy.ProductVariantId, AttributesXml = combination.AttributesXml, StockQuantity = combination.StockQuantity, AllowOutOfStockOrders = combination.AllowOutOfStockOrders }; IoC.Resolve<IProductAttributeService>().InsertProductVariantAttributeCombination(combinationCopy); } // product variant tier prices foreach (var tierPrice in productVariant.TierPrices) { InsertTierPrice( new TierPrice() { ProductVariantId = productVariantCopy.ProductVariantId, Quantity = tierPrice.Quantity, Price = tierPrice.Price }); } // product variant <-> discounts mapping foreach (var discount in productVariant.AllDiscounts) { IoC.Resolve<IDiscountService>().AddDiscountToProductVariant(productVariantCopy.ProductVariantId, discount.DiscountId); } // prices by customer role foreach (var crpp in productVariant.CustomerRoleProductPrices) { this.InsertCustomerRoleProductPrice( new CustomerRoleProductPrice() { CustomerRoleId = crpp.CustomerRoleId, ProductVariantId = productVariantCopy.ProductVariantId, Price = crpp.Price } ); } } //uncomment this line to support transactions //scope.Complete(); } return productCopy; }
/// <summary> /// Update a localized product /// </summary> /// <param name="productLocalized">Product content</param> public void UpdateProductLocalized(ProductLocalized productLocalized) { if (productLocalized == null) throw new ArgumentNullException("productLocalized"); productLocalized.Name = CommonHelper.EnsureNotNull(productLocalized.Name); productLocalized.Name = CommonHelper.EnsureMaximumLength(productLocalized.Name, 400); productLocalized.ShortDescription = CommonHelper.EnsureNotNull(productLocalized.ShortDescription); productLocalized.FullDescription = CommonHelper.EnsureNotNull(productLocalized.FullDescription); productLocalized.MetaKeywords = CommonHelper.EnsureNotNull(productLocalized.MetaKeywords); productLocalized.MetaKeywords = CommonHelper.EnsureMaximumLength(productLocalized.MetaKeywords, 400); productLocalized.MetaDescription = CommonHelper.EnsureNotNull(productLocalized.MetaDescription); productLocalized.MetaDescription = CommonHelper.EnsureMaximumLength(productLocalized.MetaDescription, 4000); productLocalized.MetaTitle = CommonHelper.EnsureNotNull(productLocalized.MetaTitle); productLocalized.MetaTitle = CommonHelper.EnsureMaximumLength(productLocalized.MetaTitle, 400); productLocalized.SEName = CommonHelper.EnsureNotNull(productLocalized.SEName); productLocalized.SEName = CommonHelper.EnsureMaximumLength(productLocalized.SEName, 100); bool allFieldsAreEmpty = string.IsNullOrEmpty(productLocalized.Name) && string.IsNullOrEmpty(productLocalized.ShortDescription) && string.IsNullOrEmpty(productLocalized.FullDescription) && string.IsNullOrEmpty(productLocalized.MetaKeywords) && string.IsNullOrEmpty(productLocalized.MetaDescription) && string.IsNullOrEmpty(productLocalized.MetaTitle) && string.IsNullOrEmpty(productLocalized.SEName); if (!_context.IsAttached(productLocalized)) _context.ProductLocalized.Attach(productLocalized); if (allFieldsAreEmpty) { //delete if all fields are empty _context.DeleteObject(productLocalized); _context.SaveChanges(); } else { _context.SaveChanges(); } if (this.CacheEnabled) { _cacheManager.RemoveByPattern(PRODUCTS_PATTERN_KEY); _cacheManager.RemoveByPattern(PRODUCTVARIANTS_PATTERN_KEY); _cacheManager.RemoveByPattern(TIERPRICES_PATTERN_KEY); _cacheManager.RemoveByPattern(CUSTOMERROLEPRICES_PATTERN_KEY); } }
protected void SaveLocalizableContent(Product product) { if (product == null) return; if (!this.HasLocalizableContent) return; foreach (RepeaterItem item in rptrLanguageDivs.Items) { if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) { var txtLocalizedName = (TextBox)item.FindControl("txtLocalizedName"); var txtLocalizedShortDescription = (TextBox)item.FindControl("txtLocalizedShortDescription"); var txtLocalizedFullDescription = (FCKeditor)item.FindControl("txtLocalizedFullDescription"); var lblLanguageId = (Label)item.FindControl("lblLanguageId"); int languageId = int.Parse(lblLanguageId.Text); string name = txtLocalizedName.Text; string shortDescription = txtLocalizedShortDescription.Text; string fullDescription = txtLocalizedFullDescription.Value; bool allFieldsAreEmpty = (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(shortDescription) && string.IsNullOrEmpty(fullDescription)); var content = this.ProductService.GetProductLocalizedByProductIdAndLanguageId(product.ProductId, languageId); if (content == null) { if (!allFieldsAreEmpty && languageId > 0) { //only insert if one of the fields are filled out (avoid too many empty records in db...) content = new ProductLocalized() { ProductId = product.ProductId, LanguageId = languageId, Name = name, ShortDescription = shortDescription, FullDescription = fullDescription }; this.ProductService.InsertProductLocalized(content); } } else { if (languageId > 0) { content.LanguageId = languageId; content.Name = name; content.ShortDescription = shortDescription; content.FullDescription = fullDescription; this.ProductService.UpdateProductLocalized(content); } } } } }
/// <summary> /// Inserts a localized product /// </summary> /// <param name="productLocalized">Product content</param> public void InsertProductLocalized(ProductLocalized productLocalized) { if (productLocalized == null) throw new ArgumentNullException("productLocalized"); productLocalized.Name = CommonHelper.EnsureNotNull(productLocalized.Name); productLocalized.Name = CommonHelper.EnsureMaximumLength(productLocalized.Name, 400); productLocalized.ShortDescription = CommonHelper.EnsureNotNull(productLocalized.ShortDescription); productLocalized.FullDescription = CommonHelper.EnsureNotNull(productLocalized.FullDescription); productLocalized.MetaKeywords = CommonHelper.EnsureNotNull(productLocalized.MetaKeywords); productLocalized.MetaKeywords = CommonHelper.EnsureMaximumLength(productLocalized.MetaKeywords, 400); productLocalized.MetaDescription = CommonHelper.EnsureNotNull(productLocalized.MetaDescription); productLocalized.MetaDescription = CommonHelper.EnsureMaximumLength(productLocalized.MetaDescription, 4000); productLocalized.MetaTitle = CommonHelper.EnsureNotNull(productLocalized.MetaTitle); productLocalized.MetaTitle = CommonHelper.EnsureMaximumLength(productLocalized.MetaTitle, 400); productLocalized.SEName = CommonHelper.EnsureNotNull(productLocalized.SEName); productLocalized.SEName = CommonHelper.EnsureMaximumLength(productLocalized.SEName, 100); _context.ProductLocalized.AddObject(productLocalized); _context.SaveChanges(); if (this.CacheEnabled) { _cacheManager.RemoveByPattern(PRODUCTS_PATTERN_KEY); _cacheManager.RemoveByPattern(PRODUCTVARIANTS_PATTERN_KEY); _cacheManager.RemoveByPattern(TIERPRICES_PATTERN_KEY); _cacheManager.RemoveByPattern(CUSTOMERROLEPRICES_PATTERN_KEY); } }
protected void SaveLocalizableContent(Product product) { if (product == null) return; if (!this.HasLocalizableContent) return; foreach (RepeaterItem item in rptrLanguageDivs.Items) { if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) { var txtLocalizedMetaKeywords = (TextBox)item.FindControl("txtLocalizedMetaKeywords"); var txtLocalizedMetaDescription = (TextBox)item.FindControl("txtLocalizedMetaDescription"); var txtLocalizedMetaTitle = (TextBox)item.FindControl("txtLocalizedMetaTitle"); var txtLocalizedSEName = (TextBox)item.FindControl("txtLocalizedSEName"); var lblLanguageId = (Label)item.FindControl("lblLanguageId"); int languageId = int.Parse(lblLanguageId.Text); string metaKeywords = txtLocalizedMetaKeywords.Text; string metaDescription = txtLocalizedMetaDescription.Text; string metaTitle = txtLocalizedMetaTitle.Text; string seName = txtLocalizedSEName.Text; bool allFieldsAreEmpty = (string.IsNullOrEmpty(metaKeywords) && string.IsNullOrEmpty(metaDescription) && string.IsNullOrEmpty(metaTitle) && string.IsNullOrEmpty(seName)); var content = this.ProductService.GetProductLocalizedByProductIdAndLanguageId(product.ProductId, languageId); if (content == null) { if (!allFieldsAreEmpty && languageId > 0) { //only insert if one of the fields are filled out (avoid too many empty records in db...) content = new ProductLocalized(); content.ProductId = product.ProductId; content.LanguageId = languageId; content.MetaKeywords = metaKeywords; content.MetaDescription = metaDescription; content.MetaTitle = metaTitle; content.SEName = seName; this.ProductService.InsertProductLocalized(content); } } else { if (languageId > 0) { content.LanguageId = languageId; content.MetaKeywords = metaKeywords; content.MetaDescription = metaDescription; content.MetaTitle = metaTitle; content.SEName = seName; this.ProductService.UpdateProductLocalized(content); } } } } }