public void Can_save_and_load_productAttribute()
        {
            var pa = new ProductAttribute
            {
                Name = "Name 1",
                Description = "Description 1",
            };

            var fromDb = SaveAndLoadEntity(pa);
            fromDb.ShouldNotBeNull();
            fromDb.Name.ShouldEqual("Name 1");
            fromDb.Description.ShouldEqual("Description 1");
        }
        /// <summary>
        /// Deletes a product attribute
        /// </summary>
        /// <param name="productAttribute">Product attribute</param>
        public virtual void DeleteProductAttribute(ProductAttribute productAttribute)
        {
            if (productAttribute == null)
                throw new ArgumentNullException("productAttribute");

            _productAttributeRepository.Delete(productAttribute);

            //cache
            _cacheManager.RemoveByPattern(PRODUCTATTRIBUTES_PATTERN_KEY);
            _cacheManager.RemoveByPattern(PRODUCTVARIANTATTRIBUTES_PATTERN_KEY);
            _cacheManager.RemoveByPattern(PRODUCTVARIANTATTRIBUTEVALUES_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(productAttribute);
        }
        protected virtual void UpdateLocales(ProductAttribute productAttribute, ProductAttributeModel model)
        {
            foreach (var localized in model.Locales)
            {
                _localizedEntityService.SaveLocalizedValue(productAttribute,
                                                               x => x.Name,
                                                               localized.Name,
                                                               localized.LanguageId);

                _localizedEntityService.SaveLocalizedValue(productAttribute,
                                                           x => x.Description,
                                                           localized.Description,
                                                           localized.LanguageId);
            }
        }
示例#4
0
        /// <summary>
        /// Inserts a product attribute
        /// </summary>
        /// <param name="productAttribute">Product attribute</param>
        public virtual void InsertProductAttribute(ProductAttribute productAttribute)
        {
            if (productAttribute == null)
                throw new ArgumentNullException("productAttribute");

            _productAttributeRepository.Insert(productAttribute);

            //cache
            _cacheManager.RemoveByPattern(PRODUCTATTRIBUTES_PATTERN_KEY);
            _cacheManager.RemoveByPattern(PRODUCTATTRIBUTEMAPPINGS_PATTERN_KEY);
            _cacheManager.RemoveByPattern(PRODUCTATTRIBUTEVALUES_PATTERN_KEY);
            _cacheManager.RemoveByPattern(PRODUCTATTRIBUTECOMBINATIONS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(productAttribute);
        }
示例#5
0
 public static ProductAttribute ToEntity(this ProductAttributeModel model, ProductAttribute destination)
 {
     return Mapper.Map(model, destination);
 }
        public new void SetUp()
        {
            #region Test data

            //color (dropdownlist)
            pa1 = new ProductAttribute
            {
                Id = 1,
                Name = "Color",
            };
            pam1_1 = new ProductAttributeMapping
            {
                Id = 11,
                ProductId = 1,
                TextPrompt = "Select color:",
                IsRequired = true,
                AttributeControlType = AttributeControlType.DropdownList,
                DisplayOrder = 1,
                ProductAttribute = pa1,
                ProductAttributeId = pa1.Id
            };
            pav1_1 = new ProductAttributeValue
            {
                Id = 11,
                Name = "Green",
                DisplayOrder = 1,
                ProductAttributeMapping= pam1_1,
                ProductAttributeMappingId = pam1_1.Id
            };
            pav1_2 = new ProductAttributeValue
            {
                Id = 12,
                Name = "Red",
                DisplayOrder = 2,
                ProductAttributeMapping = pam1_1,
                ProductAttributeMappingId = pam1_1.Id
            };
            pam1_1.ProductAttributeValues.Add(pav1_1);
            pam1_1.ProductAttributeValues.Add(pav1_2);

            //custom option (checkboxes)
            pa2 = new ProductAttribute
            {
                Id = 2,
                Name = "Some custom option",
            };
            pam2_1 = new ProductAttributeMapping
            {
                Id = 21,
                ProductId = 1,
                TextPrompt = "Select at least one option:",
                IsRequired = true,
                AttributeControlType = AttributeControlType.Checkboxes,
                DisplayOrder = 2,
                ProductAttribute = pa2,
                ProductAttributeId = pa2.Id
            };
            pav2_1 = new ProductAttributeValue
            {
                Id = 21,
                Name = "Option 1",
                DisplayOrder = 1,
                ProductAttributeMapping = pam2_1,
                ProductAttributeMappingId = pam2_1.Id
            };
            pav2_2 = new ProductAttributeValue
            {
                Id = 22,
                Name = "Option 2",
                DisplayOrder = 2,
                ProductAttributeMapping = pam2_1,
                ProductAttributeMappingId = pam2_1.Id
            };
            pam2_1.ProductAttributeValues.Add(pav2_1);
            pam2_1.ProductAttributeValues.Add(pav2_2);

            //custom text
            pa3 = new ProductAttribute
            {
                Id = 3,
                Name = "Custom text",
            };
            pam3_1 = new ProductAttributeMapping
            {
                Id = 31,
                ProductId = 1,
                TextPrompt = "Enter custom text:",
                IsRequired = true,
                AttributeControlType = AttributeControlType.TextBox,
                DisplayOrder = 1,
                ProductAttribute = pa1,
                ProductAttributeId = pa3.Id
            };

            #endregion

            _productAttributeRepo = MockRepository.GenerateMock<IRepository<ProductAttribute>>();
            _productAttributeRepo.Expect(x => x.Table).Return(new List<ProductAttribute> { pa1, pa2, pa3 }.AsQueryable());
            _productAttributeRepo.Expect(x => x.GetById(pa1.Id)).Return(pa1);
            _productAttributeRepo.Expect(x => x.GetById(pa2.Id)).Return(pa2);
            _productAttributeRepo.Expect(x => x.GetById(pa3.Id)).Return(pa3);

            _productAttributeMappingRepo = MockRepository.GenerateMock<IRepository<ProductAttributeMapping>>();
            _productAttributeMappingRepo.Expect(x => x.Table).Return(new List<ProductAttributeMapping> { pam1_1, pam2_1, pam3_1 }.AsQueryable());
            _productAttributeMappingRepo.Expect(x => x.GetById(pam1_1.Id)).Return(pam1_1);
            _productAttributeMappingRepo.Expect(x => x.GetById(pam2_1.Id)).Return(pam2_1);
            _productAttributeMappingRepo.Expect(x => x.GetById(pam3_1.Id)).Return(pam3_1);

            _productAttributeCombinationRepo = MockRepository.GenerateMock<IRepository<ProductAttributeCombination>>();
            _productAttributeCombinationRepo.Expect(x => x.Table).Return(new List<ProductAttributeCombination>().AsQueryable());

            _productAttributeValueRepo = MockRepository.GenerateMock<IRepository<ProductAttributeValue>>();
            _productAttributeValueRepo.Expect(x => x.Table).Return(new List<ProductAttributeValue> { pav1_1, pav1_2, pav2_1, pav2_2 }.AsQueryable());
            _productAttributeValueRepo.Expect(x => x.GetById(pav1_1.Id)).Return(pav1_1);
            _productAttributeValueRepo.Expect(x => x.GetById(pav1_2.Id)).Return(pav1_2);
            _productAttributeValueRepo.Expect(x => x.GetById(pav2_1.Id)).Return(pav2_1);
            _productAttributeValueRepo.Expect(x => x.GetById(pav2_2.Id)).Return(pav2_2);

            _eventPublisher = MockRepository.GenerateMock<IEventPublisher>();
            _eventPublisher.Expect(x => x.Publish(Arg<object>.Is.Anything));

            var cacheManager = new NopNullCache();

            _productAttributeService = new ProductAttributeService(cacheManager,
                _productAttributeRepo,
                _productAttributeMappingRepo,
                _productAttributeCombinationRepo,
                _productAttributeValueRepo,
                _eventPublisher);

            _productAttributeParser = new ProductAttributeParser(_productAttributeService);

            _priceCalculationService = MockRepository.GenerateMock<IPriceCalculationService>();

            var workingLanguage = new Language();
            _workContext = MockRepository.GenerateMock<IWorkContext>();
            _workContext.Expect(x => x.WorkingLanguage).Return(workingLanguage);
            _currencyService = MockRepository.GenerateMock<ICurrencyService>();
            _localizationService = MockRepository.GenerateMock<ILocalizationService>();
            _localizationService.Expect(x => x.GetResource("GiftCardAttribute.For.Virtual")).Return("For: {0} <{1}>");
            _localizationService.Expect(x => x.GetResource("GiftCardAttribute.From.Virtual")).Return("From: {0} <{1}>");
            _localizationService.Expect(x => x.GetResource("GiftCardAttribute.For.Physical")).Return("For: {0}");
            _localizationService.Expect(x => x.GetResource("GiftCardAttribute.From.Physical")).Return("From: {0}");
            _taxService = MockRepository.GenerateMock<ITaxService>();
            _priceFormatter = MockRepository.GenerateMock<IPriceFormatter>();
            _downloadService = MockRepository.GenerateMock<IDownloadService>();
            _webHelper = MockRepository.GenerateMock<IWebHelper>();
            _shoppingCartSettings = MockRepository.GenerateMock<ShoppingCartSettings>();

            _productAttributeFormatter = new ProductAttributeFormatter(_workContext,
                _productAttributeService,
                _productAttributeParser,
                _currencyService,
                _localizationService,
                _taxService,
                _priceFormatter,
                _downloadService,
                _webHelper,
                _priceCalculationService,
                _shoppingCartSettings);
        }
        protected virtual List<LocalizedProperty> UpdateLocales(ProductAttribute productAttribute, ProductAttributeModel model)
        {
            List<LocalizedProperty> localized = new List<LocalizedProperty>();
            foreach (var local in model.Locales)
            {
                if(!(String.IsNullOrEmpty(local.Name)))
                    localized.Add(new LocalizedProperty()
                    {
                        LanguageId = local.LanguageId,
                        LocaleKey = "Name",
                        LocaleValue = local.Name,
                        _id = ObjectId.GenerateNewId().ToString(),
                        Id = localized.Count > 0 ? localized.Max(x=>x.Id)+1:1,
                    });

                if (!(String.IsNullOrEmpty(local.Description)))
                    localized.Add(new LocalizedProperty()
                    {
                        LanguageId = local.LanguageId,
                        LocaleKey = "Description",
                        LocaleValue = local.Description,
                        _id = ObjectId.GenerateNewId().ToString(),
                        Id = localized.Count > 0 ? localized.Max(x => x.Id) + 1 : 1,
                    });

            }
            return localized;
        }
        private void SaveAttribute(ProductModel model,ProductAttribute attr)
        {
            //var productAttributeMapping = new ProductAttributeMapping
            //{
            //    ProductId = model.Id,
            //    ProductAttributeId = attr.Id,
            //    TextPrompt = "",
            //    IsRequired = false,
            //    AttributeControlTypeId = 4,
            //    DisplayOrder = 1
            //};
            //_productAttributeService.InsertProductAttributeMapping(productAttributeMapping);

            //var pav = new ProductAttributeValue
            //    {
            //        ProductAttributeMappingId = productAttributeMapping.Id,
            //        AttributeValueType = AttributeValueType.Simple,
            //        Name = predefinedValue.Name,
            //        PriceAdjustment = predefinedValue.PriceAdjustment,
            //        WeightAdjustment = predefinedValue.WeightAdjustment,
            //        Cost = predefinedValue.Cost,
            //        IsPreSelected = predefinedValue.IsPreSelected,
            //        DisplayOrder = predefinedValue.DisplayOrder
            //    };
            //_productAttributeService.InsertProductAttributeValue(pav);

            ////predefined values
            //var predefinedValues = _productAttributeService.GetPredefinedProductAttributeValues(model.ProductAttributeId);
            //foreach (var predefinedValue in predefinedValues)
            //{
            //    var pav = new ProductAttributeValue
            //    {
            //        ProductAttributeMappingId = productAttributeMapping.Id,
            //        AttributeValueType = AttributeValueType.Simple,
            //        Name = predefinedValue.Name,
            //        PriceAdjustment = predefinedValue.PriceAdjustment,
            //        WeightAdjustment = predefinedValue.WeightAdjustment,
            //        Cost = predefinedValue.Cost,
            //        IsPreSelected = predefinedValue.IsPreSelected,
            //        DisplayOrder = predefinedValue.DisplayOrder
            //    };
            //    _productAttributeService.InsertProductAttributeValue(pav);
            //}
        }