Reputation: 11
I am working with the BigCommerce API with a REST service that I created and I've been able to successfully update a product's quantity using their product ID for each individual product. This is an example of how I am doing it:
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(username, api_key);
handler.Proxy = null;
handler.UseProxy = false;
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("MyStoresURL");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string productInventory = "My Product Inventory";
var setQty = new { inventory_level = productInventory };
string productID = "Product ID to be updated";
string url = string.Format("products/{0}", productID);
var response = client.PutAsJsonAsync(url, setQty).Result;
//StatusCode: 200 "OK"
The problem is that with 20,000 products the process can take hours. I am looking for a way to update all product inventory at once or at least in batches. All BigCommerce documentation explains how to update individual products or orders, customers etc, but not all at once.
I find this odd, since you can manually upload a CSV file with all your products through your store's web portal and BigCommerce will update your inventory with that. I am looking for a way to do this automatically. Here is an example of what I've tried which resulted in a status 400 "bad request".
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(username,
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("My Store");
handler.Proxy = null;
string url = string.Format("products.json?page={0}", counter);
HttpResponseMessage response = new HttpResponseMessage();
List<ProductQTY> productQty = new List<ProductQTY>();
response = client.GetAsync(url).Result;
//This returns a json string with the first 50 products
//and all their attributes including Quantity.
productQty = response.Content.ReadAsAsync<List<ProductQTY>>().Result;
//This converts the response into a list of 50 Products.
//Here I update the quantity of each product.
string json = Newtonsoft.Json.JsonConvert.SerializeObject(productQty);
//convert my product list back into json.
response = client.PutAsJsonAsync(url, json).Result;
//StatusCode: 400 "Bad Request"
The preceding code would go in a loop that would download 50 products from each page.
I have also tried uploading that 50 count json string that I downloaded right back to the same url and it never works.
In short. This succesfully updates a product's inventory using HTTPClient
:
string inventory = "10";
string bcID = "10001";
var setInv = new { inventory_level = inventory };
string url = string.Format("products/{0}", bcID);
var response = client.PutAsJsonAsync(url, setInv).Result;
So is there even a way to update more than one product at a time?
Upvotes: 0
Views: 567
Reputation: 1
Its not batching, but you could bring your time down considerably by updating multiple (4,8,etc) items at a time. I am researching using the API myself, and came across this post. Guess I will plan on using ASYNC/AWAIT
Upvotes: 0
Reputation: 11
Sharing this because somebody may have the same issue. I know I've searched high and low for this information but could not find it.
I got the answer directly from BigCommerce support. I had asked before but was not given an answer because we weren't using their API support package, but a helpful person from BC finally gave me the answer. To quote:
"It is not currently possible to update multiple product inventories at once via the API. It can only be done product by product."
They did mention that they are working on adding this feature in the future.
So that's that. I will either update my inventory manually or I'll see if the wait time is acceptable.
Upvotes: 1