Пример #1
0
        public static void TestShopify()
        {
            //ShopifySharp.CustomCollectionService customCollectionService = new CustomCollectionService("https://optimalnw.myshopify.com", "3912f9da28ad92b17c79bf734b25246f");
            //IEnumerable<CustomCollection> customs = customCollectionService.ListAsync().Result;

            //foreach (var cc in customs.Where(c => c.PublishedScope != "global"))
            //{
            //	cc.PublishedScope = "global";
            //	customCollectionService.UpdateAsync(cc.Id.Value, cc).Wait();
            //}



            //CollectService collectService = new CollectService("https://optimalnw.myshopify.com", "3912f9da28ad92b17c79bf734b25246f");
            //IEnumerable<Collect> collects = collectService.ListAsync().Result;

            //ShopifySharp.MetaFieldService metaFieldService = new ShopifySharp.MetaFieldService("https://optimalnw.myshopify.com", "3912f9da28ad92b17c79bf734b25246f");
            //IEnumerable<MetaField> metas = metaFieldService.ListAsync().Result;

            ShopifySharp.ProductService productService = new ShopifySharp.ProductService("https://optimalnw.myshopify.com", "3912f9da28ad92b17c79bf734b25246f");
            IEnumerable <Product>       products       = productService.ListAsync().Result;

            //MetaField meta = new MetaField()
            //{
            //	Namespace = "ownProduct",
            //	Key = "shortDescription",
            //	Value = "Convenient Supra-Dose Vitamin D3",
            //	ValueType = "string",
            //	Description = "This is a test meta field. It is an string value."
            //};
            //var t = metaFieldService.CreateAsync(meta, products.First().Id.Value, "products").Result;
            //metas = metaFieldService.ListAsync(products.First().Id.Value, "products").Result;
            //products = productService.ListAsync().Result;
        }
Пример #2
0
        public async Task <ShopifySharp.Product> CreateProduct(ImportService.Wrapper.Product product, string vendor, string myShopifyDomain, string accessToken, ImportService.Wrapper.ColorList colorMapping)
        {
            ShopifySharp.ProductService service = new ShopifySharp.ProductService(myShopifyDomain, accessToken);

            var newProduct = new ShopifySharp.Product()
            {
                Vendor = vendor,
            };

            newProduct.Options = new List <ProductOption> {
                new ProductOption {
                    Name = "Color"
                }, new ProductOption {
                    Name = "Size"
                }
            };
            string productTitle  = product.Name;
            string productHandle = product.Name;
            var    colorList     = product.ProductVariants.ProductVariant.Select(x => new { colorName = x.ColorName, colorCode = x.ColorCode }).ToList();
            var    distColorList = colorList.Distinct().Select(x => x).ToList();

            string colorName = colorList.Distinct().FirstOrDefault().colorName;

            if (!string.IsNullOrEmpty(colorName))
            {
                colorName = CheckColorMapping(colorMapping, colorName);

                productHandle += "-" + colorName;
                productTitle  += " " + colorName;
            }
            newProduct.Handle = productHandle.ToLower();
            newProduct.Title  = productTitle.ToUpper();
            var productTags = new List <string>();

            foreach (var c in distColorList)
            {
                string _color = CheckColorMapping(colorMapping, c.colorName);
                productTags.Add(product.Name.ToLower() + "-" + _color.ToLower());
            }
            // product variant list created from ftp xml.
            var _pvList      = new List <ShopifySharp.ProductVariant>();
            var priceTagList = new List <string>();

            foreach (var pv in product.ProductVariants.ProductVariant)
            {
                var    _pv           = new ShopifySharp.ProductVariant();
                string variant_color = CheckColorMapping(colorMapping, pv.ColorName);
                _pv.SKU               = product.Name.Replace(" ", "-").ToUpper();
                _pv.Option1           = variant_color;
                _pv.Option2           = pv.SizeName;
                _pv.Option3           = pv.ColorCode;
                _pv.TaxCode           = "PC040144";
                _pv.Title             = $@"{pv.ColorName.ToUpper()} /\ {pv.SizeName} /\ {variant_color}";
                _pv.Price             = Convert.ToDecimal(pv.ProductVariantSources.ProductVariantSource.Price);
                _pv.InventoryQuantity = Convert.ToInt32(pv.Quantity);
                _pvList.Add(_pv);
                if (_pv.Price < 25)
                {
                    priceTagList.Add("PRICE_under_$25");
                }
                else if (_pv.Price >= 25 && _pv.Price <= 50)
                {
                    priceTagList.Add("PRICE_$25-$50");
                }
                else if (_pv.Price >= 50 && _pv.Price <= 100)
                {
                    priceTagList.Add("PRICE_$50-$100");
                }
                else if (_pv.Price >= 100 && _pv.Price <= 150)
                {
                    priceTagList.Add("PRICE_$100-$150");
                }
            }
            var productVariants = _pvList.DistinctBy(x => x.Title).Select(x => x).ToList();


            var exist = await GetProductByHandle(newProduct.Handle, service);

            if (exist.Count() > 0)
            {
                foreach (var p in exist)
                {
                    var existingPV = p.Variants;
                    var result     = from x in productVariants
                                     join y in existingPV
                                     on new { X1 = x.Option1, X2 = x.Option2 } equals new { X1 = y.Option1, X2 = y.Option2 } select x;
                    var newVariants = productVariants.Where(p1 => !result.Any(p2 => p2.Option1 == p1.Option1 && p2.Option2 == p1.Option2));
                    if (newVariants != null)
                    {
                        ShopifySharp.ProductVariantService pvs = new ProductVariantService(myShopifyDomain, accessToken);

                        foreach (var nvp in newVariants)
                        {
                            var variant = await pvs.CreateAsync(p.Id ?? 0, nvp);
                        }
                    }
                }
            }
            else
            {
                newProduct.Variants = productVariants;
                foreach (var p in priceTagList.Distinct().Select(x => x).ToList())
                {
                    productTags.Add(p.ToLower());
                }
                newProduct.Tags = string.Join(",", productTags.ToArray());

                var createdDate = product.Attributes.Attribute.Where(x => x.ProductAttributeCode == "createddate").Select(x => x.ProductAttributeValue).FirstOrDefault();
                if (createdDate != null)
                {
                    newProduct.CreatedAt = Convert.ToDateTime(createdDate);
                }



                try
                {
                    newProduct = await service.CreateAsync(newProduct);
                }
                catch (Exception ex)
                {
                    string exception = ex.Message;
                    if (ex.InnerException != null)
                    {
                        exception = ex.InnerException.Message;
                    }
                    Log.Error("Error in CreateProduct method in class ProductService.cs", ex, vendor);
                }
            }
            return(newProduct);
        }
Пример #3
0
        public async Task <IEnumerable <ShopifySharp.Product> > GetProductByHandle(string handle, ShopifySharp.ProductService service)
        {
            ShopifySharp.Filters.ProductListFilter filter = new ShopifySharp.Filters.ProductListFilter();
            filter.Handle = handle;
            var data = await service.ListAsync(filter);

            return(data.Items);
        }