ShopifySharp icon indicating copy to clipboard operation
ShopifySharp copied to clipboard

Product Services:UpdateAsync - (400 Bad Request) Write requests to inventory_quantity and inventory_quantity_adjustment are no longer supported.

Open madushann opened this issue 4 years ago • 8 comments

Loading the product using GetAsync, Product product = await productService.GetAsync((long)apiCall.Product.Id); product.Tags = newTags; product = await productService.UpdateAsync((long)product.Id, product);

calling the UpdateAsync gives below error, (400 Bad Request) Write requests to inventory_quantity and inventory_quantity_adjustment are no longer supported. Please use the Inventory Levels API.

Stack trace: at ShopifySharp.ShopifyService.CheckResponseExceptions(HttpResponseMessage response, String rawResponse) at ShopifySharp.ShopifyService.<>c__DisplayClass26_01.<<ExecuteRequestAsync>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at ShopifySharp.DefaultRequestExecutionPolicy.Run[T](CloneableRequestMessage request, ExecuteRequestAsync1 executeRequestAsync) at ShopifySharp.ShopifyService.ExecuteRequestAsync[T](RequestUri uri, HttpMethod method, HttpContent content, String rootElement) at ShopifySharp.ProductService.UpdateAsync(Int64 productId, Product product)

madushann avatar Mar 17 '20 22:03 madushann

I see that same issue has been raised on the Shopify api ruby gem repo. See https://github.com/Shopify/shopify_api/issues/702

This means it's a bug in Shopify side and not a problem with ShopifySharp.

clement911 avatar Mar 18 '20 07:03 clement911

Please see my comment here.

EmmaB avatar Mar 18 '20 08:03 EmmaB

Thank you @clement911 and @EmmaB

could you please help me to understand how to handle within shopifysharp?

found below threads in shopify, https://community.shopify.com/c/Shopify-APIs-SDKs/400-BadRequest-on-variant-save/m-p/602230/highlight/true#M40797 https://github.com/Shopify/shopify_api/issues/632

madushann avatar Mar 18 '20 12:03 madushann

@madushann Try this code out.

var productToUpdate = new Product {
    Id = product.Id,
    Tags = newTags,
    PublishedAt = product.PublishedAt ?? product.PublishedAt
};
var result = await productService.UpdateAsync(productToUpdate.Id.Value, productToUpdate);

markus-24 avatar Mar 18 '20 23:03 markus-24

@mark-jeanswhse Thank you very much.

I am going to try, just wondering will this update the product with missing data ?

madushann avatar Mar 19 '20 00:03 madushann

@madushann It's possible that it could mess up some product data, since many of the properties you don't set would be serialized as null. That's why @mark-jeanswhse is using PublishedAt = ... -- if that one is accidentally set to null, it will unpublish your product.

You should also be able to just set the InventoryQuantity property to null before you update the product, and that should get it working again.

var product = await productService.GetAsync(productId);

product.Tags = newTags;
product.InventoryQuantity = null;

product = await productService.UpdateAsync(productId, product);

I'm going to publish an update to the ProductService that will automatically do this behind the scenes for each update call, as I've had a handful of questions and emails about this error. It's too easy for us to accidentally shoot ourselves in the foot when trying to update products thanks to the property deprecation.

nozzlegear avatar Mar 19 '20 01:03 nozzlegear

Actually "InventoryQuantity" is in the ProductVariant, which need to be set iterating the variant list.

foreach (var variant in product.Variants) { variant.InventoryQuantity = null; } This worked for me for the moment.

madushann avatar Mar 20 '20 17:03 madushann

@nozzlegear

I'm going to publish an update to the ProductService that will automatically do this behind the scenes for each update call, as I've had a handful of questions and emails about this error. It's too easy for us to accidentally shoot ourselves in the foot when trying to update products thanks to the property deprecation.

Hope, Once you push the update will be available straight forward with newer version.

madushann avatar Mar 20 '20 17:03 madushann