Skip to navigation
Update shopify stock with ShopifySharp
27.03.23
using System; using ShopifySharp; using ShopifySharp.Filters; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using System.Diagnostics; namespace shopify { class MainClass { public string Url { get; set; } public string Token { get; set; } static async Task Main(string[] args) { Console.WriteLine("...Sync Shopify "); var a = new MainClass(); a.Url = "https://foo.myshopify.com"; a.Token = "shpat_xxxxxxxxxxxxxyyyyyyyyyyy"; string location = await a.Get_first_location(a.Url,a.Token); Console.WriteLine("First Location: " + location); string x = await a.Set_Stock(a.Url, a.Token,"10", location, "44896837533856"); //System.Threading.Thread.Sleep(1000); //int count = await Product_count(a.Url,a.Token); //Console.WriteLine("Products Count: " + count); Dictionary
> products = await a.Product_list(a.Url, a.Token); foreach(var i in products) { Console.WriteLine(i.Key); Console.WriteLine("qty:" + i.Value["inventory_quantity"]); Console.WriteLine("inventory item id:"+ i.Value["inventory_item_id"]); Console.WriteLine("sku id:" + i.Value["id"]); } } public async Task
>> Product_list(string url, string token) { var service = new ProductService(url, token); //int count = await service.CountAsync(); var stocks = new Dictionary
>(); Console.WriteLine("...first shopify api request"); var page = await service.ListAsync(new ProductListFilter { Limit = 250, Fields = "variants" }); if (page.LinkHeader != null) { stocks = Stock_collect(page); while (page.HasNextPage) { System.Threading.Thread.Sleep(5000); Console.WriteLine("...next shopify api request"); page = await service.ListAsync(page.GetNextPageFilter()); var stocks2 = Stock_collect(page); foreach (var i in stocks2) { if (!stocks.ContainsKey(i.Key)) { stocks.Add(i.Key, i.Value); } } } } return stocks; } Type GetStaticType
(T x) => typeof(T); public Dictionary
> Stock_collect(ShopifySharp.Lists.ListResult
page) { var stocks = new Dictionary
>(); foreach (var i in page.Items) { //Console.WriteLine(i.Variants); //Console.WriteLine(i.Variants); foreach (var ii in i.Variants) { try { //Console.WriteLine(ii); if (ii.SKU != null) { if (!stocks.ContainsKey(ii.SKU)) { var v = new Dictionary
{ { "inventory_quantity", ii.InventoryQuantity.ToString()}, { "inventory_item_id", ii.InventoryItemId.ToString() }, { "id", ii.Id.ToString() }, { "product_id", ii.ProductId.ToString() }, }; stocks.Add(ii.SKU, v); } } } catch (Exception e) { Console.WriteLine(e); Console.WriteLine(ii.SKU); } } } return stocks; } public async Task
Product_count(string url, string token) { var service = new ProductService(url, token); int count = await service.CountAsync(); return count; } public async Task
Set_Stock(string url, string token, string _stock, string _location_id, string _inventory_item_id) { long stock = Int32.Parse(_stock); long location_id = long.Parse(_location_id); long inventory_item_id = long.Parse(_inventory_item_id); var service = new InventoryLevelService(url, token); var count = await service.SetAsync(new InventoryLevel() { LocationId = location_id, InventoryItemId = inventory_item_id, Available = stock }); return "ok"; } public async Task
Get_first_location(string url, string token) { var service = new LocationService(url, token); var locations = await service.ListAsync(); foreach(var i in locations.Items) { //Console.WriteLine(i.Id); return i.Id.ToString(); } return ""; } } }
Reply
Anonymous
Information Epoch 1742249065
Design for simplicity, add complexity only where you must.
Home
Notebook
Contact us