Exemplo n.º 1
0
        public IActionResult Delete(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageWidgets))
            {
                return(AccessDeniedView());
            }

            IList <ProductAttributeMapping> mappings = _productAttributeService
                                                       .GetProductAttributeMappingsByProductId(id);

            var allAttributes = _productPictureModifierService.GetProductAttributeUsedByPlugin();

            //remove product attribute mapping
            foreach (var mapping in mappings)
            {
                if (allAttributes.Any(x => x.Id == mapping.ProductAttributeId))
                {
                    _productAttributeService.DeleteProductAttributeMapping(mapping);
                }
            }

            //remove product picture with logo
            _productPictureModifierService.RemovePictureCreatedByPluginForProduct(id);

            //remove logo image's setting
            _logoPositionService.DeleteByProductId(id);

            SuccessNotification(_localizationService.GetResource("Widgets.ProductPictureModifier.Mapping.Deleted"));
            return(RedirectToAction("Configure"));
        }
        public async Task <bool> Handle(DeleteProductAttributeMappingCommand request, CancellationToken cancellationToken)
        {
            //insert mapping
            var productAttributeMapping = request.Model.ToEntity();
            await _productAttributeService.DeleteProductAttributeMapping(productAttributeMapping, request.Product.Id);

            return(true);
        }
