Developer forum

Forum » Development » Ajax add to cart - error management

Ajax add to cart - error management

Gaëtan Di Caro
Reply

Hello,

I am adding several products to the basket via an ajax call :

var postData = {
    'cartcmd': 'addmulti',

    'ProductLoopCounter1' : 1,
    'ProductNumber1' : 1234,
    'Quantity1' : 1,

    'ProductLoopCounter2' : 2,
    'ProductNumber2' : 12345,
    'Quantity2' : 1

    // etc.
};

$.ajax({
    async: false,
    type: 'POST',
    url: '/Default.aspx',
    data: postData,
    error: function (data) {
        alert(data);
    },
    success: function (data) {
        location.reload();
    }
});

It works fine enough, but the problem is that I don't know how to get the errors. If there is a problem with one of the products, the http status is still ok, and the 'data' I get is a whole HTML page.

Is it possible to have a summary of the errors in a way that is easy to get in javascript ?

 

Thanks.


Replies

 
Nuno Aguiar
Reply

Hi Gaëtan,

 

We do that all the time. Just post it to a page ID that would render the errors, much like it would render the contents of the basket.

 

Best Regards,

Nuno Aguiar

 
Gaëtan Di Caro
Reply

Sorry for the late up, I'm only getting to do that now.

How do I do that concretely ? I've changed the url to post to a page with nothing in except template tags, but what I get in the "data" argument of my success function is simply the contents of the basket page itself. I've tried to add a cart paragraph to this page, same problem.

 
Gaëtan Di Caro
Reply

Does anyone have an idea ?

Thanks

 
Nicolai Pedersen
Reply

Hard to tell what you have done and where it is not working.

You can see how we implement this on wrap: http://wrap.dynamicweb-cms.com/produkter/bikes/mountain-bikes

Here it is ajax based.

BR Nicolai

 
Gaëtan Di Caro
Reply

Hi Nicolai,

In my interface, the user has 2 fields in which to type the product number and the quantity (repeated on as many lines are needed). The ajax call works fine, i.e the products are added to the basket. But what I need is a way to handle errors, mostly if the user types in a wrong product number. Right now it fails silently, the line is simply skipped.

Thanks

 

edit : Ok I see the way you do it is similar now. i'll get an eye on it

 
Nicolai Pedersen
Reply

ok. I am not sure I have any great ideas for that without digging into your code.

 
Gaëtan Di Caro
Reply

I've tried to make it more like on the wrap, but still no luck. When I show my json page, I do get json (I use the JsonCart.html from the wrap). But when I post my data to it, I get an html page which is actually the basket. Is there something about using post instead of get like you do on the wrap ?

Here's my js code :

$('#direct-add').click(function () {
        var postData = {
            'cartcmd': 'addmulti'
        };

        var rows = $('.add-to-basket-line:visible');
        for (var i = 0; i < rows.length; i++) {

            var productNumber = $(rows[i]).find('input.input-product-number').val();
            var quantity = $(rows[i]).find('input.input-quantity').val();

            if (productNumber !== '') {
                postData['ProductLoopCounter' + (i + 1)] = (i + 1) + '';
                postData['ProductNumber' + (i + 1)] = productNumber;
                postData['Quantity' + (i + 1)] = quantity;
            }
        }

        $.ajax({
            async: false,
            type: 'POST',
            url: '/Default.aspx?ID=' + BasketSettings.DirectAddPageId,
            data: postData,
            error: function (data) {
                alert(data);
            },
            success: function (data) {
                alert(data);
            }
        });
    });

 
Gaëtan Di Caro
Reply

Ok i do manage to get proper json now. However if I put a wrong product number, where do i get the error in the template tags ? I've looked through them and through the loops and I can't see anything.

Here's what my url look like (with a bogus product number) : /Default.aspx?ID=9220&cartcmd=addmulti&ProductLoopCounter1=1&ProductNumber1=fsfdsfs&Quantity1=1

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Gaëtan

I do not think there is an error in the template. If the product number does not exist, it will not add a line, and that is that...

BR Nicolai

Votes for this answer: 1
 
Gaëtan Di Caro
Reply

Ok, that's what I wanted to know to begin with. I'll code something myself then, but it would be nice in the future to have more (and easier) possibilities to implement json solutions for controlling the flow (maybe by having some default web services ?).

Several of our B2B customers have requested the feature I'm making right now and I guess it's not the last time i'll have to manipulate the basket via ajax.

Thanks.

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Guys,

A message that product does not exist would be helpful also for Reorder functionality where you don't get any feedback about what was added to cart or if it was added to cart.

Might be a bit of performance hit though, if the add to cart will also check the validity of the product.

Either way, it's a compromise. Better performance or better feedback.

Adrian

 

You must be logged in to post in the forum