0

I am using Store 2 as my e-commerce module, with the AJAX Form plugin to submit products to the cart.

My question is: Is there a way to detect if the item was added successfully and update the CSRF token?

I thought the below would work, but unfortunately not:

$(document).on('click', '.add-basket', function() {
    $('.add-basket-form').ajaxForm({
        success: updateCart,
        dataType: 'json'
    });
});

function updateCart(data, statusText, xhr) {        

    if (data.success) {                                                 
        $("input[name=csrf_token]").val(data.csrf_token);

        var url = 'http://www.domain.net/site/embed-mini-basket';

        $('#masthead-mini-basket').load(url, function() {  
            alert('success');
        }); 

    } else {

        alert('Failed');

    }
}

Thanks.

0

I'm currently not using Store for one of our projects. So I don't know the exact callback you're getting from the ajax call.

But you could store the token and the items in the cart in javascript variables. This makes it possible to compare them with possible new data. It's a bit of a workaround, but it could work...

You could do something like this in the updateCart function:

if(typeof data.csrf_token !== 'undefined' && typeof data.count !== 'undefined'){

    // checking if all data in callback

    if(data.csrf_token != current_csrf_token){
        // new token > change token in html
    }

    if(data.count != current_count){
        // changed shopping cart count > pretty sure there was an update?
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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