Beispiel #1
0
        internal static IEnumerable <ShopifyProduct> GetShopifyAllProduct(ShopifyProductService shopifyProductService, string fields = null)
        {
            var listProduct = default(Task <IEnumerable <ShopifyProduct> >);
            var allProducts = new List <ShopifyProduct>();
            var page        = 1;

            do
            {
                ShopifyCall.ExecuteCall(delegate()
                {
                    listProduct = shopifyProductService.ListAsync(new ShopifyProductFilterOptions
                    {
                        PublishedStatus = "any",
                        Limit           = 250,
                        Page            = page,
                        Fields          = fields
                    });

                    listProduct.Wait();
                });

                allProducts.AddRange(listProduct.Result);

                page++;
            }while (listProduct.Result.Count() == 250);

            return(allProducts);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var settings = new ShopifySettings("vlad-p.myshopify.com", "a14623576b0b4fc774468131b4552a8f");
            var serv     = new ShopifyProductService(settings);
            //var webhookServ = new ShopifyWebhookService(settings);

            //var hooks = webhookServ.GetAll();

            var prods = serv.GetAll();

            //var evListServ = new EventListService();
            //var productListServ = new ProductListService();

            //var res = evListServ.Search(new EventListSearchRequest()
            //{
            //    SearchType = 2,
            //    Skip = 0,
            //    Top = 10,
            //    Title = "gfdfgdfg3434tdsf"
            //});

            //var prodLists = productListServ.Search(new ProductListSearchRequest
            //{
            //    Skip = 0,
            //    Top = 10
            //});

            //var gfdg = 2;
        }
Beispiel #3
0
        internal static ShopifyProduct UpdateVariantsOnShopify(ShopifyProductService shopifyProductService, AliShopifyProduct refProduct, ShopifyProduct shopifyProduct)
        {
            var updateShopifyProduct = default(ShopifyProduct);

            if (shopifyProduct.Variants.Any())
            {
                shopifyProduct.Variants = shopifyProduct.Variants
                                          .Select(delegate(ShopifyProductVariant shopifyVariant)
                {
                    if (string.IsNullOrWhiteSpace(shopifyVariant.SKU) && shopifyProduct.Variants.Count() == 1)
                    {
                        return(shopifyVariant);
                    }

                    var variant = refProduct.AliProduct.AliProductVariant.FirstOrDefault(i => i.AliProductVariantId.ToString() == shopifyVariant.SKU);

                    if (variant == null || !variant.Enabled)
                    {
                        return(null);
                    }

                    var shopifyImage = default(ShopifyProductImage);

                    if (variant.AliProductImage != null)
                    {
                        shopifyImage = ShopifyHelper.GetShopifyImageFromUrl(variant.AliProductImage.Url, shopifyProduct.Images);
                    }

                    if (shopifyImage != null)
                    {
                        shopifyVariant.ImageId = shopifyImage.Id;
                    }

                    return(shopifyVariant);
                })
                                          .Where(i => i != null)
                                          .ToArray();

                ShopifyCall.ExecuteCall(delegate()
                {
                    var task = shopifyProductService.UpdateAsync(shopifyProduct);

                    task.Wait();

                    updateShopifyProduct = task.Result;
                });
            }

            return(updateShopifyProduct ?? shopifyProduct);
        }
