Fixing Missing Error Messages on Shopify Cart Pages
When customers try to exceed product quantity limits on your Shopify store, they should receive clear error messages explaining why their action failed. However, many themes fail to display these notifications on the cart page. The result? Confused customers who can't understand why their quantity adjustments aren't working.

Why cart error messages sometimes disappear
The problem occurs because of a disconnect between Shopify's Storefront API and how themes display error responses. When a customer tries to add too many items:
- The API correctly returns a 422 error response with an explanatory message.
- The quantity doesn't increase, which is the expected behaviour, but the error message that should inform the customer is not displayed.
However, many themes fail to capture and display the error message returned by the API. This means that while the functional aspect of preventing the quantity increase works correctly, the error message that should inform the customer of the issue is not shown. As a result, customers are left confused about why their quantity adjustments aren't working.
A simple fix for any Shopify theme
Fortunately, this problem can be solved with a simple JavaScript snippet that intercepts API responses and displays the error messages properly. Here's how to implement it:
- Go to your Shopify admin, then Online Store > Themes
- Click Actions (3 dots beside customize) > Edit code on your active theme
- Find your cart template file—typically
templates/cart.liquid
orsections/cart-template.liquid
(this varies by theme) - Add the following code at the end of the file, just before the closing
{% schema %}
tag (if present) or at the end of the file:
<script>
(function() {
// Save the original open method
const originalOpen = XMLHttpRequest.prototype.open;
// Override the open method to intercept requests
XMLHttpRequest.prototype.open = function(method, url) {
// Listen for the load event on the XMLHttpRequest instance
this.addEventListener('load', function() {
// Check if the request is to /cart/change.js or /cart/add.js and is a POST request
if ((url.includes('/cart/change.js') || url.includes('/cart/add.js')) && method.toUpperCase() === 'POST') {
// Check if the response status is 422
if (this.status === 422) {
try {
// Parse the response text to get the error message
const response = JSON.parse(this.responseText);
alert(response.message || 'An error occurred. Please try again.');
} catch (error) {
console.error('Error parsing response:', error);
}
}
}
});
// Call the original open method with the provided arguments
return originalOpen.apply(this, arguments);
};
})();
</script>
This code works by monitoring all XMLHttpRequest calls to Shopify's cart endpoints. When it detects a 422 error (validation failed), it extracts the error message and displays it to the customer using a standard alert.
Enhancing the user experience
While the code above uses a simple JavaScript alert, you might want a more polished solution. A styled notification can improve the user experience by providing a more professional and visually appealing error message.
<script>
(function() {
// Create styles for the notification
const style = document.createElement('style');
style.textContent = `
.cart-error-notification {
position: fixed;
top: 20px;
right: 20px;
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
border-radius: 4px;
padding: 12px 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
z-index: 10000;
animation: slideIn 0.3s ease-out forwards;
}
@keyframes slideIn {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
`;
document.head.appendChild(style);
// Function to show custom notification
function showErrorNotification(message) {
const notification = document.createElement('div');
notification.className = 'cart-error-notification';
notification.textContent = message;
document.body.appendChild(notification);
// Remove notification after 5 seconds
setTimeout(() => {
notification.style.opacity = '0';
notification.style.transform = 'translateX(100%)';
notification.style.transition = 'all 0.3s ease-in';
setTimeout(() => notification.remove(), 300);
}, 5000);
}
// Save the original open method
const originalOpen = XMLHttpRequest.prototype.open;
// Override the open method to intercept requests
XMLHttpRequest.prototype.open = function(method, url) {
// Listen for the load event on the XMLHttpRequest instance
this.addEventListener('load', function() {
// Check if the request is to /cart/change.js or /cart/add.js and is a POST request
if ((url.includes('/cart/change.js') || url.includes('/cart/add.js')) && method.toUpperCase() === 'POST') {
// Check if the response status is 422
if (this.status === 422) {
try {
// Parse the response text to get the error message
const response = JSON.parse(this.responseText);
showErrorNotification(response.message || 'An error occurred. Please try again.');
} catch (error) {
console.error('Error parsing response:', error);
}
}
}
});
// Call the original open method with the provided arguments
return originalOpen.apply(this, arguments);
};
})();
</script>
This enhanced version creates a styled notification that slides in from the right side of the screen and automatically disappears after five seconds. It looks more professional and integrates better with most store designs.

Testing your implementation
After adding the code to your theme, follow these steps to ensure it's working correctly:
- Set up a product with a maximum quantity limit (through inventory constraints or DC Cart + Order Limits).
- Add it to your cart.
- Try to increase the quantity beyond the limit.
- You should now see an error message explaining why the quantity can't be increased.
Note: If your changes don't appear immediately, try clearing your browser cache or creating a fresh test order.
Beyond simple alerts
For the best customer experience, consider these additional enhancements:
- Customize error messages through your app settings (if available)
- Style the notification to match your store's branding
- Add different notification styles for different types of errors
- Consider adding visual indicators next to the quantity inputs that have reached limits
By implementing proper error handling on your cart page, you'll reduce customer confusion, lower support requests, and create a smoother shopping experience that helps convert more sales.