Jump to content

(Solved) Ajax errors are not shown when adding product to cart on product page


Recommended Posts

Hello, I have a little problem with PrestaShop 8.1.0. On the product page, when you click the add to cart button, I correctly receive an error from ajax in the console (out of stock for example), my problem is that it doesn't show as before on the product page. That is, PrestaShop correctly does not add the product to the cart but does not show the customer anything warning him of the error. Is it an issue with this version of PrestaShop or should I program the script inside my module to display errors correctly? Thanks in advance.

Link to comment
Share on other sites

Hi how are things? This post has been around for some time and has no answers so since I found it myself, I'm posting it here for anyone who can help. Previously in the php of our module we have to have issued the corresponding errors when adding the product to the cart, likewise it would be necessary to make a previous verification that these are versions with this incidence: 8.0.0 or greater. It is simply adding the following code to the javascript of our module:

    if (typeof prestashop !== 'undefined') {
        prestashop.on('handleError',(function (e) {
            if (e.eventType == 'addProductToCart' && e.resp.hasError) {
                $('.alert.alert-danger.ajax-error').remove();
                var htt = '<div class="alert alert-danger ajax-error" role="alert">' + e.resp.errors[0] + '</div>';
                $(htt).insertAfter($('.product-quantity.clearfix'));
            }
        }));
        prestashop.on('updateCart', (function (e) {
            $('.alert.alert-danger.ajax-error').remove();
        }));
    }

I hope it works for you. Greetings and have a great day.

  • Thanks 1
Link to comment
Share on other sites

  • Yelish changed the title to (Solved) Ajax errors are not shown when adding product to cart on product page
  • 6 months later...

Thank you very much for posting this solution! It helped me a lot!!

 

 I wasn't aware of the eventType "addProductToCart", I think it's not on the prestashop documentation, wich is weird.

Anyway thank you again, I hope you have a great day! ;-)

Link to comment
Share on other sites

  • 4 weeks later...

Hi, sorry, I hadn't seen the post until now. In my case, the JavaScript was specific to the module we use for the product page. Of course, keep in mind that you have to properly trigger the error in PHP in your specific module. In our case, it was a product unit purchase limit verification, we triggered the error, but PrestaShop in versions 8- didn't display it properly under the add to cart button. Regards,

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...