Need a way to import and export specification attribute

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
9 years ago
Dear all,

Please help me on this...


Thanks in advanced.
9 years ago
You need to customize the code.

If you want to export specification attribute when product import/export then you need add some code in Nop.Services/ExportImport .

There are different different services for each export and import.

Otherwise you want to export only specification attributes then you have to make different functions that export/import only specification attributes.
9 years ago
Thanks for the reply..

just like to know anyone has worked on this before and could share?
9 years ago
Check NopAdmin extension
7 years ago
Hello,

What folder is this located in "Nop.Services/ExportImport"? How do I get to it? I got the most recent download
7 years ago
kithung1983 wrote:
Dear all,

Please help me on this...


Thanks in advanced.


Use the Xml export for export, this contains all the extra data that doesn't fit on a spreadsheet.

A good way to accomplish the import it with an intermin model like below:

The code can import the product attributes from the xml generated by nopcommerce and would be some help.


public partial class ProductImportModel
    {

        public ProductImportModel()
        {
            Categories = new List<string>();
            Pictures = new List<string>();
            Fabrics = new List<string>();
            Sizes = new List<string>(); // sets a specification attribute
            TierPrices1 = new List<decimal>();
            TierPrices2 = new List<decimal>();
            TierPrices3 = new List<decimal>();
            TierPrices4 = new List<decimal>();
            Tags = new List<string>();
            Colours = new List<string>();
            Specifications = new List<string>();
            Videos = new List<string>();
        }

        public int ProductTypeId { get; set; }
        public int ParentGroupedProductId { get; set; }
        public bool VisibleIndividually { get; set; }
            
        public string Name { get; set; }
        public string ShortDescription { get; set; }
        public string FullDescription { get; set; }
        public string AdminComment { get; set; }

        public int VendorId { get; set; }
        public int ProductTemplateId { get; set; }
        public bool ShowOnHomePage { get; set; }

        public string MetaKeywords { get; set; }
        public string MetaDescription { get; set; }
        public string MetaTitle { get; set; }

        public string SEName { get; set; }
        public bool AllowCustomerReviews { get; set; }
        public string Sku { get; set; }



        public string Colour { get; set; }
        public string Size { get; set; }
        public string Width { get; set; }
        
        public string SubSku { get; set; }
        public string GTIN { get; set; }
        public string Brand { get; set; }
        public string Status { get; set; }
        
        public bool Published { get; set; }
        public int Stock { get; set; }
        public decimal CostPrice { get; set; }
        public decimal SellPrice { get; set; }
        public decimal OldPrice { get; set; }

        public int TaxCode { get; set; }
        public bool Sale { get; set; }
        public int DeliveryDateId { get; set; }

        public XElement ProductDiscountsXML { get; set; }
        public XElement TierPricesXML { get; set; }
        public XElement ProductAttributesXML { get; set; }
        public XElement ProductPicturesXML { get; set; }
        public XElement ProductCategoriesXML { get; set; }
        public XElement ProductManufacturersXML { get; set; }
        public XElement ProductSpecificationAttributesXML { get; set; }

        public decimal NameText { get; set; }
        public decimal InitialsText { get; set; }
        public decimal NumberText { get; set; }
        public int CustomizationType { get; set; }
        public string SizeDescription { get; set; }
        public string FabricType { get; set; }
        public string FabricWeight { get; set; }

        public string ClosureType { get; set; }
        public string OuterMaterial { get; set; }

        public string ColourFileName { get; set; } // used for ralawise to decode their colours from the colour codes in the filename

        public string UserAgent { get; set; }
        public List<string> Categories { get; set; }
        public List<string> Pictures { get; set; }
        public List<string> Fabrics { get; set; }
        public List<string> Sizes { get; set; } // sets a specification attribute
        public List<decimal> TierPrices1 { get; set; }
        public List<decimal> TierPrices2 { get; set; }
        public List<decimal> TierPrices3 { get; set; }
        public List<decimal> TierPrices4 { get; set; }
        public List<string> Tags { get; set; }
        public List<string> Colours { get; set; }
        public List<string> Specifications { get; set; }
        public List<string> Videos { get; set; }
    }


and import the xml file:


string xml_doc = File.ReadAllText(filename);

                var element = XElement.Parse(xml_doc);

                string Version = element.Attribute("Version").Value.ToLower();

                var items = element.Elements("Product");

                foreach (var item in items)
                {
                    ProductImportModel model = new ProductImportModel();

                    model.ProductTypeId = int.Parse(item.Elements("ProductTypeId").First().Value);
                    model.ParentGroupedProductId = int.Parse(item.Elements("ParentGroupedProductId").First().Value);
                    model.VisibleIndividually = item.Elements("VisibleIndividually").First().Value == "True" ? true : false;

                    model.Name = item.Elements("Name").First().Value;
                    model.ShortDescription = item.Elements("ShortDescription").First().Value;
                    model.FullDescription = item.Elements("FullDescription").First().Value;
                    model.AdminComment = item.Elements("AdminComment").First().Value;

                    model.VendorId = int.Parse(item.Elements("VendorId").First().Value);
                    model.ProductTemplateId = int.Parse(item.Elements("ProductTemplateId").First().Value);
                    model.ShowOnHomePage = item.Elements("ShowOnHomePage").First().Value == "True" ? true : false;

                    model.MetaKeywords = item.Elements("MetaKeywords").First().Value;
                    model.MetaDescription = item.Elements("MetaDescription").First().Value;
                    model.MetaTitle = item.Elements("MetaTitle").First().Value;

                    model.SEName = item.Elements("SEName").First().Value;
                    model.AllowCustomerReviews = item.Elements("AllowCustomerReviews").First().Value == "True" ? true : false;
                    model.Sku = item.Elements("SKU").First().Value;

                    model.Published = item.Elements("Published").First().Value == "True" ? true : false;
                    model.SellPrice = decimal.Parse(item.Elements("Price").First().Value);

                    model.ProductDiscountsXML = item.Elements("ProductDiscounts").First();
                    model.TierPricesXML = item.Elements("TierPrices").First();
                    model.ProductAttributesXML = item.Elements("ProductAttributes").First(); //todo only the attributes and their values are imported
                    model.ProductPicturesXML = item.Elements("ProductPictures").First();
                    model.ProductCategoriesXML = item.Elements("ProductCategories").First();
                    model.ProductManufacturersXML = item.Elements("ProductManufacturers").First();
                    model.ProductSpecificationAttributesXML = item.Elements("ProductSpecificationAttributes").First();

                    list.Add(model);
                }



