Add to Cart Using AJAX in Magento

addtocartjquery

I'm having a problem, I've placed an ajax Request in my cart, and she's doing what she has to do that is adding the product to the cart, but she does not redirect to the next page, she stands in the same place, so I added A submit function, but then add 2 products which would be what the functions has to do, how do I redirect to next screen without the submit?

view.phtml

            productAddToCartForm.submit = function(button, url) {
            if (this.validator.validate()) {
                var form = this.form;
                var oldUrl = form.action;

                if (url) {
                   form.action = url;
                }
                var e = null;

                if(!url){
                url = jQuery('#product_addtocart_form').attr('action');
                }
                url = url.replace("checkout/cart","ajax/index"); 
                var data = jQuery('#product_addtocart_form').serialize();
                data += '&isAjax=1';   
                jQuery('#ajax_loader').show();
                try {
                    jQuery.ajax({
                          url: url,
                          dataType: 'json',
                          type : 'post',
                          data: data,
                          success: function(data){
                                jQuery('#ajax_loader').hide();

                                if(jQuery('.block-cart')){
                                    jQuery('.block-cart').replaceWith(data.sidebar);
                                }
                                if(jQuery('.header .links')){
                                    jQuery('.header .links').replaceWith(data.toplink);
                                }
                          }
                    });
                } catch (e) {
                }

If I add this here it redirects the page, but adds 2 items

                this.form.submit();
                } catch (e) {
                }

Best Answer

Redirecting AJAX requests to another url does not affect the original page. The most simple solution would be to redirect using javascript on when AJAX result is returned.

success: function(data){
         //...
         //perform checks if needed
         window.location.href = '<?php echo Mage::getUrl("checkout/cart");?>';
}
Related Topic