Exemplo n.º 3
0
        private void UpdateProductAttributes(Product entityToUpdate, Delta <ProductDto> productDtoDelta)
        {
            // If no product attribute mappings are specified means we don't have to update anything
            if (productDtoDelta.Dto.ProductAttributeMappings == null)
            {
                return;
            }

            // delete unused product attribute mappings
            var toBeUpdatedIds                 = productDtoDelta.Dto.ProductAttributeMappings.Where(y => y.Id != 0).Select(x => x.Id);
            var productAttributeMappings       = _productAttributeService.GetProductAttributeMappingsByProductId(entityToUpdate.Id);
            var unusedProductAttributeMappings = productAttributeMappings.Where(x => !toBeUpdatedIds.Contains(x.Id)).ToList();

            foreach (var unusedProductAttributeMapping in unusedProductAttributeMappings)
            {
                _productAttributeService.DeleteProductAttributeMapping(unusedProductAttributeMapping);
            }

            foreach (var productAttributeMappingDto in productDtoDelta.Dto.ProductAttributeMappings)
            {
                if (productAttributeMappingDto.Id > 0)
                {
                    // update existing product attribute mapping
                    var productAttributeMappingToUpdate = productAttributeMappings.FirstOrDefault(x => x.Id == productAttributeMappingDto.Id);
                    if (productAttributeMappingToUpdate != null)
                    {
                        productDtoDelta.Merge(productAttributeMappingDto, productAttributeMappingToUpdate, false);

                        _productAttributeService.UpdateProductAttributeMapping(productAttributeMappingToUpdate);

                        UpdateProductAttributeValues(productAttributeMappingDto, productDtoDelta);
                    }
                }
                else
                {
                    var newProductAttributeMapping = new ProductAttributeMapping
                    {
                        ProductId = entityToUpdate.Id
                    };

                    productDtoDelta.Merge(productAttributeMappingDto, newProductAttributeMapping);

                    // add new product attribute
                    _productAttributeService.InsertProductAttributeMapping(newProductAttributeMapping);
                }
            }
        }
        public JsonResult DeleteCcAttributes(int productId)
        {
            try
            {
                var specificationAttributes = _specificationAttributeService.GetProductSpecificationAttributes(productId);
                if (
                    specificationAttributes.Any(
                        attribute =>
                        attribute.SpecificationAttributeOptionId ==
                        _ccService.EditorDefinitionSpecificationAttributeOptionId()))
                {
                    _specificationAttributeService.DeleteProductSpecificationAttribute(
                        specificationAttributes.First(attribute =>
                                                      attribute.SpecificationAttributeOptionId ==
                                                      _ccService.EditorDefinitionSpecificationAttributeOptionId()));
                }

                if (
                    specificationAttributes.Any(
                        attribute =>
                        attribute.SpecificationAttributeOptionId ==
                        _ccService.EditorConfigurationSpecificationAttributeOptionId()))
                {
                    _specificationAttributeService.DeleteProductSpecificationAttribute(
                        specificationAttributes.First(attribute =>
                                                      attribute.SpecificationAttributeOptionId ==
                                                      _ccService.EditorConfigurationSpecificationAttributeOptionId()));
                }

                var productAttributes = _productAttributeService.GetProductAttributeMappingsByProductId(productId);
                foreach (var attrId in _ccService.GetCcAttrIds())
                {
                    if (productAttributes.Any(attr => attr.ProductAttributeId == attrId))
                    {
                        _productAttributeService.DeleteProductAttributeMapping(
                            productAttributes.First(attr => attr.ProductAttributeId == attrId));
                    }
                }

                return(Json(new { status = "success" }));
            }
            catch (Exception ex)
            {
                return(Json(new { status = "error", message = ex.Message }));
            }
        }
        private void UpdateProductAttributes(Product entityToUpdate, Delta <ProductDto> productDtoDelta)
        {
            // If no product attribute mappings are specified means we don't have to update anything
            if (productDtoDelta.Dto.ProductAttributeMappings == null)
            {
                return;
            }

            //If it has attributes, then stock should be managed by them
            entityToUpdate.ManageInventoryMethod = ManageInventoryMethod.ManageStockByAttributes;
            entityToUpdate.AllowAddingOnlyExistingAttributeCombinations = true;
            entityToUpdate.DisplayStockAvailability = true;

            // delete unused product attribute mappings
            var toBeUpdatedIds    = new List <int>();
            var useRecordMappings = false;

            foreach (var entityToUpdateMapping in entityToUpdate.ProductAttributeMappings)
            {
                var entityRecords = entityToUpdateMapping.ProductAttributeValues.Select(v => _genericAttributeService.GetAttribute <int>(v, "nop.product.attributevalue.recordid"));

                if (entityRecords.Count() > 0)
                {
                    useRecordMappings = true;

                    foreach (var deltaMapping in productDtoDelta.Dto.ProductAttributeMappings)
                    {
                        var values = deltaMapping.ProductAttributeValues.Where(v => entityRecords.Contains(v.RecordId));
                        if (values != null && values.Count() != 0 && deltaMapping.Id == 0)
                        {
                            toBeUpdatedIds.Add(entityToUpdateMapping.Id);
                            deltaMapping.Id = entityToUpdateMapping.Id;

                            foreach (var entityValue in entityToUpdateMapping.ProductAttributeValues)
                            {
                                var deltaValue = deltaMapping.ProductAttributeValues.Where(dv => dv.RecordId == _genericAttributeService.GetAttribute <int>(entityValue, "nop.product.attributevalue.recordid")).FirstOrDefault();
                                if (deltaValue != null)
                                {
                                    deltaValue.Id = entityValue.Id;
                                }
                            }
                        }
                    }
                }
            }

            if (!useRecordMappings)
            {
                toBeUpdatedIds = productDtoDelta.Dto.ProductAttributeMappings.Where(y => y.Id != 0).Select(x => x.Id).ToList();
            }

            var unusedProductAttributeMappings = entityToUpdate.ProductAttributeMappings.Where(x => !toBeUpdatedIds.Contains(x.Id)).ToList();

            foreach (var unusedProductAttributeMapping in unusedProductAttributeMappings)
            {
                _productAttributeService.DeleteProductAttributeMapping(unusedProductAttributeMapping);
            }

            // delete unused product attribute mappings
            foreach (var productAttributeMappingDto in productDtoDelta.Dto.ProductAttributeMappings)
            {
                if (productAttributeMappingDto.Id > 0)
                {
                    // update existing product attribute mapping
                    var productAttributeMappingToUpdate = entityToUpdate.ProductAttributeMappings.FirstOrDefault(x => x.Id == productAttributeMappingDto.Id);
                    productAttributeMappingToUpdate.AttributeControlTypeId = 1;
                    if (productAttributeMappingToUpdate != null)
                    {
                        productDtoDelta.Merge(productAttributeMappingDto, productAttributeMappingToUpdate, false);

                        _productAttributeService.UpdateProductAttributeMapping(productAttributeMappingToUpdate);

                        UpdateProductAttributeValues(productAttributeMappingDto, productDtoDelta);
                    }
                }
                else
                {
                    var newProductAttributeMapping = new ProductAttributeMapping
                    {
                        ProductId = entityToUpdate.Id,
                        AttributeControlTypeId = 1
                    };

                    productDtoDelta.Merge(productAttributeMappingDto, newProductAttributeMapping);

                    // add new product attribute
                    _productAttributeService.InsertProductAttributeMapping(newProductAttributeMapping);
                    productAttributeMappingDto.Id = newProductAttributeMapping.Id;

                    for (int i = 0; i < newProductAttributeMapping.ProductAttributeValues.Count; i++)
                    {
                        var attributeValue    = newProductAttributeMapping.ProductAttributeValues.ElementAt(i);
                        var dtoAttributeValue = productAttributeMappingDto.ProductAttributeValues.ElementAt(i);
                        if (dtoAttributeValue.ProductPictureId.HasValue)
                        {
                            attributeValue.PictureId = dtoAttributeValue.ProductPictureId.Value;
                        }
                        else
                        {
                            attributeValue.PictureId = 0;
                        }

                        _genericAttributeService.SaveAttribute(attributeValue, "nop.product.attributevalue.recordid", dtoAttributeValue.RecordId);
                    }
                }
            }
        }
