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

Support for inventory quantity #753

Closed
peepniit opened this issue Apr 15, 2020 · 27 comments
Closed

Support for inventory quantity #753

peepniit opened this issue Apr 15, 2020 · 27 comments

Comments

@peepniit
Copy link

Hey, great job maintaining the libarary!

Curious to hear when could we access the product variant's quantityAvailable field?
https://shopify.dev/changelog/inventory-quantity-available-on-storefront-graphql-api

Link to API reference

@arobbins
Copy link

I was just looking for this last night. Support for this would be amazing!

Is anyone taking pull requests to make this happen?

@arobbins
Copy link

arobbins commented Apr 15, 2020

Quick update on this.

I've forked the repo and followed the contribution guide to attempt a pull request for this change. I'm assuming it simply requires updating the VariantFragment and graphql schema. however I'm not too confident about that :)

I've tried updating the package locally with the above changes but still receive the error:

Field 'quantityAvailable' doesn't exist on type 'ProductVariant'

I'm guessing that in addition to these changes, Shopify needs to "enable" these fields on the API server.

I wonder if @StefanSlehta or @rebeccajfriedman could offer some guidance on which files to edit. Happy to do the work to make it happen.

@StefanSlehta
Copy link
Contributor

Hi, Sorry for the delayed response

@arobbins The issue is that we're still on 2020-01 schema, which doesn't support the quantityAvailable field. I've opened a PR to update the api version which should allow you to pull this field in and open a PR for it!

@arobbins
Copy link

@StefanSlehta Awesome thank you!

@StefanSlehta
Copy link
Contributor

Hey just an update, you can also expand the queries you'd like to use instead of adding to the VariantFragment .
We'll have to determine if adding quantityAvailable for everyone makes sense at the moment, mainly because fetching surplus fields starts slowing down the library for everyone even if they don't need all the fields. By expanding certain queries you can fetch exactly the things you need and still keep it relatively quick.
Hope this helps!

@arobbins
Copy link

arobbins commented Apr 24, 2020

@StefanSlehta

Thanks for the direction.

I managed to get the quantityAvailable field pulled in successfully this morning. I'm using the unoptimized UMD bundle and expanding the main query like you mentioned. Works great!

However I do have another question:

  1. To get this to work, I needed to fetch the master branch, run a build, and then include the UMD bundle that way. Can we get this change pushed to a new 2.9.4 release? That way I can continue importing the package like normal. I definitely don't want to include an unoptimized build in my final product.

Since I'm expanding the query I wouldn't need to submit a PR.

Thanks again!

@StefanSlehta
Copy link
Contributor

Just updated to 2.10.0, npm is there, CDN might take up to ~24h to bust the cache.

@arobbins
Copy link

You rock!

@wirsing
Copy link

wirsing commented Jun 8, 2020

@arobbins @StefanSlehta Thanks for this! Would you guys mind posting a short example on how to expand the query to pull the inventory field? I checked the link but can't get the query right. Thanks!

@obelmont
Copy link

@arobbins @StefanSlehta Thanks for this! Would you guys mind posting a short example on how to expand the query to pull the inventory field? I checked the link but can't get the query right. Thanks!

I'm building a static site (nextjs) that uses a useEffect hook to pull the available qty.
Below is the extended query I use to get this data.
Keep in mind that if the quantityAvaliable returns null it just means that the product's qty is not tracked!

const productAvailableQty = (handle) => {
  const qtyQuery = shopify.graphQLClient.query((root) => {
    root.add(
      "productByHandle",
      { args: { handle: `${handle}` } },
      (product) => {
        product.add("title");
        product.add("handle");
        product.add("id");
        product.addConnection(
          "variants",
          { args: { first: 99 } },
          (variant) => {
            variant.add("id");
            variant.add("availableForSale");
            variant.add("quantityAvailable");
          }
        );
      }
    );
  });
  return shopify.graphQLClient
    .send(qtyQuery)
    .then((res) => JSON.parse(JSON.stringify(res.model.productByHandle)))
    .catch(() => null);
};

@50bbx
Copy link

50bbx commented Oct 6, 2020

@obelmont what do you mean by saying that the quantity is not tracked?

@obelmont
Copy link

@50bbx It means that the product's available inventory is not tracked in Shopify. It's an option in the Inventory section of each product in Shopify's admin panel that is used to determine available stock to ensure you don't sell more than you have.

@socreative
Copy link

socreative commented Feb 4, 2021

@obelmont I get
No field of name "quantityAvailable" found on type "ProductVariant" in schema

when I run the query :(

@ktaftaf
Copy link

ktaftaf commented Mar 16, 2021

@socreative try inventoryQuantity

@city17
Copy link

city17 commented Mar 24, 2021

@ktaftaf That's for the regular Shopify API, Storefront uses quantityAvailable.

@socreative I also get the same: Error: No field of name "quantityAvailable" found on type "ProductVariant" in schema.

I do have the correct access in the Shopify App as I can query the quantityAvailable field with a standard GQL query at build time.

@obelmont Could you confirm that your code is still working for you? Trying the same code results in the schema error for me...

EDIT: Found the answer, you need to change from the standard SDK to the unoptimized one (the last one here: https://github.com/Shopify/js-buy-sdk#builds)

@iamprofessork
Copy link

@obelmont I tried to use your code and i am getting quantityAvailable: null . I have already turned on "Track quantity" for the product. Do you happen to know why?

@kolorfilm
Copy link

I have the same problem. Tried to access quantityAvailable from variant and it seems it is not in there.

@arobbins
Copy link

I wonder if this is related to: Shopify/storefront-api-feedback#101

@owystyle
Copy link

owystyle commented Nov 3, 2022

Found the issue for quantityAvailable: null. You need to have access to read_inventory scope for your access token https://shopify.dev/api/usage/access-scopes

@Mritunjay004
Copy link

I have provided both read_inventory access in the storefront and tracking the quantity still getting quantityAvailable: null. Someone help please.

@kolorfilm
Copy link

There is also no quantityAvailable within the data item.variant. There isn't even quantityAvailable at all.

Bildschirm­foto 2023-01-19 um 17 28 56

I gave access to the prop unauthenticated_read_product_inventory of course.

Any ideas?

@walton-alex
Copy link

No field of name "quantityAvailable" found on type "ProductVariant" in schema
or
No field of name "inventoryQuantity" found on type "ProductVariant" in schema

Any ideas?

@Mritunjay004
Copy link

Mritunjay004 commented May 16, 2023 via email

@walton-alex
Copy link

@Mritunjay004 As in un-install then re-install the app in the Shopify apps area?

@Mritunjay004
Copy link

Mritunjay004 commented May 16, 2023 via email

@Mritunjay004
Copy link

Mritunjay004 commented May 16, 2023 via email

@mwrigley
Copy link

The issue for me was I had to have 'unauthenticated_read_product_inventory' included in my scopes. This is not in the documentation, but was showing as an error in the response.

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

No branches or pull requests