Beispiel #4
0
        /// <summary>
        /// Creates a new product that can be used to test the Collect API.
        /// </summary>
        /// <returns>The new product.</returns>
        public static async Task<ShopifyProduct> CreateProduct()
        {
            var service = new ShopifyProductService(Utils.MyShopifyUrl, Utils.AccessToken);
            var product = new ShopifyProduct()
            {
                CreatedAt = DateTime.UtcNow,
                Title = "Burton Custom Freestlye 151",
                Vendor = "Burton",
                BodyHtml = "<strong>Good snowboard!</strong>",
                ProductType = "Snowboard",
                Images = new List<ShopifyProductImage> { new ShopifyProductImage { Attachment = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" } },
            };

            return await service.CreateAsync(product);
        }
Beispiel #5
0
        public static async Task<long> GetProductId()
        {
            if (ProductId.HasValue)
            {
                return ProductId.Value;
            }

            var service = new ShopifyProductService(Utils.MyShopifyUrl, Utils.AccessToken);
            var products = await service.ListAsync(new Filters.ShopifyProductFilter()
            {
                Limit = 1,
                Fields = "id"
            });
            
            ProductId = products.First().Id;

            return ProductId.Value;
        }
Beispiel #6
0
        internal static ShopifyProduct GetProductOnShopify(ShopifyProductService shopifyProductService, string handle)
        {
            var shopifyProduct = default(ShopifyProduct);

            ShopifyCall.ExecuteCall(delegate()
            {
                var task = shopifyProductService.ListAsync(new ShopifyProductFilterOptions
                {
                    Handle = handle
                });

                task.Wait();

                shopifyProduct = task.Result.FirstOrDefault();
            });

            return(shopifyProduct);
        }
Beispiel #7
0
        public static async Task <long> GetProductId()
        {
            if (ProductId.HasValue)
            {
                return(ProductId.Value);
            }

            var service  = new ShopifyProductService(Utils.MyShopifyUrl, Utils.AccessToken);
            var products = await service.ListAsync(new Filters.ShopifyProductFilter()
            {
                Limit  = 1,
                Fields = "id"
            });

            ProductId = products.First().Id;

            return(ProductId.Value);
        }
Beispiel #8
0
        /// <summary>
        /// Creates a new product that can be used to test the Collect API.
        /// </summary>
        /// <returns>The new product.</returns>
        public static async Task <ShopifyProduct> CreateProduct()
        {
            var service = new ShopifyProductService(Utils.MyShopifyUrl, Utils.AccessToken);
            var product = new ShopifyProduct()
            {
                CreatedAt   = DateTime.UtcNow,
                Title       = "Burton Custom Freestlye 151",
                Vendor      = "Burton",
                BodyHtml    = "<strong>Good snowboard!</strong>",
                ProductType = "Snowboard",
                Images      = new List <ShopifyProductImage> {
                    new ShopifyProductImage {
                        Attachment = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
                    }
                },
            };

            return(await service.CreateAsync(product));
        }
Beispiel #9
0
        public static List <ShopifyProduct> GetProductsByStores(Dictionary <long, List <long> > productIdsByStore, Dictionary <long, Store> stores)
        {
            var allProducts = new List <ShopifyProduct>();

            foreach (var storeProduct in productIdsByStore)
            {
                if (!stores.ContainsKey(storeProduct.Key))
                {
                    continue;
                }

                var store = stores[storeProduct.Key];

                var productService = new ShopifyProductService(new ShopifySettings(store.Host, store.AccessToken));

                allProducts.AddRange(productService.GetByIDs(storeProduct.Value));
            }

            return(allProducts);
        }
Beispiel #10
0
        internal static ShopifyProduct UnpublishProductOnShopify(ShopifyProductService shopifyProductService, long shopifyProductId)
        {
            var updateShopifyProduct = default(ShopifyProduct);
            var shopifyProduct       = new ShopifyProduct
            {
                Id        = shopifyProductId,
                Published = false
            };

            ShopifyCall.ExecuteCall(delegate()
            {
                var task = shopifyProductService.UpdateAsync(shopifyProduct);

                task.Wait();

                updateShopifyProduct = task.Result;
            });

            return(updateShopifyProduct);
        }
Beispiel #11
0
        public IEnumerable <ShopifyProduct> Search(ProductSearchRequest request)
        {
            var store = _storeService.GetById(request.StoreId);

            if (store == null || store.Options.HasFlag(StoreOptions.Disabled))
            {
                throw new Exception("Store not found or disabled");
            }

            var productService = new ShopifyProductService(new ShopifySettings(store.Host, store.AccessToken));

            var products = productService.Filter(new ShopifyProductFilterRequest
            {
                Title           = request.Title,
                PublishedStatus = ShopifyPublishedStatus.Published,
                Page            = request.Page,
                Limit           = request.Limit
            });

            return(products);
        }
Beispiel #12
0
        internal static ShopifyProduct SaveProductOnShopify(ShopifyProductService shopifyProductService, ShopifyProduct shopifyProduct)
        {
            var saveShopifyProduct = default(ShopifyProduct);

            ShopifyCall.ExecuteCall(delegate()
            {
                var task = default(Task <ShopifyProduct>);

                if (shopifyProduct.Id == null)
                {
                    task = shopifyProductService.CreateAsync(shopifyProduct);
                }
                else
                {
                    task = shopifyProductService.UpdateAsync(shopifyProduct);
                }

                task.Wait();

                saveShopifyProduct = task.Result;
            });

            return(saveShopifyProduct);
        }
Beispiel #13
0
        /// <summary>
        /// Deletes a product that was used to test the Collect API.
        /// </summary>
        public static async Task DeleteProduct(long id)
        {
            var service = new ShopifyProductService(Utils.MyShopifyUrl, Utils.AccessToken);

            await service.DeleteAsync(id);
        }
Beispiel #14
0
        public static void UpdateShopifyProduct(UpdateShopifyProductModel item)
        {
            using (var db = AliShopEntities.New())
            {
                var shopifyProductRules = RulesCreator.NewRules <AliShopifyProductRules>(db);
                var shopifyPriceRules   = RulesCreator.NewRules <AliShopifyPriceRules>(db);
                var parameterRules      = RulesCreator.NewRules <AliParameterRules>(db);

                var shopifyProduct = shopifyProductRules.GetById(
                    item.AliShopifyProductId,
                    "AliProduct.AliProductVariant",
                    "AliProduct.AliProductImage",
                    "AliProduct.AliProductLink",
                    "AliProduct.AliProductOption",
                    "AliProduct.AliProductSpecific",
                    "AliProduct.AliStore"
                    ).First();

                foreach (var image in shopifyProduct.AliProduct.AliProductImage)
                {
                    if (!image.Url.EndsWith("_640x640.jpg"))
                    {
                        image.Url += "_640x640.jpg";
                    }
                }

                var title           = shopifyProduct.AliProduct.Title;
                var invalidKeywords = ShopifyHelper.GetShopifyTags(parameterRules.GetByName("product_title_invalid_keywords").Value);

                foreach (var keyword in invalidKeywords)
                {
                    title = Regex.Replace(title, Regex.Escape(keyword), "", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase).Trim();
                }

                title = Regex.Replace(title, "\\s{1,}", " ", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                title = new TitleCollection(title).Short(100).Edited;

                Print.PrintStatus(item.RefName, "BeforeRun", title);

                var calculationPrices     = shopifyPriceRules.GetFixedCalculationPrices(shopifyProduct.AliProductId);
                var stockSafetyMargin     = int.Parse(parameterRules.GetByName("stock_safety_margin").Value);
                var handleFriendlyName    = ShopifyHelper.ShopifyHandleFriendlyName(shopifyProduct.AliProductId, title);
                var shopifyProductService = new ShopifyProductService(AppSettings.ShopifyMyShopifyUrl, AppSettings.ShopifyApiKey);
                var shopifyOrderService   = new ShopifyOrderService(AppSettings.ShopifyMyShopifyUrl, AppSettings.ShopifyApiKey);
                var getShopifyProduct     = default(ShopifyProduct);

                var tryStatusOK = Program.Try(item.RefName, title, 3, () => getShopifyProduct = ShopifyMethod.GetProductOnShopify(shopifyProductService, shopifyProduct.HandleFriendlyName ?? handleFriendlyName));

                Print.PrintStatus(item.RefName, tryStatusOK ? "GetProduct" : "FailGetProduct", title, ConsoleColor.Blue);

                if (tryStatusOK)
                {
                    var changeShopifyProduct = ShopifyHelper.ChangeShopifyProduct(shopifyProduct, calculationPrices, getShopifyProduct, stockSafetyMargin, handleFriendlyName, title);

                    tryStatusOK = Program.Try(item.RefName, title, 3, () => getShopifyProduct = ShopifyMethod.SaveProductOnShopify(shopifyProductService, changeShopifyProduct));

                    Print.PrintStatus(item.RefName, tryStatusOK ? "SaveProduct" : "FailSaveProduct", title, ConsoleColor.Blue);
                }

                if (getShopifyProduct != null && tryStatusOK)
                {
                    tryStatusOK = Program.Try(item.RefName, title, 3, () => getShopifyProduct = ShopifyMethod.UpdateVariantsOnShopify(shopifyProductService, shopifyProduct, getShopifyProduct));

                    Print.PrintStatus(item.RefName, tryStatusOK ? "UpdateVariant" : "FailUpdateVariant", title, ConsoleColor.Blue);
                }

                var hasProductOnShopify = getShopifyProduct != null;

                if (hasProductOnShopify && (!tryStatusOK || !shopifyProduct.AliProduct.Enabled && getShopifyProduct.PublishedAt != null))
                {
                    var unPublishTryStatusOK = Program.Try(item.RefName, title, 3, () => getShopifyProduct = ShopifyMethod.UnpublishProductOnShopify(shopifyProductService, getShopifyProduct.Id.Value));

                    Print.PrintStatus(item.RefName, unPublishTryStatusOK ? "Unpublish" : "FailUnpublish", title, ConsoleColor.Blue);
                }

                shopifyProduct = shopifyProductRules.GetById(item.AliShopifyProductId).Where(i => i.RowVersion == shopifyProduct.RowVersion).FirstOrDefault();

                if (hasProductOnShopify)
                {
                    if (shopifyProduct != null)
                    {
                        shopifyProduct.AvgCompareAtPrice       = (decimal?)getShopifyProduct.Variants.Average(i => i.CompareAtPrice);
                        shopifyProduct.AvgPrice                = (decimal?)getShopifyProduct.Variants.Average(i => i.Price);
                        shopifyProduct.ExistsOnShopify         = true;
                        shopifyProduct.HandleFriendlyName      = getShopifyProduct.Handle;
                        shopifyProduct.LastUpdate              = DateTime.UtcNow;
                        shopifyProduct.Published               = getShopifyProduct.PublishedAt != null;
                        shopifyProduct.RequiredUpdateOnShopify = !tryStatusOK;
                        shopifyProduct.ShopifyProductId        = getShopifyProduct.Id;

                        db.SaveChanges();
                    }
                }
                else
                {
                    if (shopifyProduct != null)
                    {
                        shopifyProduct.ExistsOnShopify         = false;
                        shopifyProduct.LastUpdate              = DateTime.UtcNow;
                        shopifyProduct.RequiredUpdateOnShopify = true;

                        db.SaveChanges();
                    }

                    Print.PrintStatus(item.RefName, "DoesNotHaveProductOnShopify", title, ConsoleColor.Blue);
                }

                Print.PrintStatus(item.RefName, shopifyProduct == null ? "NotUpdateOnDatabase" : "UpdateOnDatabase", title, ConsoleColor.Blue);
                Print.PrintStatus(item.RefName, "RunComplete", title, ConsoleColor.Green);
            }
        }
Beispiel #15
0
        /// <summary>
        /// Deletes a product that was used to test the Collect API.
        /// </summary>
        public static async Task DeleteProduct(long id)
        {
            var service = new ShopifyProductService(Utils.MyShopifyUrl, Utils.AccessToken);

            await service.DeleteAsync(id);
        }