Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Product Variant Inventory Quantity error #286

Closed
bennybennybenny opened this issue Aug 29, 2018 · 15 comments
Closed

Update Product Variant Inventory Quantity error #286

bennybennybenny opened this issue Aug 29, 2018 · 15 comments
Labels

Comments

@bennybennybenny
Copy link

I have write the following function to update the inventory quantity

    static async Task<ShopifySharp.ProductVariant> UpdateProductVariant(long variantId, int qty)
    {
        var service = new ProductVariantService(ShopifyURL, Token);

        ShopifySharp.ProductVariant pv = new ShopifySharp.ProductVariant();
        pv.InventoryQuantity = qty;
        var variant = await service.UpdateAsync(variantId, pv);

        return variant;
    }

However it returns an error with invalid quantity request.
Could anyone help on this?

@clement911
Copy link
Collaborator

InventoryQuantity is read-only. I guess the property should be changed to be a getter only.

/// <summary>
        /// The number of items in stock for this product variant.
        /// NOTE: After 2018-07-01, this field will be read-only in the Shopify API. Use the `InventoryLevelService` instead.
        /// </summary>
        [JsonProperty("inventory_quantity")]
        public int? InventoryQuantity { get; set; }

@bennybennybenny
Copy link
Author

Do you have any example on the InventoryLevelService?
Many thanks

@dkershner6
Copy link
Contributor

Variant contains inventory item Id, you use that and a location id to set inventory level.

@mattxo
Copy link

mattxo commented Sep 20, 2018

@bennybennybenny

foreach(var lineItem in order.LineItems)
{
    var variant = await _productVariantService.GetAsync(lineItem.VariantId.Value);

    var ids = new List<long>()
    {
        variant.InventoryItemId.Value
    };

    var inventoryLevelFilter = new InventoryLevelFilter()
    {
        InventoryItemIds = ids
    };

    var levels = await _inventoryLevelService.ListAsync(inventoryLevelFilter);
    var level = levels.Single();
                    
    level.Available -= lineItem.Quantity;

    await _inventoryLevelService.SetAsync(level);
}

@nozzlegear nozzlegear added the bug label Oct 9, 2018
@jhunexjun
Copy link

Hello, is there an updated way to do this? I'm using version 5.7.0. Looks like the preceding example solution isn't the thing for my current version.

@eight1echo
Copy link

@jhunexjun This worked for me:

        var variant = await variantService.GetAsync(variantId);

        var filter = new InventoryLevelListFilter()
        {
            InventoryItemIds = new List<long>()
            {
                variant.InventoryItemId.Value,
            }
        };

        var levels = await inventoryService.ListAsync(filter);
        var level = levels.Items.First();

        level.Available = newQuantity;

        await inventoryService.SetAsync(level);

I think the only difference from above is InventoryLevelListFilter instead of InventoryLevelFilter which appears to no longer exist (?)

@GUEN-SKG
Copy link

Hello,

I tried the code as above and everything works fine up until the SetAsync then I get a 404.

When I look at the RequestMessage I see this

{Method: GET, RequestUri: 'https://xxxxx.myshopify.com/admin/api/2021-10/inventory_levels/set.json', Version: 1.1, Content: , Headers:{ X-Shopify-Access-Token: xxxxxxxxxxxxxxx Accept: application/json}}

Am I missing something here or doing something wrong?

Any help would be appreciated,

Guillaume

@GUEN-SKG
Copy link

Hello,

Just tried something to see the results

var service = new InventoryLevelService(domain, accessToken);
var invList = await service.ListAsync(new InventoryLevelListFilter() { InventoryItemIds = new List() { 44764286812406 } });

Console.WriteLine($"InventoryItemId : {invList.Items.First().InventoryItemId} LocationId : {invList.Items.First().LocationId} Available : {invList.Items.First().Available}");

var inv = await service.SetAsync(new InventoryLevel() { InventoryItemId = 44764286812406, LocationId= 66762801398, Available = 5 });

