示例#1
0
        private void ImportProduct(ImportPageModel model)
        {
            // load up hard-coded product
            UProduct testProduct = UProductRepository.Load(2951);
            Guid     mGuid       = MProductService.MakeMerchelloProduct(testProduct);

            UProductService.MakeProduct(testProduct, model.ParentNodeId, mGuid);
        }
示例#2
0
        public static void MakeProduct(UProduct prod, int contentNodeParentId, Guid merchelloGuid)
        {
            // create new content
            var content = _cs.CreateContent(prod.Name.Trim(), contentNodeParentId, "Product");

            // set content properties based on data from Able
            content.SetValue("headline", prod.Name);
            content.SetValue("metaDescription", prod.MetaDescription);
            content.SetValue("metaKeywords", prod.MetaKeywords);
            content.SetValue("pageTitle", prod.Name);
            content.SetValue("bodyText", prod.Description);
            content.SetValue("brief", prod.Summary);

            // Establish link to merchello product
            content.SetValue("product", merchelloGuid.ToString());


            // publish content so we can get nodeId
            _cs.Publish(content);

            // pull in images if we have some
            if (prod.Images.Count > 0)
            {
                // make media objects for each product image
                List <string> mediaIds = new List <string>();
                foreach (string imageUrl in prod.Images)
                {
                    // make media and get ID
                    if (!string.IsNullOrEmpty(imageUrl))
                    {
                        string mediaNodeId = MakeMediaNode(content, imageUrl, prod.Name);
                        if (!string.IsNullOrEmpty(mediaNodeId))
                        {
                            mediaIds.Add(mediaNodeId);
                        }
                    }
                }

                // save all images to the media picker property value
                string idList = string.Join(",", mediaIds.ToArray());
                content.SetValue("images", idList);
                _cs.Save(content);
            }
        }
示例#3
0
        public static void SaveProductsMappingToDB()
        {
            //var productCategoriesWorkbook = ExcelProdvider.GetWorkbook("_data/nopVareMapping.xlsx");

            //var products = ExcelProdvider.GetExcelData(productCategoriesWorkbook, 1, 30, true);
            //File.WriteAllText($"_data/productsNopVare.json", JsonConvert.SerializeObject(products));
            var products = JsonConvert.DeserializeObject <List <List <string> > >(File.ReadAllText($"_data/productsNopVare.json"));

            var headers      = ExcelProdvider.GetExcelHeaders(products);
            var descriptions = GetLongDescriptions();

            products.RemoveAt(0);

            int j = 0;

            foreach (var product in products)
            {
                Console.WriteLine(++j);

                if (!string.IsNullOrEmpty(product[headers["SupplierId"]]) &&
                    !string.IsNullOrEmpty(product[headers["ProductId"]]) &&
                    !string.IsNullOrEmpty(product[headers["SupplierName"]]) &&
                    !string.IsNullOrEmpty(product[headers["SupplierProductId"]]))
                {
                    var supplier = context.Suppliers.FirstOrDefault(s => s.SupplierId == product[headers["SupplierId"]]);
                    var isNew    = supplier == null;

                    if (isNew)
                    {
                        supplier = new Supplier();

                        supplier.SupplierId   = product[headers["SupplierId"]];
                        supplier.SupplierName = product[headers["SupplierName"]];

                        context.Suppliers.Add(supplier);
                        context.SaveChanges();
                    }

                    var newProduct = context.Products.FirstOrDefault(p => p.OmnishopProductId == product[headers["ProductId"]]);
                    isNew = newProduct == null;

                    if (isNew)
                    {
                        newProduct = new Product();

                        newProduct.OmnishopProductId = product[headers["ProductId"]];
                        context.Products.Add(newProduct);

                        var productSupplier = new ProductSupplier();

                        productSupplier.ProductId         = newProduct.Id;
                        productSupplier.SupplierId        = supplier.Id;
                        productSupplier.ProductSupplierId = product[headers["SupplierProductId"]];

                        context.ProductSuppliers.Add(productSupplier);

                        var uProduct = new UProduct();

                        uProduct.ProductId = newProduct.Id;

                        uProduct.UProductId       = product[headers["ProductId2"]];
                        uProduct.PimSKU           = product[headers["PimSKU"]];
                        uProduct.ContainerModel   = product[headers["ContainerModel"]];
                        uProduct.VariantModel     = product[headers["VariantModel"]];
                        uProduct.FullModel        = product[headers["FullModel"]];
                        uProduct.DisplayName      = product[headers["DisplayName"]];
                        uProduct.ShortDescription = product[headers["ShortDescription"]];
                        uProduct.LongDescription  = descriptions.FirstOrDefault(d => d[0].Trim() == uProduct.UProductId.Trim())?[1] ?? "";

                        uProduct.SuggestedRetailPriceIncVat = double.TryParse(product[headers["SuggestedRetailPriceIncVat"]], out double suggestedRetailPriceIncVat) ? suggestedRetailPriceIncVat : 0;
                        uProduct.Price = double.TryParse(product[headers["Price"]], out double price) ? price : 0;

                        uProduct.Currency = product[headers["Currency"]];
                        uProduct.VAT      = int.TryParse(product[headers["VAT"]], out int vat) ? vat : 0;

                        uProduct.VendorProductId       = product[headers["VendorProductId"]];
                        uProduct.ManufacturerProductId = product[headers["ManufacturerProductId"]];

                        uProduct.PriceSuggestion   = double.TryParse(product[headers["PriceSuggestion"]], out double priceSuggestion) ? priceSuggestion : 0;
                        uProduct.ProductStatusName = product[headers["ProductStatusName"]];

                        uProduct.PackedWeight  = double.TryParse(product[headers["PackedWeight"]], out double packedWeight) ? packedWeight : 0;
                        uProduct.ModifiedOnUTC = DateTime.TryParse(product[headers["ModifiedOn"]], out DateTime modifiedOn) ? (Nullable <DateTime>)modifiedOn : null;

                        context.UProducts.Add(uProduct);
                        context.SaveChanges();
                    }
                }
            }
        }
