public static void InitServices(string updateInventoryAPIFile = null, string updateVariantAPIfile = null, string updateMetafieldsAPIFile = null) { if (updateInventoryAPIFile != null) { updateInventoryService = ShopifyAPIConfig.MakeService <InventoryLevelService>(updateInventoryAPIFile); } if (updateVariantAPIfile != null) { updateVariantService = ShopifyAPIConfig.MakeService <ProductVariantService>(updateVariantAPIfile); } if (updateMetafieldsAPIFile != null) { updateMetafieldService = ShopifyAPIConfig.MakeService <MetaFieldService>(updateMetafieldsAPIFile); } }
private static async Task ImportNewProductsAsync(Shop2 shop, List <Models.Product> products, bool willPublishProducts) { Console.WriteLine(); WriteInfo("Importing products to {0}...", _myShopifyUrl); for (int i = 0; i < products.Count; i++) { var product = products[i]; var productVariant = new ProductVariant { SKU = product.Sku, Title = product.Title, Price = product.Price, InventoryManagement = "shopify", CompareAtPrice = product.PriceBeforeSale }; Console.WriteLine("{0}/{1}\t\t{2} ({3})", i + 1, products.Count, product.Title, product.Sku); Product existingProduct = null; var productService = new ProductService(_myShopifyUrl, _shopAccessToken); if (i == 0) { // HACK: API CALL var existingProducts = await productService.ListAsync(new ProductFilter { Title = product.Title, Limit = 1 }); existingProduct = existingProducts.FirstOrDefault(); } if (existingProduct == null) { var productImages = new List <ProductImage> { new ProductImage { Attachment = product.Image } }; existingProduct = new Product { Title = product.Title, BodyHtml = string.Format("<strong>{0}</strong>", product.Description), ProductType = "", Images = productImages, Tags = product.Category.Replace("/", ","), Variants = new List <ProductVariant> { productVariant } }; // HACK: API CALL existingProduct = await productService .CreateAsync(existingProduct, new ProductCreateOptions { Published = willPublishProducts }); WriteInfoWithTime("✓ Product added"); } else { WriteInfoWithTime("- Product ALREADY added"); } productVariant = existingProduct.Variants.First(); var inventoryLevelService = new InventoryLevelService(_myShopifyUrl, _shopAccessToken); var inventoryLevel = new InventoryLevel { InventoryItemId = productVariant.InventoryItemId, LocationId = shop.PrimaryLocationId, Available = product.Quantity, }; // HACK: API CALL await inventoryLevelService.SetAsync(inventoryLevel); WriteInfoWithTime("✓ Inventory label has been set"); using (var c = new ServerConnection(_connectionString)) { var command = c.SetStatement(@"INSERT INTO [SeparatedTable](Id) VALUES (@id)"); command.AddParameter("@id", System.Data.SqlDbType.Int, product.Id); await command.ExecuteNonQueryAsync(); WriteInfoWithTime("✓ Added to database"); } Console.WriteLine(); } WriteInfoWithTime("DONE Importing products to {0}", _myShopifyUrl); }
public async Task <IActionResult> AddShopifyInventory([FromBody] List <VariantInventory> products) { var response = ""; try { long locationid = 0; VariantInventory model = products.FirstOrDefault(); string shopifyurl = $"https://{model.StoreName}.myshopify.com/"; var locationservice = new LocationService(shopifyurl, model.Token); var locations = await locationservice.ListAsync(); var lid = locations.Where(x => x.Name.ToLower() == "parco").FirstOrDefault(); if (lid == null) { response = "No Location Found"; return(Ok(response)); } if (lid != null) { locationid = Convert.ToInt64(lid.Id); } //model.Token = "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"; var inventoryservice = new InventoryLevelService(shopifyurl, model.Token); var inventoryLevel = new List <InventoryLevel>(); //locationid for parco //long locationid = Convert.ToInt64("45679804460"); //var id1 = Convert.ToInt64("36306970148908"); //var id2 = Convert.ToInt64("36306970181676"); //var id3 = Convert.ToInt64("36306970214444"); //var id4 = Convert.ToInt64("36306982731820"); var ids = new List <long>(); //ids.Add(id1); //ids.Add(id2); //ids.Add(id3); //ids.Add(id4); foreach (var item in products) { ids.Add(Convert.ToInt64(item.InventoryItemId)); } var inventoryLevelFilter = new InventoryLevelListFilter() { InventoryItemIds = ids, Limit = 250 }; var page = await inventoryservice.ListAsync(inventoryLevelFilter); while (true) { inventoryLevel.AddRange(page.Items); if (!page.HasNextPage) { break; } page = await inventoryservice.ListAsync(inventoryLevelFilter); } foreach (var item in inventoryLevel) { if (item.LocationId == locationid) { var iteminfo = products.Where(x => x.InventoryItemId == item.InventoryItemId).FirstOrDefault(); item.Available = iteminfo.InventoryQuantity; var service = await inventoryservice.SetAsync(item); System.Threading.Thread.Sleep(500); } } //var level = levels.Items.FirstOrDefault(); //if (level != null) //{ // level.Available = 26; // var service = await inventoryservice.SetAsync(level); //} //ShopifySharp.ProductVariant pv = new ShopifySharp.ProductVariant(); //pv.InventoryQuantity = 500; //foreach (var item in products) //{ // foreach (var variant in item.Variants) // { // var variantid = Convert.ToInt64(variant.InventoryItemId);/*Convert.ToInt64("36257378205740");*/ // var ids = new List<long>() // { // variantid // }; // var inventoryLevelFilter = new InventoryLevelListFilter() // { // InventoryItemIds = ids // }; // var levels = await inventoryservice.ListAsync(inventoryLevelFilter); // var level = levels.Items.FirstOrDefault(); // if (level!=null) // { // level.Available = 26; // var service = await inventoryservice.SetAsync(level); // } // } // //var service = new ProductService(model.shopifyurl, model.token); // //List<Product> retlist = await new ProductHandler().ReturnProductModel(products); // //foreach (var item in retlist) // //{ // // var a = item.Variants.FirstOrDefault(); // // a.InventoryItemId = Convert.ToInt64("36257378205740"); // // a.InventoryQuantity = 500; // // a.InventoryManagement = "shopify"; // // a.InventoryPolicy = "deny"; // // var msg = await service.UpdateAsync(Convert.ToInt64(item.Id), item); // //} response = "success"; //} } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Ok(response)); }