📣 The forum has moved to GitHub

We’ve switched to GitHub Discussions as the hub for our community. This will improve the experience of collaborating for everyone, sharing what you’re working on, and discussing ideas for how Swell can be improved. Existing threads will remain open, but new posts are disabled.

Go to GitHub →

Adding quantity to cart that does not exist

Is this up to us as frontend developers, or is it a missing feature in your api?

But what I'm used to from earlier systems is that it is not possible to add items to cart if the submitted quantity exceeds product stock levels.

As it is now, I can add infinite amount of items, even though I've only set product quantity to 1.

Answers

  • Hi @Johan Åhman ,


    Can you kindly confirm if you are experiencing this on a certain product or through out all your available products?

    I have tried replicating your concern on my store but was unsuccessful, system is throwing me an error when trying to add a product on my cart with more than what is available on stock.


    Also, would like to confirm if "allow purchase when out of stock" is off for this product?

    You can check it in the dashboard>products>"product name">inventory>allow purchase when out of stock

    Please see below for your reference.


  • That's exactly how I have it:

    But still:


  • This get's even better:

    const res = await swell.cart.addItem({
      product_id: id,
      quantity: 349
    })
    


  • I can add whatever amount of quantity I like, even though I've clearly set "4" as quantity on the product itself, as you can see.

  • Any news on this one?

  • @Arjay @Bong De Guzman

    Will you fix this? Or is this, as many other bugs, expected to be taken care of by us frontend devs? Feels hacky, backend should throw an error trying to add stuff that does not exist.

    What happens if 2 ppl are trying to buy the last item of a product at the same time? It's impossible for me to know that.

  • @Steven Knobles this is exactly the fatigue I'm talking about. This thread has been stale since 31 January. 3 months, not even a reply from you guys. Honestly... idk.

  • @Arjay I guess you're referring to your own frontend? I'm talking about adding items through API. I could absolutley do a test where I request each product in cart, with every added item and compare stock levels to cart amount, but that will be heavy on both the client and in your backend.

    Why not just add an message to the initial API response from swell.cart.addItem, with a status that we can read?

  • Yehuda FRuchter
    edited May 2022

    @Johan Åhman Curious what your solution is for this? We have the same issue and were thinking of just comparing the stock level to cart amount in the client. Before actually executing the API call to add to cart, we would do a quick compare. Data for inventory will be sent on the product page load. Have you done something like this? I'm curious why you think it would be "heavy" on the client? the inventory quantity is already on the client, so it's just a quick comparison between 2 numbers. BTW, I agree this should be part of the API core. Sort of a pain to have to code this client side. API should take care of something like this.

  • I currently have no solution. I'm running a store with many products with small quantities. Almost each product is "limited" and therefor rare. This opens up for a real mess for us if 2 customers or more are out for the last item of any sort.

    Since it's impossible for me to control this in any other way than going through backend, I'm left with nothing.

    Any suggestions is really appreciated.

  • @Johan Åhman Which frontend are you using for your store? I worked on this yesterday, and think I'll code it today/tomorrow in NextJS with useSwr for data fetching. Actually, did a test run and it wasn't too complicated (though still a real pain to have to code this ourselves) and I think with useSwr it will be fast and very accurate client-side, assuming a site doesn't have ordering every few seconds.

  • If you are using NextJS you can call the function below with useSWR to get an updated quantity and compare to the quantity added to the cart. This is not a full solution but a basic idea to get started. hope it helps someone.

    const { data: inventoryData, error: inventoryError } = useSWR([product.id, variant?.id], checkInventory)

    /*the checkInventory function*/

    export const checkInventory = async (productId: string, variantId: string) => {

      let data = await swell.products.get(productId)

      //just set a default stock level if nothing is available

      const defaultStockLevel = 10

      let stockLevel

     //***ERROR CHECKING

      if (!data || data.errors) {

        //if we have no data, we just use our default, which can be changed. We use 10

        return defaultStockLevel

      }

    if (data?.variants?.count === 0) {

        stockLevel = data?.stock_level

      } else {

        //filter variants to find the specific variant we want, if array is corrupted for some reason, send back a default

        if (Array.isArray(data?.variants?.results)) {

          const dataVariant = data?.variants?.results.filter((item:any)=> item.id === variantId)

          stockLevel = dataVariant?.[0]?.stock_level

        } else {

          stockLevel = defaultStockLevel

        }

       

      }

      return stockLevel ? stockLevel : defaultStockLevel

    }

  • Thank you for example, but yes, this is not optimal. I'm running sveltekit so I could do an almost clone of this, but it is still no good. This info should be returned in the response from initial addtocart call.

  • Could anyone please elaborate on this? Am I too lazy / comfortable here? I would really like to depend on backend info about these kinds of events, rather than me checking this on clientside.

  • @Jesse B Should I forget about this request? It surely feels like an easy fix to respond with an status if you're trying to add more items than availiable. If we have a high traffic moment on a specific item, this info will not be visible until the customer has entered their credentials, but if it was handled in backend, the response would be immediate as the customer try to add it to the cart.

    But if I'm the only one with these concerns, we can close this thread.

Sign In or Register to comment.