Edit error message when customer adds more units than inventory quantity available

Hi all,

I am currently using a custom theme (Empire) and trying to change some of our key error messages on the site. I have been browsing the Edit Languages section, and was able to find the error message I want to change, but I can’t seem to make it work.

The error is showing when customers try to add more units of an item than the inventory available. I.e: a product has 5 units in stock, and the users adds 7 units to the cart.

The current error message shows: “You can’t add more units of {{ name }} to your cart as we don’t have that many in stock. Please re-adjust the quantity.

What I would like is for that error message to include the inventory quantity available. Currently the user has to try different quantities and hope it won’t show the error message, meaning we have enough stock left. This is definitely not the best user-experience.

I feel comfortable understanding Liquid and was able to make it work so it would show this info on my pdp with this code:

{% assign current_variant = product.selected_or_first_available_variant %}

We have {{ current_variant.inventory_quantity }} left in stock!

However, I don’t want to display the inventory quantity left on my pdp (for various reason). Hence why adding it to the error message seems the best option. But I just don’t know how to add this variable to the Edit Languages section. I’m thinking this needs to be done in .js via the cart API but I am not familiar with this at all.

I’d love to get some help on this please :slightly_smiling_face:

Hm, there’s multiple places it can be. A lot of the time you can find messages like this in the settings_schema.json file in your Config folder, but you wont be able to access the inventory quantity there. It just houses big objects of strings. Or it may be adding the message via javascript. Wouldn’t be able to tell you without interacting with it and might need access to your theme’s if it’s not JS, even if it was you don’t have access to inventory quantity with javascript so you’d need to output it via liquid and retrieve it with JS when the error happens.

Thank you @Ninthony for your quick reply!

You are correct there are some messages located in the Settings_schema.json file in my Config folder. The error messages are located in the Locales folder > en.default.json file. I think I understand what you are saying, but I am not familiar with .js and not sure how I can retrieve it with JS :/.

Here is what I have in the en.default.json:

"shopify": {
    "notices": {
      "cart": {
        "too_many_items_in_cart": "You can't add more units of {{ name }} to your cart as we don't have that many in stock. Please re-adjust the quantity.",
        "only_n_items_available": "You can only add {{ count }} units of {{ name }} to your cart as we don't have more in stock. Please re-adjust the quantity.",
        "all_items_in_cart": "All {{ count }} units of {{ name }} have already been added to your cart."
      }
    },

Any help would be greatly appreciated. Thank you!

Well it looks like there’s an error message for your specific case right there. This happens in the product page I assume. I would go to product-template.liquid and ctrl + f and search for:

“too_many_items_in_cart”, then you’ll probably see something like:

{{ 'shopify.notice.cart.too_many_items_in_cart' | t }}

Change that to the only_n_items_available version instead and see if that doesnt work for you.

{{ 'shopify.notice.cart.only_n_items_available' | t }}

@Ninthony , that would make a lot of sense, but unfortunately I can’t seem to find that information.

Our custom theme (Empire) has a very different structure than other “regular” theme. I.e: my product.liquid in Template folder looks like this:

{% section 'static-product' %}
{% section 'static-product-recommendations' %}
{% section 'static-product-reviews' %}
{% section 'static-recently-viewed' %}
{% render 'judgeme_widgets', widget_type: 'judgeme_review_widget', concierge_install: true, auto_install: true, product: product %}

Whenever I had to make changes to my pdp, I go to Snippets > Product.liquid and add code there (or make changes to existing code).

Long story short… I am not able to find the query you listed there. Here is an example of my pdp: https://orka.ca/collections/panels-and-breakers/products/leviton-100a-120-240v-30-circuit-30-spaces-indoor-load-center-and-door-with-main-breaker-model-lp310-74b. I only have (1) unit left, so if you try adding more, you will see the error message. But when accessing Dev Tool, I’m not able to locate where this part of the page is pulled from :confused:

Thanks again for your help!

Check your section file called “static-product.liquid” – if you want I can request access to your themes and take a look for you and see if I can’t get it figured out.

Hi Ninthony, we want to do the same thing on the dawn theme, have been looking everywhere but can’t seem to find this code anywhere. {{ ‘shopify.notice.cart.too_many_items_in_cart’ | t }}. Does this function work differently in dawn as when inspecting it seems to be under error message? Any help on this would be amazing!