Specification Attributes can be imported manually with:


var _attribute = _specificationAttributeRepository.Table.FirstOrDefault(x => x.Name == "Available Sizes");

                if (_attribute == null)
                {
                    var model = new SpecificationAttribute();

                    model.Name = "Available Sizes";

                    _specificationAttributeService.InsertSpecificationAttribute(model);
                    _attribute = _specificationAttributeRepository.Table.FirstOrDefault(x => x.Name == "Available Sizes");
                }

                foreach (var _size in NewProduct.Sizes)
                {
                    if (!string.IsNullOrEmpty(_size))
                    {
                        var _option = _specificationAttributeOptionRepository.Table.FirstOrDefault(x => x.Name == _size);

                        if (_option == null)
                        {
                            var model = new SpecificationAttributeOption();

                            model.SpecificationAttributeId = _attribute.Id;
                            model.Name = _size;

                            _specificationAttributeService.InsertSpecificationAttributeOption(model);
                            _option = _specificationAttributeOptionRepository.Table.FirstOrDefault(x => x.Name == "Available Sizes");
                        }

                        product.ProductSpecificationAttributes.Add(new ProductSpecificationAttribute()
                        {
                            AllowFiltering = true,
                            ShowOnProductPage = true,
                            DisplayOrder = 0,
                            SpecificationAttributeOption = _specificationAttributeRepository.Table.Single(sa => sa.Name == "Available Sizes").SpecificationAttributeOptions.Single(sao => sao.Name == _size)
                        });
                    }
                }


and the Product Attributes XML can be parsed with:


ProductAttribute _attribute;
                    string _attributeName;

                    foreach (var item in NewProduct.ProductAttributesXML.Elements("ProductVariantAttribute"))
                    {
                        _attributeName = item.Elements("ProductAttributeName").First().Value;
                        _attribute = _productAttributeRepository.Table.FirstOrDefault(x => x.Name == _attributeName);

                        if (_attribute == null)
                        {

                            _attribute = _dbContext.Set<ProductAttribute>().Add(new ProductAttribute()
                            {
                                Name = _attributeName
                            });

                            _dbContext.SaveChanges();
                        }

                        var validation = item.Elements("ValidationMinLength");
                        int validat = 0;
                        if (validation.Count() > 0)
                        {
                            validat = int.Parse(item.Elements("ValidationMinLength").First().Value);
                        }
                        var _pva = new ProductVariantAttribute()
                        {
                            ProductAttributeId = _attribute.Id,
                            TextPrompt = item.Elements("TextPrompt").First().Value,
                            IsRequired = item.Elements("IsRequired").First().Value == "True" ? true : false,
                            AttributeControlType = (AttributeControlType)int.Parse(item.Elements("AttributeControlTypeId").First().Value),
                            DisplayOrder = int.Parse(item.Elements("DisplayOrder").First().Value),
                            ValidationMinLength = validat
                        };

                        foreach (var value in item.Elements("ProductVariantAttributeValues").Elements("ProductVariantAttributeValue"))
                        {
                            _pva.ProductVariantAttributeValues.Add(new ProductVariantAttributeValue()
                            {
                                Name = value.Elements("Name").First().Value,
                                AttributeValueTypeId = int.Parse(value.Elements("AttributeValueTypeId").First().Value),
                                AssociatedProductId = int.Parse(value.Elements("AssociatedProductId").First().Value),
                                ColorSquaresRgb = value.Elements("ColorSquaresRgb").First().Value,
                                PriceAdjustment = decimal.Parse(value.Elements("PriceAdjustment").First().Value),
                                WeightAdjustment = decimal.Parse(value.Elements("WeightAdjustment").First().Value),
                                Cost = decimal.Parse(value.Elements("Cost").First().Value),
                                Quantity = int.Parse(value.Elements("Quantity").First().Value),
                                IsPreSelected = value.Elements("IsPreSelected").First().Value == "True" ? true : false,
                                DisplayOrder = int.Parse(value.Elements("DisplayOrder").First().Value),
                                PictureId = int.Parse(value.Elements("PictureId").First().Value)
                            });
                        }

                        product.ProductVariantAttributes.Add(_pva);
                    }

                    _dbContext.SaveChanges();
6 years ago
https://www.nopcommerce.com/p/3034/specification-attribute-easy-import-export.aspx
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.