public void Can_save_and_load_productAttributeCombination() { var combination = new ProductAttributeCombination { AttributesXml = "Some XML", StockQuantity = 2, AllowOutOfStockOrders = true, Sku = "Sku1", ManufacturerPartNumber = "ManufacturerPartNumber1", Gtin = "Gtin1", OverriddenPrice = 0.01M, NotifyAdminForQuantityBelow = 3, Product = GetTestProduct() }; var fromDb = SaveAndLoadEntity(combination); fromDb.ShouldNotBeNull(); fromDb.AttributesXml.ShouldEqual("Some XML"); fromDb.StockQuantity.ShouldEqual(2); fromDb.AllowOutOfStockOrders.ShouldEqual(true); fromDb.Sku.ShouldEqual("Sku1"); fromDb.ManufacturerPartNumber.ShouldEqual("ManufacturerPartNumber1"); fromDb.Gtin.ShouldEqual("Gtin1"); fromDb.OverriddenPrice.ShouldEqual(0.01M); fromDb.NotifyAdminForQuantityBelow.ShouldEqual(3); }
/// <summary> /// Updates a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> public virtual void UpdateProductAttributeCombination(ProductAttributeCombination combination) { if (combination == null) { throw new ArgumentNullException("combination"); } //_productAttributeCombinationRepository.Update(combination); var builder = Builders <Product> .Filter; var filter = builder.Eq(x => x.Id, combination.ProductId); filter = filter & builder.ElemMatch(x => x.ProductAttributeCombinations, y => y.Id == combination.Id); var update = Builders <Product> .Update .Set("ProductAttributeCombinations.$.StockQuantity", combination.StockQuantity) .Set("ProductAttributeCombinations.$.AllowOutOfStockOrders", combination.AllowOutOfStockOrders) .Set("ProductAttributeCombinations.$.Sku", combination.Sku) .Set("ProductAttributeCombinations.$.ManufacturerPartNumber", combination.ManufacturerPartNumber) .Set("ProductAttributeCombinations.$.Gtin", combination.Gtin) .Set("ProductAttributeCombinations.$.OverriddenPrice", combination.OverriddenPrice) .Set("ProductAttributeCombinations.$.NotifyAdminForQuantityBelow", combination.NotifyAdminForQuantityBelow); var result = _productRepository.Collection.UpdateManyAsync(filter, update).Result; //cache _cacheManager.RemoveByPattern(string.Format(PRODUCTS_BY_ID_KEY, combination.ProductId)); //event notification _eventPublisher.EntityUpdated(combination); }
/// <summary> /// Updates a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> public virtual async Task UpdateProductAttributeCombination(ProductAttributeCombination combination) { if (combination == null) { throw new ArgumentNullException("combination"); } var builder = Builders <Product> .Filter; var filter = builder.Eq(x => x.Id, combination.ProductId); filter = filter & builder.ElemMatch(x => x.ProductAttributeCombinations, y => y.Id == combination.Id); var update = Builders <Product> .Update .Set("ProductAttributeCombinations.$.StockQuantity", combination.StockQuantity) .Set("ProductAttributeCombinations.$.AllowOutOfStockOrders", combination.AllowOutOfStockOrders) .Set("ProductAttributeCombinations.$.Sku", combination.Sku) .Set("ProductAttributeCombinations.$.Text", combination.Text) .Set("ProductAttributeCombinations.$.ManufacturerPartNumber", combination.ManufacturerPartNumber) .Set("ProductAttributeCombinations.$.Gtin", combination.Gtin) .Set("ProductAttributeCombinations.$.OverriddenPrice", combination.OverriddenPrice) .Set("ProductAttributeCombinations.$.NotifyAdminForQuantityBelow", combination.NotifyAdminForQuantityBelow) .Set("ProductAttributeCombinations.$.WarehouseInventory", combination.WarehouseInventory) .Set("ProductAttributeCombinations.$.PictureId", combination.PictureId) .Set("ProductAttributeCombinations.$.TierPrices", combination.TierPrices); await _productRepository.Collection.UpdateManyAsync(filter, update); //cache await _cacheManager.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, combination.ProductId)); //event notification await _mediator.EntityUpdated(combination); }
private void SaveNewVariant(VariantData data) { _currentManufacturer = _manufacturers.Where(m => m.Name == data.Brand).FirstOrDefault(); if (_currentManufacturer == null) { var manuturerName = GetAssociatedManufacturerName(data.Brand); _currentManufacturer = _manufacturers.Where(m => m.Name == manuturerName).FirstOrDefault(); if (_currentManufacturer == null) { _logger.Error("SaveVariant, missing manufacturer (" + _updaterName + "): '" + data.Brand + "'"); return; } } ProductAttributeCombination combination = _productAttributeCombinations.Where(p => p.Gtin == data.EAN).FirstOrDefault(); if (combination == null) { // Now we have a new EAN number, lets find out if we also need to create a new product. Product product = _productsSKUAndName.Where(p => p.Sku == data.OrgItemNumber && p.Name == data.OriginalTitle).FirstOrDefault(); if (product == null) { // Only create the product if we have valid data product = CreateProduct(data); _newlyCreatedProducts++; } if (product != null) { _newlyCreatePSC++; throw new NotImplementedException("Mangler kode til at tilføje en ny combination (" + _updaterName + ")"); } } }
/// <summary> /// Sends a "quantity below" notification to a store owner /// </summary> /// <param name="combination">Attribute combination</param> /// <param name="languageId">Message language identifier</param> /// <returns>Queued email identifier</returns> public override int SendQuantityBelowStoreOwnerNotification(ProductAttributeCombination combination, int languageId) { return(SendNotification(0, languageId, "QuantityBelow.AttributeCombination.StoreOwnerNotification", new TokenModel { Product = combination.Product, Combination = combination })); }
public LiquidAttributeCombination(Customer customer, Product product, ProductAttributeCombination combination) { this._customer = customer; this._product = product; this._combination = combination; AdditionalTokens = new Dictionary <string, string>(); }
public void AddAttributeCombinationTokens(LiquidObject liquidObject, ProductAttributeCombination combination, string languageId) { var liquidAttributeCombination = new LiquidAttributeCombination(combination, languageId); liquidObject.AttributeCombination = liquidAttributeCombination; _eventPublisher.EntityTokensAdded(combination, liquidAttributeCombination, liquidObject); }
/// <summary> /// Updates a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> public virtual void UpdateProductAttributeCombination(ProductAttributeCombination combination) { if (combination == null) { throw new ArgumentNullException("combination"); } _productAttributeCombinationRepository.Update(combination); //_unitOfWork.Commit(); }
public LiquidAttributeCombination(ProductAttributeCombination combination, string languageId) { this._workContext = EngineContext.Current.Resolve <IWorkContext>(); this._productAttributeParser = EngineContext.Current.Resolve <IProductAttributeParser>(); this._combination = combination; this._languageId = languageId; this._product = EngineContext.Current.Resolve <IProductService>().GetProductById(combination.ProductId); AdditionalTokens = new Dictionary <string, string>(); }
/// <summary> /// Updates a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> public virtual void UpdateProductAttributeCombination(ProductAttributeCombination combination) { if (combination == null) { throw new ArgumentNullException(nameof(combination)); } _productAttributeCombinationRepository.Update(combination); //event notification _eventPublisher.EntityUpdated(combination); }
public LiquidObjectBuilder AddAttributeCombinationTokens(Product product, ProductAttributeCombination combination) { _chain.Add(async liquidObject => { var liquidAttributeCombination = await _mediator.Send(new GetAttributeCombinationTokensCommand() { Product = product, Combination = combination }); liquidObject.AttributeCombination = liquidAttributeCombination; await _mediator.EntityTokensAdded(combination, liquidAttributeCombination, liquidObject); }); return(this); }
/// <summary> /// Get total quantity /// </summary> /// <param name="product">Product</param> /// <param name="combination">Combination</param> /// <param name="useReservedQuantity"> /// A value indicating whether we should consider "Reserved Quantity" property /// when "multiple warehouses" are used /// </param> /// <param name="warehouseId"> /// Warehouse identifier. Used to limit result to certain warehouse. /// Used only with "multiple warehouses" enabled. /// </param> /// <returns>Result</returns> public static int GetTotalStockQuantityForCombination(this Product product, ProductAttributeCombination combination, bool useReservedQuantity = true, string warehouseId = "") { if (product == null) { throw new ArgumentNullException("product"); } if (combination == null) { throw new ArgumentNullException("combination"); } if (product.ManageInventoryMethod != ManageInventoryMethod.ManageStockByAttributes) { return(0); } if (product.UseMultipleWarehouses) { var pwi = combination.WarehouseInventory; if (!String.IsNullOrEmpty(warehouseId)) { pwi = pwi.Where(x => x.WarehouseId == warehouseId).ToList(); } var result = pwi.Sum(x => x.StockQuantity); if (useReservedQuantity) { result = result - pwi.Sum(x => x.ReservedQuantity); } return(result); } if (string.IsNullOrEmpty(warehouseId) || string.IsNullOrEmpty(product.WarehouseId)) { return(combination.StockQuantity); } else { if (product.WarehouseId == warehouseId) { return(combination.StockQuantity); } else { return(0); } } }
public virtual int GetTotalStockQuantityForCombination(Product product, ProductAttributeCombination combination, bool useReservedQuantity = true, string warehouseId = "") { if (product == null) { throw new ArgumentNullException(nameof(product)); } if (combination == null) { throw new ArgumentNullException(nameof(combination)); } if (product.ManageInventoryMethodId != ManageInventoryMethod.ManageStockByAttributes) { return(0); } if (product.UseMultipleWarehouses) { var pwi = combination.WarehouseInventory.FirstOrDefault(x => x.WarehouseId == warehouseId); if (pwi != null) { var result = pwi.StockQuantity; if (useReservedQuantity) { result -= pwi.ReservedQuantity; } return(result); } return(0); } if (string.IsNullOrEmpty(warehouseId) || string.IsNullOrEmpty(product.WarehouseId)) { return(combination.StockQuantity - (useReservedQuantity ? combination.ReservedQuantity : 0)); } else { if (product.WarehouseId == warehouseId) { return(combination.StockQuantity - (useReservedQuantity ? combination.ReservedQuantity : 0)); } else { return(0); } } }
/// <summary> /// Updates a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> /// <param name="productId">Product ident</param> public virtual async Task UpdateProductAttributeCombination(ProductAttributeCombination combination, string productId) { if (combination == null) { throw new ArgumentNullException(nameof(combination)); } await _productRepository.UpdateToSet(productId, x => x.ProductAttributeCombinations, z => z.Id, combination.Id, combination); //cache await _cacheBase.RemoveByPrefix(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, productId)); //event notification await _mediator.EntityUpdated(combination); }
private void UpdateStock(VariantData data) { ProductAttributeCombination combination = _productAttributeCombinations.Where(p => p.Gtin == data.EAN).FirstOrDefault(); if (combination == null) { _variantDataToBeCreated.Add(data); Thread.Sleep(50); } else { combination.StockQuantity = data.StockCount; _productAttributeService.UpdateProductAttributeCombination(combination); _updatedPSCs++; } }
/// <summary> /// Deletes a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> public virtual async Task DeleteProductAttributeCombination(ProductAttributeCombination combination) { if (combination == null) { throw new ArgumentNullException("combination"); } var updatebuilder = Builders <Product> .Update; var update = updatebuilder.Pull(p => p.ProductAttributeCombinations, combination); await _productRepository.Collection.UpdateOneAsync(new BsonDocument("_id", combination.ProductId), update); //cache _cacheManager.RemoveByPattern(string.Format(PRODUCTS_BY_ID_KEY, combination.ProductId)); //event notification await _eventPublisher.EntityDeleted(combination); }
/// <summary> /// Inserts a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> public virtual async Task InsertProductAttributeCombination(ProductAttributeCombination combination) { if (combination == null) { throw new ArgumentNullException("combination"); } var updatebuilder = Builders <Product> .Update; var update = updatebuilder.AddToSet(p => p.ProductAttributeCombinations, combination); await _productRepository.Collection.UpdateOneAsync(new BsonDocument("_id", combination.ProductId), update); //cache await _cacheManager.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, combination.ProductId)); //event notification await _mediator.EntityInserted(combination); }
/// <summary> /// Updates a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> public virtual void UpdateProductAttributeCombination(ProductAttributeCombination combination) { if (combination == null) { throw new ArgumentNullException(nameof(combination)); } _productAttributeCombinationRepository.Update(combination); //cache _cacheManager.RemoveByPattern(NopCatalogDefaults.ProductAttributesPatternCacheKey); _cacheManager.RemoveByPattern(NopCatalogDefaults.ProductAttributeMappingsPatternCacheKey); _cacheManager.RemoveByPattern(NopCatalogDefaults.ProductAttributeValuesPatternCacheKey); _cacheManager.RemoveByPattern(NopCatalogDefaults.ProductAttributeCombinationsPatternCacheKey); //event notification _eventPublisher.EntityUpdated(combination); }
/// <summary> /// Updates a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> public virtual void UpdateProductAttributeCombination(ProductAttributeCombination combination) { if (combination == null) { throw new ArgumentNullException("combination"); } _productAttributeCombinationRepository.Update(combination); //cache _cacheManager.GetCache(CACHE_NAME_PRODUCTATTRIBUTES).Clear(); _cacheManager.GetCache(CACHE_NAME_PRODUCTATTRIBUTEMAPPINGS).Clear(); _cacheManager.GetCache(CACHE_NAME_PRODUCTATTRIBUTEVALUES).Clear(); _cacheManager.GetCache(CACHE_NAME_PRODUCTATTRIBUTECOMBINATIONS).Clear(); //event notification //_eventPublisher.EntityUpdated(combination); }
/// <summary> /// Updates a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> public virtual void UpdateProductAttributeCombination(ProductAttributeCombination combination) { if (combination == null) { throw new ArgumentNullException("combination"); } _productAttributeCombinationRepository.Update(combination); //cache _cacheManager.RemoveByPattern(PRODUCTATTRIBUTES_PATTERN_KEY); _cacheManager.RemoveByPattern(PRODUCTATTRIBUTEMAPPINGS_PATTERN_KEY); _cacheManager.RemoveByPattern(PRODUCTATTRIBUTEVALUES_PATTERN_KEY); _cacheManager.RemoveByPattern(PRODUCTATTRIBUTECOMBINATIONS_PATTERN_KEY); //event notification _eventPublisher.EntityUpdated(combination); }
/// <summary> /// Deletes a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> /// <param name="productId">Product ident</param> public virtual async Task DeleteProductAttributeCombination(ProductAttributeCombination combination, string productId) { if (combination == null) { throw new ArgumentNullException(nameof(combination)); } var updatebuilder = Builders <Product> .Update; var update = Builders <Product> .Update.PullFilter(p => p.ProductAttributeCombinations, Builders <ProductAttributeCombination> .Filter.Eq(per => per.Id, combination.Id)); var result = await _productRepository.Collection.UpdateOneAsync(new BsonDocument("_id", productId), update); //cache await _cacheBase.RemoveByPrefix(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, productId)); //event notification await _mediator.EntityDeleted(combination); }
/// <summary> /// Inserts a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> public virtual void InsertProductAttributeCombination(ProductAttributeCombination combination) { if (combination == null) { throw new ArgumentNullException(nameof(combination)); } _productAttributeCombinationRepository.Insert(combination); //cache _cacheManager.RemoveByPrefix(NopCatalogDefaults.ProductAttributesPrefixCacheKey); _staticCacheManager.RemoveByPrefix(NopCatalogDefaults.ProductAttributesPrefixCacheKey); _cacheManager.RemoveByPrefix(NopCatalogDefaults.ProductAttributeMappingsPrefixCacheKey); _cacheManager.RemoveByPrefix(NopCatalogDefaults.ProductAttributeValuesPrefixCacheKey); _cacheManager.RemoveByPrefix(NopCatalogDefaults.ProductAttributeCombinationsPrefixCacheKey); //event notification _eventPublisher.EntityInserted(combination); }
public List <AOPresentationOrder> GetCurrentOrders(ref int markedProductId, string searchphrase = "") { _presentationOrders = GetOrders(); if (string.IsNullOrEmpty(searchphrase)) { return(_presentationOrders); } long num = 0; bool isNumber = long.TryParse(searchphrase, out num); if (isNumber) { if (searchphrase.Length <= 8) { _presentationOrders = _presentationOrders.Where(o => o.OrderId == num).ToList(); } else { ProductAttributeCombination productAttributeCombination = GetProductAttributeCombinationByGtin(searchphrase); if (productAttributeCombination != null) { markedProductId = productAttributeCombination.ProductId; _presentationOrders = _presentationOrders.Where(o => o.PresentationOrderItems.Any(p => p.ProductId == productAttributeCombination.ProductId)).ToList(); } else { return(null); } } } else { _presentationOrders = _presentationOrders .Where(o => o.PresentationOrderItems .Any(p => p.ProductName.ToLower().Contains(searchphrase.ToLower()))) .ToList(); } return(_presentationOrders); }
public string GetReference(Product product, ProductAttributeCombination combination) { var reference = product.Sku; if (string.IsNullOrEmpty(reference)) { reference = product.Id.ToString(CultureInfo.InvariantCulture); } if (combination != null) { reference = combination.Sku; if (string.IsNullOrEmpty(reference)) { reference = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", product.Id, combination.Id); } } return(reference); }
/// <summary> /// Updates a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> /// <returns>A task that represents the asynchronous operation</returns> public virtual async Task UpdateProductAttributeCombinationAsync(ProductAttributeCombination combination) { await _productAttributeCombinationRepository.UpdateAsync(combination); }
/// <summary> /// Inserts a product attribute combination /// </summary> /// <param name="combination">Product attribute combination</param> /// <returns>A task that represents the asynchronous operation</returns> public virtual async Task InsertProductAttributeCombinationAsync(ProductAttributeCombination combination) { await _productAttributeCombinationRepository.InsertAsync(combination); }
public async Task AddAttributeCombinationTokens(LiquidObject liquidObject, Product product, ProductAttributeCombination combination) { var liquidAttributeCombination = await _mediator.Send(new GetAttributeCombinationTokensCommand() { Product = product, Combination = combination }); liquidObject.AttributeCombination = liquidAttributeCombination; await _mediator.EntityTokensAdded(combination, liquidAttributeCombination, liquidObject); }
/// <summary> /// Create a copy of product with all depended data /// </summary> /// <param name="product">The product to copy</param> /// <param name="newName">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> /// <param name="copyAssociatedProducts">A value indicating whether the copy associated products</param> /// <returns>Product copy</returns> public virtual Product CopyProduct(Product product, string newName, bool isPublished = true, bool copyImages = true, bool copyAssociatedProducts = true) { if (product == null) { throw new ArgumentNullException("product"); } if (String.IsNullOrEmpty(newName)) { throw new ArgumentException("Product name is required"); } //product download & sample download int downloadId = product.DownloadId; int sampleDownloadId = product.SampleDownloadId; if (product.IsDownload) { var download = _downloadService.GetDownloadById(product.DownloadId); if (download != null) { var downloadCopy = new Download { DownloadGuid = Guid.NewGuid(), UseDownloadUrl = download.UseDownloadUrl, DownloadUrl = download.DownloadUrl, DownloadBinary = download.DownloadBinary, ContentType = download.ContentType, Filename = download.Filename, Extension = download.Extension, IsNew = download.IsNew, }; _downloadService.InsertDownload(downloadCopy); downloadId = downloadCopy.Id; } if (product.HasSampleDownload) { var sampleDownload = _downloadService.GetDownloadById(product.SampleDownloadId); if (sampleDownload != null) { var sampleDownloadCopy = new Download { DownloadGuid = Guid.NewGuid(), UseDownloadUrl = sampleDownload.UseDownloadUrl, DownloadUrl = sampleDownload.DownloadUrl, DownloadBinary = sampleDownload.DownloadBinary, ContentType = sampleDownload.ContentType, Filename = sampleDownload.Filename, Extension = sampleDownload.Extension, IsNew = sampleDownload.IsNew }; _downloadService.InsertDownload(sampleDownloadCopy); sampleDownloadId = sampleDownloadCopy.Id; } } } // product var productCopy = new Product { ProductTypeId = product.ProductTypeId, ParentGroupedProductId = product.ParentGroupedProductId, VisibleIndividually = product.VisibleIndividually, Name = newName, ShortDescription = product.ShortDescription, FullDescription = product.FullDescription, VendorId = product.VendorId, ProductTemplateId = product.ProductTemplateId, AdminComment = product.AdminComment, ShowOnHomePage = product.ShowOnHomePage, MetaKeywords = product.MetaKeywords, MetaDescription = product.MetaDescription, MetaTitle = product.MetaTitle, AllowCustomerReviews = product.AllowCustomerReviews, LimitedToStores = product.LimitedToStores, Sku = product.Sku, ManufacturerPartNumber = product.ManufacturerPartNumber, Gtin = product.Gtin, IsGiftCard = product.IsGiftCard, GiftCardType = product.GiftCardType, OverriddenGiftCardAmount = product.OverriddenGiftCardAmount, RequireOtherProducts = product.RequireOtherProducts, RequiredProductIds = product.RequiredProductIds, AutomaticallyAddRequiredProducts = product.AutomaticallyAddRequiredProducts, IsDownload = product.IsDownload, DownloadId = downloadId, UnlimitedDownloads = product.UnlimitedDownloads, MaxNumberOfDownloads = product.MaxNumberOfDownloads, DownloadExpirationDays = product.DownloadExpirationDays, DownloadActivationType = product.DownloadActivationType, HasSampleDownload = product.HasSampleDownload, SampleDownloadId = sampleDownloadId, HasUserAgreement = product.HasUserAgreement, UserAgreementText = product.UserAgreementText, IsRecurring = product.IsRecurring, RecurringCycleLength = product.RecurringCycleLength, RecurringCyclePeriod = product.RecurringCyclePeriod, RecurringTotalCycles = product.RecurringTotalCycles, IsRental = product.IsRental, RentalPriceLength = product.RentalPriceLength, RentalPricePeriod = product.RentalPricePeriod, IsShipEnabled = product.IsShipEnabled, IsFreeShipping = product.IsFreeShipping, ShipSeparately = product.ShipSeparately, AdditionalShippingCharge = product.AdditionalShippingCharge, DeliveryDateId = product.DeliveryDateId, IsTaxExempt = product.IsTaxExempt, TaxCategoryId = product.TaxCategoryId, IsTelecommunicationsOrBroadcastingOrElectronicServices = product.IsTelecommunicationsOrBroadcastingOrElectronicServices, ManageInventoryMethod = product.ManageInventoryMethod, ProductAvailabilityRangeId = product.ProductAvailabilityRangeId, UseMultipleWarehouses = product.UseMultipleWarehouses, WarehouseId = product.WarehouseId, StockQuantity = product.StockQuantity, DisplayStockAvailability = product.DisplayStockAvailability, DisplayStockQuantity = product.DisplayStockQuantity, MinStockQuantity = product.MinStockQuantity, LowStockActivityId = product.LowStockActivityId, NotifyAdminForQuantityBelow = product.NotifyAdminForQuantityBelow, BackorderMode = product.BackorderMode, AllowBackInStockSubscriptions = product.AllowBackInStockSubscriptions, OrderMinimumQuantity = product.OrderMinimumQuantity, OrderMaximumQuantity = product.OrderMaximumQuantity, AllowedQuantities = product.AllowedQuantities, AllowAddingOnlyExistingAttributeCombinations = product.AllowAddingOnlyExistingAttributeCombinations, NotReturnable = product.NotReturnable, DisableBuyButton = product.DisableBuyButton, DisableWishlistButton = product.DisableWishlistButton, AvailableForPreOrder = product.AvailableForPreOrder, PreOrderAvailabilityStartDateTimeUtc = product.PreOrderAvailabilityStartDateTimeUtc, CallForPrice = product.CallForPrice, Price = product.Price, OldPrice = product.OldPrice, ProductCost = product.ProductCost, SpecialPrice = product.SpecialPrice, SpecialPriceStartDateTimeUtc = product.SpecialPriceStartDateTimeUtc, SpecialPriceEndDateTimeUtc = product.SpecialPriceEndDateTimeUtc, CustomerEntersPrice = product.CustomerEntersPrice, MinimumCustomerEnteredPrice = product.MinimumCustomerEnteredPrice, MaximumCustomerEnteredPrice = product.MaximumCustomerEnteredPrice, BasepriceEnabled = product.BasepriceEnabled, BasepriceAmount = product.BasepriceAmount, BasepriceUnitId = product.BasepriceUnitId, BasepriceBaseAmount = product.BasepriceBaseAmount, BasepriceBaseUnitId = product.BasepriceBaseUnitId, MarkAsNew = product.MarkAsNew, MarkAsNewStartDateTimeUtc = product.MarkAsNewStartDateTimeUtc, MarkAsNewEndDateTimeUtc = product.MarkAsNewEndDateTimeUtc, Weight = product.Weight, Length = product.Length, Width = product.Width, Height = product.Height, AvailableStartDateTimeUtc = product.AvailableStartDateTimeUtc, AvailableEndDateTimeUtc = product.AvailableEndDateTimeUtc, DisplayOrder = product.DisplayOrder, Published = isPublished, Deleted = product.Deleted, CreatedOnUtc = DateTime.UtcNow, UpdatedOnUtc = DateTime.UtcNow }; //validate search engine name _productService.InsertProduct(productCopy); //search engine name _urlRecordService.SaveSlug(productCopy, productCopy.ValidateSeName("", productCopy.Name, true), 0); var languages = _languageService.GetAllLanguages(true); //localization foreach (var lang in languages) { var name = product.GetLocalized(x => x.Name, lang.Id, false, false); if (!String.IsNullOrEmpty(name)) { _localizedEntityService.SaveLocalizedValue(productCopy, x => x.Name, name, lang.Id); } var shortDescription = product.GetLocalized(x => x.ShortDescription, lang.Id, false, false); if (!String.IsNullOrEmpty(shortDescription)) { _localizedEntityService.SaveLocalizedValue(productCopy, x => x.ShortDescription, shortDescription, lang.Id); } var fullDescription = product.GetLocalized(x => x.FullDescription, lang.Id, false, false); if (!String.IsNullOrEmpty(fullDescription)) { _localizedEntityService.SaveLocalizedValue(productCopy, x => x.FullDescription, fullDescription, lang.Id); } var metaKeywords = product.GetLocalized(x => x.MetaKeywords, lang.Id, false, false); if (!String.IsNullOrEmpty(metaKeywords)) { _localizedEntityService.SaveLocalizedValue(productCopy, x => x.MetaKeywords, metaKeywords, lang.Id); } var metaDescription = product.GetLocalized(x => x.MetaDescription, lang.Id, false, false); if (!String.IsNullOrEmpty(metaDescription)) { _localizedEntityService.SaveLocalizedValue(productCopy, x => x.MetaDescription, metaDescription, lang.Id); } var metaTitle = product.GetLocalized(x => x.MetaTitle, lang.Id, false, false); if (!String.IsNullOrEmpty(metaTitle)) { _localizedEntityService.SaveLocalizedValue(productCopy, x => x.MetaTitle, metaTitle, lang.Id); } //search engine name _urlRecordService.SaveSlug(productCopy, productCopy.ValidateSeName("", name, false), lang.Id); } //product tags foreach (var productTag in product.ProductTags) { productCopy.ProductTags.Add(productTag); } _productService.UpdateProduct(productCopy); //product pictures //variable to store original and new picture identifiers var originalNewPictureIdentifiers = new Dictionary <int, int>(); if (copyImages) { foreach (var productPicture in product.ProductPictures) { var picture = productPicture.Picture; var pictureCopy = _pictureService.InsertPicture( _pictureService.LoadPictureBinary(picture), picture.MimeType, _pictureService.GetPictureSeName(newName), picture.AltAttribute, picture.TitleAttribute); _productService.InsertProductPicture(new ProductPicture { ProductId = productCopy.Id, PictureId = pictureCopy.Id, DisplayOrder = productPicture.DisplayOrder }); originalNewPictureIdentifiers.Add(picture.Id, pictureCopy.Id); } } // product <-> warehouses mappings foreach (var pwi in product.ProductWarehouseInventory) { var pwiCopy = new ProductWarehouseInventory { ProductId = productCopy.Id, WarehouseId = pwi.WarehouseId, StockQuantity = pwi.StockQuantity, ReservedQuantity = 0, }; productCopy.ProductWarehouseInventory.Add(pwiCopy); } _productService.UpdateProduct(productCopy); // product <-> categories mappings foreach (var productCategory in product.ProductCategories) { var productCategoryCopy = new ProductCategory { ProductId = productCopy.Id, CategoryId = productCategory.CategoryId, IsFeaturedProduct = productCategory.IsFeaturedProduct, DisplayOrder = productCategory.DisplayOrder }; _categoryService.InsertProductCategory(productCategoryCopy); } // product <-> manufacturers mappings foreach (var productManufacturers in product.ProductManufacturers) { var productManufacturerCopy = new ProductManufacturer { ProductId = productCopy.Id, ManufacturerId = productManufacturers.ManufacturerId, IsFeaturedProduct = productManufacturers.IsFeaturedProduct, DisplayOrder = productManufacturers.DisplayOrder }; _manufacturerService.InsertProductManufacturer(productManufacturerCopy); } // product <-> releated products mappings foreach (var relatedProduct in _productService.GetRelatedProductsByProductId1(product.Id, true)) { _productService.InsertRelatedProduct( new RelatedProduct { ProductId1 = productCopy.Id, ProductId2 = relatedProduct.ProductId2, DisplayOrder = relatedProduct.DisplayOrder }); } // product <-> cross sells mappings foreach (var csProduct in _productService.GetCrossSellProductsByProductId1(product.Id, true)) { _productService.InsertCrossSellProduct( new CrossSellProduct { ProductId1 = productCopy.Id, ProductId2 = csProduct.ProductId2, }); } // product specifications foreach (var productSpecificationAttribute in product.ProductSpecificationAttributes) { var psaCopy = new ProductSpecificationAttribute { ProductId = productCopy.Id, AttributeTypeId = productSpecificationAttribute.AttributeTypeId, SpecificationAttributeOptionId = productSpecificationAttribute.SpecificationAttributeOptionId, CustomValue = productSpecificationAttribute.CustomValue, AllowFiltering = productSpecificationAttribute.AllowFiltering, ShowOnProductPage = productSpecificationAttribute.ShowOnProductPage, DisplayOrder = productSpecificationAttribute.DisplayOrder }; _specificationAttributeService.InsertProductSpecificationAttribute(psaCopy); } //store mapping var selectedStoreIds = _storeMappingService.GetStoresIdsWithAccess(product); foreach (var id in selectedStoreIds) { _storeMappingService.InsertStoreMapping(productCopy, id); } // product <-> attributes mappings var associatedAttributes = new Dictionary <int, int>(); var associatedAttributeValues = new Dictionary <int, int>(); foreach (var productAttributeMapping in _productAttributeService.GetProductAttributeMappingsByProductId(product.Id)) { var productAttributeMappingCopy = new ProductAttributeMapping { ProductId = productCopy.Id, ProductAttributeId = productAttributeMapping.ProductAttributeId, TextPrompt = productAttributeMapping.TextPrompt, IsRequired = productAttributeMapping.IsRequired, AttributeControlTypeId = productAttributeMapping.AttributeControlTypeId, DisplayOrder = productAttributeMapping.DisplayOrder, ValidationMinLength = productAttributeMapping.ValidationMinLength, ValidationMaxLength = productAttributeMapping.ValidationMaxLength, ValidationFileAllowedExtensions = productAttributeMapping.ValidationFileAllowedExtensions, ValidationFileMaximumSize = productAttributeMapping.ValidationFileMaximumSize, DefaultValue = productAttributeMapping.DefaultValue, //UNDONE copy ConditionAttributeXml (we should replace attribute IDs with new values) }; _productAttributeService.InsertProductAttributeMapping(productAttributeMappingCopy); //save associated value (used for combinations copying) associatedAttributes.Add(productAttributeMapping.Id, productAttributeMappingCopy.Id); // product attribute values var productAttributeValues = _productAttributeService.GetProductAttributeValues(productAttributeMapping.Id); foreach (var productAttributeValue in productAttributeValues) { int attributeValuePictureId = 0; if (originalNewPictureIdentifiers.ContainsKey(productAttributeValue.PictureId)) { attributeValuePictureId = originalNewPictureIdentifiers[productAttributeValue.PictureId]; } var attributeValueCopy = new ProductAttributeValue { ProductAttributeMappingId = productAttributeMappingCopy.Id, AttributeValueTypeId = productAttributeValue.AttributeValueTypeId, AssociatedProductId = productAttributeValue.AssociatedProductId, Name = productAttributeValue.Name, ColorSquaresRgb = productAttributeValue.ColorSquaresRgb, PriceAdjustment = productAttributeValue.PriceAdjustment, WeightAdjustment = productAttributeValue.WeightAdjustment, Cost = productAttributeValue.Cost, CustomerEntersQty = productAttributeValue.CustomerEntersQty, Quantity = productAttributeValue.Quantity, IsPreSelected = productAttributeValue.IsPreSelected, DisplayOrder = productAttributeValue.DisplayOrder, PictureId = attributeValuePictureId, }; //picture associated to "iamge square" attribute type (if exists) if (productAttributeValue.ImageSquaresPictureId > 0) { var origImageSquaresPicture = _pictureService.GetPictureById(productAttributeValue.ImageSquaresPictureId); if (origImageSquaresPicture != null) { //copy the picture var imageSquaresPictureCopy = _pictureService.InsertPicture( _pictureService.LoadPictureBinary(origImageSquaresPicture), origImageSquaresPicture.MimeType, origImageSquaresPicture.SeoFilename, origImageSquaresPicture.AltAttribute, origImageSquaresPicture.TitleAttribute); attributeValueCopy.ImageSquaresPictureId = imageSquaresPictureCopy.Id; } } _productAttributeService.InsertProductAttributeValue(attributeValueCopy); //save associated value (used for combinations copying) associatedAttributeValues.Add(productAttributeValue.Id, attributeValueCopy.Id); //localization foreach (var lang in languages) { var name = productAttributeValue.GetLocalized(x => x.Name, lang.Id, false, false); if (!String.IsNullOrEmpty(name)) { _localizedEntityService.SaveLocalizedValue(attributeValueCopy, x => x.Name, name, lang.Id); } } } } //attribute combinations foreach (var combination in _productAttributeService.GetAllProductAttributeCombinations(product.Id)) { //generate new AttributesXml according to new value IDs string newAttributesXml = ""; var parsedProductAttributes = _productAttributeParser.ParseProductAttributeMappings(combination.AttributesXml); foreach (var oldAttribute in parsedProductAttributes) { if (associatedAttributes.ContainsKey(oldAttribute.Id)) { var newAttribute = _productAttributeService.GetProductAttributeMappingById(associatedAttributes[oldAttribute.Id]); if (newAttribute != null) { var oldAttributeValuesStr = _productAttributeParser.ParseValues(combination.AttributesXml, oldAttribute.Id); foreach (var oldAttributeValueStr in oldAttributeValuesStr) { if (newAttribute.ShouldHaveValues()) { //attribute values int oldAttributeValue = int.Parse(oldAttributeValueStr); if (associatedAttributeValues.ContainsKey(oldAttributeValue)) { var newAttributeValue = _productAttributeService.GetProductAttributeValueById(associatedAttributeValues[oldAttributeValue]); if (newAttributeValue != null) { newAttributesXml = _productAttributeParser.AddProductAttribute(newAttributesXml, newAttribute, newAttributeValue.Id.ToString()); } } } else { //just a text newAttributesXml = _productAttributeParser.AddProductAttribute(newAttributesXml, newAttribute, oldAttributeValueStr); } } } } } var combinationCopy = new ProductAttributeCombination { ProductId = productCopy.Id, AttributesXml = newAttributesXml, StockQuantity = combination.StockQuantity, AllowOutOfStockOrders = combination.AllowOutOfStockOrders, Sku = combination.Sku, ManufacturerPartNumber = combination.ManufacturerPartNumber, Gtin = combination.Gtin, OverriddenPrice = combination.OverriddenPrice, NotifyAdminForQuantityBelow = combination.NotifyAdminForQuantityBelow }; _productAttributeService.InsertProductAttributeCombination(combinationCopy); } //tier prices foreach (var tierPrice in product.TierPrices) { _productService.InsertTierPrice( new TierPrice { ProductId = productCopy.Id, StoreId = tierPrice.StoreId, CustomerRoleId = tierPrice.CustomerRoleId, Quantity = tierPrice.Quantity, Price = tierPrice.Price }); } // product <-> discounts mapping foreach (var discount in product.AppliedDiscounts) { productCopy.AppliedDiscounts.Add(discount); _productService.UpdateProduct(productCopy); } //update "HasTierPrices" and "HasDiscountsApplied" properties _productService.UpdateHasTierPricesProperty(productCopy); _productService.UpdateHasDiscountsApplied(productCopy); //associated products if (copyAssociatedProducts) { var associatedProducts = _productService.GetAssociatedProducts(product.Id, showHidden: true); foreach (var associatedProduct in associatedProducts) { var associatedProductCopy = CopyProduct(associatedProduct, string.Format("Copy of {0}", associatedProduct.Name), isPublished, copyImages, false); associatedProductCopy.ParentGroupedProductId = productCopy.Id; _productService.UpdateProduct(productCopy); } } return(productCopy); }
/// <summary> /// Copy attributes mapping /// </summary> /// <param name="product">Product</param> /// <param name="productCopy">New product</param> /// <param name="originalNewPictureIdentifiers">Identifiers of pictures</param> protected virtual void CopyAttributesMapping(Product product, Product productCopy, Dictionary <int, int> originalNewPictureIdentifiers) { var associatedAttributes = new Dictionary <int, int>(); var associatedAttributeValues = new Dictionary <int, int>(); //attribute mapping with condition attributes var oldCopyWithConditionAttributes = new List <ProductAttributeMapping>(); //all product attribute mapping copies var productAttributeMappingCopies = new Dictionary <int, ProductAttributeMapping>(); var languages = _languageService.GetAllLanguages(true); foreach (var productAttributeMapping in _productAttributeService.GetProductAttributeMappingsByProductId(product.Id)) { var productAttributeMappingCopy = new ProductAttributeMapping { ProductId = productCopy.Id, ProductAttributeId = productAttributeMapping.ProductAttributeId, TextPrompt = productAttributeMapping.TextPrompt, IsRequired = productAttributeMapping.IsRequired, AttributeControlTypeId = productAttributeMapping.AttributeControlTypeId, DisplayOrder = productAttributeMapping.DisplayOrder, ValidationMinLength = productAttributeMapping.ValidationMinLength, ValidationMaxLength = productAttributeMapping.ValidationMaxLength, ValidationFileAllowedExtensions = productAttributeMapping.ValidationFileAllowedExtensions, ValidationFileMaximumSize = productAttributeMapping.ValidationFileMaximumSize, DefaultValue = productAttributeMapping.DefaultValue }; _productAttributeService.InsertProductAttributeMapping(productAttributeMappingCopy); //localization foreach (var lang in languages) { var textPrompt = _localizationService.GetLocalized(productAttributeMapping, x => x.TextPrompt, lang.Id, false, false); if (!string.IsNullOrEmpty(textPrompt)) { _localizedEntityService.SaveLocalizedValue(productAttributeMappingCopy, x => x.TextPrompt, textPrompt, lang.Id); } } productAttributeMappingCopies.Add(productAttributeMappingCopy.Id, productAttributeMappingCopy); if (!string.IsNullOrEmpty(productAttributeMapping.ConditionAttributeXml)) { oldCopyWithConditionAttributes.Add(productAttributeMapping); } //save associated value (used for combinations copying) associatedAttributes.Add(productAttributeMapping.Id, productAttributeMappingCopy.Id); // product attribute values var productAttributeValues = _productAttributeService.GetProductAttributeValues(productAttributeMapping.Id); foreach (var productAttributeValue in productAttributeValues) { var attributeValuePictureId = 0; if (originalNewPictureIdentifiers.ContainsKey(productAttributeValue.PictureId)) { attributeValuePictureId = originalNewPictureIdentifiers[productAttributeValue.PictureId]; } var attributeValueCopy = new ProductAttributeValue { ProductAttributeMappingId = productAttributeMappingCopy.Id, AttributeValueTypeId = productAttributeValue.AttributeValueTypeId, AssociatedProductId = productAttributeValue.AssociatedProductId, Name = productAttributeValue.Name, ColorSquaresRgb = productAttributeValue.ColorSquaresRgb, PriceAdjustment = productAttributeValue.PriceAdjustment, PriceAdjustmentUsePercentage = productAttributeValue.PriceAdjustmentUsePercentage, WeightAdjustment = productAttributeValue.WeightAdjustment, Cost = productAttributeValue.Cost, CustomerEntersQty = productAttributeValue.CustomerEntersQty, Quantity = productAttributeValue.Quantity, IsPreSelected = productAttributeValue.IsPreSelected, DisplayOrder = productAttributeValue.DisplayOrder, PictureId = attributeValuePictureId, }; //picture associated to "iamge square" attribute type (if exists) if (productAttributeValue.ImageSquaresPictureId > 0) { var origImageSquaresPicture = _pictureService.GetPictureById(productAttributeValue.ImageSquaresPictureId); if (origImageSquaresPicture != null) { //copy the picture var imageSquaresPictureCopy = _pictureService.InsertPicture( _pictureService.LoadPictureBinary(origImageSquaresPicture), origImageSquaresPicture.MimeType, origImageSquaresPicture.SeoFilename, origImageSquaresPicture.AltAttribute, origImageSquaresPicture.TitleAttribute); attributeValueCopy.ImageSquaresPictureId = imageSquaresPictureCopy.Id; } } _productAttributeService.InsertProductAttributeValue(attributeValueCopy); //save associated value (used for combinations copying) associatedAttributeValues.Add(productAttributeValue.Id, attributeValueCopy.Id); //localization foreach (var lang in languages) { var name = _localizationService.GetLocalized(productAttributeValue, x => x.Name, lang.Id, false, false); if (!string.IsNullOrEmpty(name)) { _localizedEntityService.SaveLocalizedValue(attributeValueCopy, x => x.Name, name, lang.Id); } } } } //copy attribute conditions foreach (var productAttributeMapping in oldCopyWithConditionAttributes) { var oldConditionAttributeMapping = _productAttributeParser .ParseProductAttributeMappings(productAttributeMapping.ConditionAttributeXml).FirstOrDefault(); if (oldConditionAttributeMapping == null) { continue; } var oldConditionValues = _productAttributeParser.ParseProductAttributeValues(productAttributeMapping.ConditionAttributeXml, oldConditionAttributeMapping.Id); if (!oldConditionValues.Any()) { continue; } var newAttributeMappingId = associatedAttributes[oldConditionAttributeMapping.Id]; var newConditionAttributeMapping = productAttributeMappingCopies[newAttributeMappingId]; var newConditionAttributeXml = string.Empty; foreach (var oldConditionValue in oldConditionValues) { newConditionAttributeXml = _productAttributeParser.AddProductAttribute(newConditionAttributeXml, newConditionAttributeMapping, associatedAttributeValues[oldConditionValue.Id].ToString()); } var attributeMappingId = associatedAttributes[productAttributeMapping.Id]; var conditionAttribute = productAttributeMappingCopies[attributeMappingId]; conditionAttribute.ConditionAttributeXml = newConditionAttributeXml; _productAttributeService.UpdateProductAttributeMapping(conditionAttribute); } //attribute combinations foreach (var combination in _productAttributeService.GetAllProductAttributeCombinations(product.Id)) { //generate new AttributesXml according to new value IDs var newAttributesXml = string.Empty; var parsedProductAttributes = _productAttributeParser.ParseProductAttributeMappings(combination.AttributesXml); foreach (var oldAttribute in parsedProductAttributes) { if (!associatedAttributes.ContainsKey(oldAttribute.Id)) { continue; } var newAttribute = _productAttributeService.GetProductAttributeMappingById(associatedAttributes[oldAttribute.Id]); if (newAttribute == null) { continue; } var oldAttributeValuesStr = _productAttributeParser.ParseValues(combination.AttributesXml, oldAttribute.Id); foreach (var oldAttributeValueStr in oldAttributeValuesStr) { if (newAttribute.ShouldHaveValues()) { //attribute values var oldAttributeValue = int.Parse(oldAttributeValueStr); if (!associatedAttributeValues.ContainsKey(oldAttributeValue)) { continue; } var newAttributeValue = _productAttributeService.GetProductAttributeValueById(associatedAttributeValues[oldAttributeValue]); if (newAttributeValue != null) { newAttributesXml = _productAttributeParser.AddProductAttribute(newAttributesXml, newAttribute, newAttributeValue.Id.ToString()); } } else { //just a text newAttributesXml = _productAttributeParser.AddProductAttribute(newAttributesXml, newAttribute, oldAttributeValueStr); } } } //picture originalNewPictureIdentifiers.TryGetValue(combination.PictureId, out var combinationPictureId); var combinationCopy = new ProductAttributeCombination { ProductId = productCopy.Id, AttributesXml = newAttributesXml, StockQuantity = combination.StockQuantity, AllowOutOfStockOrders = combination.AllowOutOfStockOrders, Sku = combination.Sku, ManufacturerPartNumber = combination.ManufacturerPartNumber, Gtin = combination.Gtin, OverriddenPrice = combination.OverriddenPrice, NotifyAdminForQuantityBelow = combination.NotifyAdminForQuantityBelow, PictureId = combinationPictureId }; _productAttributeService.InsertProductAttributeCombination(combinationCopy); //quantity change history _productService.AddStockQuantityHistoryEntry(productCopy, combination.StockQuantity, combination.StockQuantity, message: string.Format(_localizationService.GetResource("Admin.StockQuantityHistory.Messages.CopyProduct"), product.Id), combinationId: combination.Id); } }
/// <summary> /// Unblocks the given quantity reserved items in the warehouses /// </summary> /// <param name="product">Product</param> /// <param name="quantity">Quantity, must be positive</param> public virtual async Task UnblockReservedInventoryCombination(Product product, ProductAttributeCombination combination, int quantity, string warehouseId) { if (product == null) { throw new ArgumentNullException("product"); } if (quantity < 0) { throw new ArgumentException("Value must be positive.", "quantity"); } var productInventory = combination.WarehouseInventory .OrderByDescending(pwi => pwi.StockQuantity - pwi.ReservedQuantity) .ToList(); if (productInventory.Count <= 0) { return; } var qty = quantity; foreach (var item in productInventory.Where(x => x.WarehouseId == warehouseId || string.IsNullOrEmpty(warehouseId))) { var selectQty = Math.Min(item.ReservedQuantity, qty); if (qty - selectQty < 0) { break; } item.ReservedQuantity -= selectQty; qty -= selectQty; if (qty <= 0) { break; } } if (qty > 0) { var pwi = productInventory[0]; pwi.StockQuantity += qty; } combination.StockQuantity = combination.WarehouseInventory.Sum(x => x.StockQuantity); var builder = Builders <Product> .Filter; var filter = builder.Eq(x => x.Id, combination.ProductId); filter = filter & builder.ElemMatch(x => x.ProductAttributeCombinations, y => y.Id == combination.Id); var update = Builders <Product> .Update .Set("ProductAttributeCombinations.$.StockQuantity", combination.StockQuantity) .Set("ProductAttributeCombinations.$.WarehouseInventory", combination.WarehouseInventory) .CurrentDate("UpdatedOnUtc"); await _productRepository.Collection.UpdateManyAsync(filter, update); //cache await _cacheManager.RemoveAsync(string.Format(PRODUCTS_BY_ID_KEY, product.Id)); //event notification await _mediator.EntityUpdated(product); }