In this episode, we're tackling an important part of any e-commerce cart—making sure users know if the quantity of products in their cart has changed due to stock level updates. We start by adding an item (in this case, some Nike Air Force 1s) to our cart and playing around with the quantity. Then, we simulate a stock change directly in the database to see how our cart reacts when the stock drops while the user is shopping.
You'll see the hiccup where our cart still displays the original quantity, even though it's no longer available. This helps us realize the need for checking and syncing cart quantities each time the cart is shown or the user attempts to check out.
From there, we jump into the code. We go through adding a method to verify if there’s enough stock for each product in the cart. If the requested quantity exceeds available stock, we throw a new, custom exception so we have something specific to catch and handle.
By the end of the episode, our app will throw this custom exception if someone has items in their cart that are no longer available in those quantities. In the next episode, we’ll take this further and update the cart so it automatically syncs and reduces quantities to what’s actually available, keeping the user in the loop about any changes!