InventoryItemId and LocationId copied from the Console.WriteLine to the SetAsync but still the samen 404.

If I send this body with Postman ( POST )
{"location_id":66762801398,"inventory_item_id":44764286812406,"available":5}
to the endpoint
https://xxxxxxxxxx.myshopify.com/admin/api/2021-10/inventory_levels/set.json

Then the available gets updated correctly.

Is this a bug or am i still missing something?

Guillaume

@nozzlegear
Copy link
Owner

nozzlegear commented Apr 25, 2022 via email

@jhunexjun
Copy link

For my issue above for version 5.7.0 this is how I do it.

InventoryLevel response = await _inventoryLevelService.SetAsync(new InventoryLevel
 {
          LocationId = location.Id,
          Available = 100,
          InventoryItemId = productVariant.InventoryItemId
});

To get the productVariant.InventoryItemId unless you know:
ProductVariant pv = await _productVariantService.GetAsync(shopifyVariantId);

@GUEN-SKG
Copy link

Hello Joshua,

Thanks for the quick reply.
I noticed the Get in the response but wasn't sure if that was the problem because I kept getting the 404 and I would expect a Bad Request.

I'll leave the change up to you and will make a small workaround for the moment until the update is available.
Strange that nobody reported this issue, updating inventory seems like something that I have in almost all my project were the ERP is connecting to some kind of webshop.

Thanks again for the quick reply.

Guillaume

@dnatabar
Copy link

Greetings.

I've had a similar problem as @GUEN-SKG, although this was not due to a GET being sent instead of a POST, but i did get a 404 despite the endpoint being "correct", and it worked perfectly in Postman.

I'm just posting here to mention it for others that might have similar issues, as i don't believe this is an issue on ShopifySharps end, but rather on either Shopify's end or the redirection happening between a domain and the shopify shop.

We've had a customer that just could not get their inventory updated in Shopify when they had changed their stock in our system, and it worked for other customers fine.
Yet everything else worked fine for them, and i had already ruled out permissions/scope.

The base URL to their shop was set to their regular domain, such as myepicshop.com, which loads their shopify shop at myepicshop.myshopify.com .

This worked without a hitch for everything we use the ShopifyAPI for, EXCEPT InventoryLevel.

InventoryLevel always returned a 404 no matter what, even though the requestURI that the HttpClient sends too is the correct one, as in, https://myepicshop.myshopify.com/api/etc.etc.
The url if tried in Postman have no issues, and after changing their base url to be directly to the xxx.myshopify.com url, there was no longer any issues for ShopifySharp either.

Again, i believe this is more of an issue on the redirection part of the initial domain, but either way, figured i would post it here, in case others ran into this issue.

//Ronnie

@nozzlegear
Copy link
Owner

@dnatabar Thank you for the information! That's super helpful, I'm going to add a note about that to the readme. I've seen this myself several times either in my own projects or while helping others diagnose problems. It's a mystery why some endpoints work on "real" domains and some don't.

@ajvanlaningham
Copy link

LocationId

This worked for me. Something worthy of note and why I struggled with this for a long time was that the store for whom I was attempting to update inventory did not have a store location set up and the location_id field was challenging to find. I eventually found it by navigating to their admin dashboard -> settings -> location -> shop location and then pulling it from the end of the URL.

@Joyjk
Copy link

Joyjk commented Sep 25, 2023

@jhunexjun This worked for me:

        var variant = await variantService.GetAsync(variantId);

        var filter = new InventoryLevelListFilter()
        {
            InventoryItemIds = new List<long>()
            {
                variant.InventoryItemId.Value,
            }
        };

        var levels = await inventoryService.ListAsync(filter);
        var level = levels.Items.First();

        level.Available = newQuantity;

        await inventoryService.SetAsync(level);

I think the only difference from above is InventoryLevelListFilter instead of InventoryLevelFilter which appears to no longer exist (?)

Thank you it working fine. Thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests