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.
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.
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."
}
},
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 }}
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!