Exemplo n.º 6
0
        public IActionResult CreateProductAttrs([ModelBinder(typeof(JsonModelBinder <CustomeProductAttributesDto>))] Delta <CustomeProductAttributesDto> customeProductAttributesDtoDelta)
        {
            // Here we display the errors if the validation has failed at some point.
            if (!ModelState.IsValid)
            {
                return(Error());
            }
            var list = _productAttributeService.GetAllProductAttributes().Select(l => l.Name.ToLower().Trim()).ToList();

            if (!list.Contains(customeProductAttributesDtoDelta.Dto.AttributeName.ToLower().Trim()))
            {
                var productAttribute = new ProductAttribute()
                {
                    Description = customeProductAttributesDtoDelta.Dto.Description,
                    Id          = 0,
                    Name        = customeProductAttributesDtoDelta.Dto.AttributeName
                };
                _productAttributeService.InsertProductAttribute(productAttribute);
                CustomerActivityService.InsertActivity("AddNewProductAttribute", LocalizationService.GetResource("ActivityLog.AddNewProductAttribute"), productAttribute);
            }
            //get Product By SKU
            var product = _productService.GetProductBySku(customeProductAttributesDtoDelta.Dto.Sku);

            if (product == null)
            {
                return(Error(HttpStatusCode.NotFound, "product", "not found"));
            }
            var attr = _productAttributeService.GetProductAttributeByAXId(customeProductAttributesDtoDelta.Dto.AttributeId);

            if (attr == null)
            {
                if (customeProductAttributesDtoDelta.Dto.Values.Count > 0)
                {
                    var prodAttr = _productAttributeService.GetProductAttributeByName(customeProductAttributesDtoDelta.Dto.AttributeName);
                    //create
                    var tempAttr = new ProductAttributeMapping()
                    {
                        AttrIdAX             = customeProductAttributesDtoDelta.Dto.AttributeId,
                        AttributeControlType = AttributeControlType.DropdownList,
                        ProductId            = product.Id,
                        ProductAttributeId   = prodAttr.Id,
                        IsRequired           = true,
                    };
                    _productAttributeService.InsertProductAttributeMapping(tempAttr);
                    //
                    foreach (var item in customeProductAttributesDtoDelta.Dto.Values)
                    {
                        _productAttributeService.InsertProductAttributeValue(new ProductAttributeValue()
                        {
                            Id = 0,
                            ProductAttributeMappingId = tempAttr.Id,
                            Name            = item.Text,
                            PriceAdjustment = item.Price,
                            DisplayOrder    = item.DisplayOrder,
                        });
                    }
                }
            }
            else
            {
                var prodAttr = _productAttributeService.GetProductAttributeByName(customeProductAttributesDtoDelta.Dto.AttributeName);
                if (customeProductAttributesDtoDelta.Dto.Values.Count == 0)
                {
                    _productAttributeService.DeleteProductAttributeMapping(attr);
                }
                else
                {
                    //edit
                    attr.ProductAttributeId = prodAttr.Id;
                    _productAttributeService.UpdateProductAttributeMapping(attr);
                    //
                    var listofValues = _productAttributeService.GetProductAttributeValues(attr.Id);
                    //delete
                    foreach (var item in listofValues)
                    {
                        _productAttributeService.DeleteProductAttributeValue(item);
                    }
                    foreach (var item in customeProductAttributesDtoDelta.Dto.Values)
                    {
                        _productAttributeService.InsertProductAttributeValue(new ProductAttributeValue()
                        {
                            Id = 0,
                            ProductAttributeMappingId = attr.Id,
                            Name            = item.Text,
                            PriceAdjustment = item.Price,
                            DisplayOrder    = item.DisplayOrder,
                        });
                    }
                }
            }

            var customeProductAttributesRootObjectDto = new CustomeProductAttributesRootObjectDto();

            customeProductAttributesRootObjectDto.ProductAttributes.Add(new CustomeProductAttributesDto()
            {
                AttributeId   = 123456,
                AttributeName = "Test",
                Description   = "Test",
                Sku           = "1200",
                Values        = new List <ValueDto>()
                {
                    new ValueDto()
                    {
                        DisplayOrder = 0,
                        Price        = 1222,
                        Text         = "Test"
                    }
                    ,
                    new ValueDto()
                    {
                        DisplayOrder = 0,
                        Price        = 1222,
                        Text         = "Test"
                    }
                }
            });

            var json = JsonFieldsSerializer.Serialize(customeProductAttributesRootObjectDto, string.Empty);

            return(new RawJsonActionResult("Done"));
        }