示例#4
0
        public static Guid MakeMerchelloProduct(UProduct product)
        {
            // set up return value
            Guid retVal;

            // make sure we have a unique sku
            string sku = MakeUniqueSku(product.Sku);

            if (product.Name.Contains("Droppers With Suction Bulb"))
            {
                string dummy = string.Empty;
            }


            IProduct mProduct = _service.CreateProduct(product.Name, sku, product.Price);

            mProduct.CostOfGoods = product.COGS;
            mProduct.Shippable   = true;
            mProduct.Taxable     = true;

            if (product.HasOptions)
            {
                foreach (UOption uOption in product.Options)
                {
                    // make new merchello option
                    ProductOption mOption = new ProductOption(uOption.Name.Trim());

                    // load able option choices into merchello option
                    IList <string> skuValues = new List <string>();
                    foreach (UOptionChoice uChoice in uOption.Choices)
                    {
                        // make sure we have a unique sku
                        string tempSku    = string.IsNullOrEmpty(uChoice.SkuModifier) ? uChoice.OptionChoiceId.ToString() : uChoice.SkuModifier;
                        int    skuCounter = 1;
                        while (skuValues.Contains(tempSku) || _service.SkuExists(tempSku))
                        {
                            tempSku += "-" + skuCounter.ToString();
                            skuCounter++;
                        }

                        string newSku = tempSku;

                        // add attribute as new option choice to this option
                        mOption.Choices.Add(new ProductAttribute(uChoice.Name.Trim(), newSku));
                        skuValues.Add(newSku);
                    }

                    // add new option to merchello product
                    mProduct.ProductOptions.Add(mOption);
                }
            }

            // save the merchello product
            _service.Save(mProduct);

            IEnumerable <IProductVariant> newVariants = MerchelloContext.Current.Services.ProductVariantService.GetProductVariantsThatCanBeCreated(mProduct);

            MerchelloContext.Current.Services.ProductVariantService.Save(newVariants);


            // set prices on variants now that they are constructed
            foreach (IProductVariant pVariant in newVariants)
            {
                // find the able variant for this choice and get the price
                string originalOptionName = pVariant.Name.Replace(product.Name + " - ", string.Empty);
                originalOptionName = originalOptionName.Trim();
                UProductVariant uVariant = UProductVariantRepository.Load(product.ProductId, originalOptionName);
                if (uVariant != null)
                {
                    // set variant price
                    pVariant.Price  = uVariant.Price;
                    pVariant.Weight = uVariant.Weight;
                    pVariant.Length = product.Length;
                    pVariant.Height = product.Height;
                    pVariant.Width  = product.Width;


                    MerchelloContext.Current.Services.ProductVariantService.Save(pVariant);

                    // make mapping entry
                    string optionList = string.Format("{0},{1},{2}", uVariant.Option1, uVariant.Option2, uVariant.Option3);
                    UProductMappingService.Save(product.ProductId, uVariant.ProductVariantId, optionList, mProduct.Key, pVariant.Key, uVariant.AcUrl, uVariant.AcSku);
                }
            }



            // get key and return Guid
            retVal = mProduct.Key;
            return(retVal);
        }