1

Currently, if a customer attempts to add a greater quantity of a product to the cart than is in stock, an "Unable to update cart" flash message in shown. e.g. there are 3 in stock and the customer tries to add 4 to their cart.

Is there any way to change what that error says? As obviously a customer isn't going to know what that means.

Thanks!

1 Answer 1

1

If you want to modify the error message, the way to do is not to use the craft.app.session.getFlash('error') value but use the cart.errors instead. Each of the errors has a unique key for specific error then you can add a condition on what message to display. It would something this.

{% if cart.errors|length %}
            <ul>
            {% for attribute, errors in cart.errors %}
               {% if attribute == "lineItems.0.qty" %}
                    {% for key, error in errors %}
                        <li>Product is not available</li>
                    {% endfor %}
                {% else %}
                    {% for key, error in errors %}
                        <li>{{ attribute }}: {{ error }}</li>
                    {% endfor %}
                {% endif %}
            {% endfor %}
            </ul>
{% endif %}

You could also modify the Unable to cart message by adding this to the update cart form E.g.

{{ hiddenInput('failMessage', 'Error adding cart.'|hash) }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.