среда, 8 мая 2013 г.

среда, 1 мая 2013 г.

Adding products to cart using AJAX

JS functions:
    function processNext(urls, i) {
            var next = i + 1;
            //jQuery('processing-text').update('Processing item ' + next);
            if (next < urls.size()) {
                new Ajax.Request(urls[i], {
                  method: 'get',
                  onComplete: function(transport) {
                    processNext(urls, next);
                  }
                });
            } else {
                new Ajax.Request(urls[i], {
                  method: 'get',
                  onComplete: function(transport) {
                      window.location.href = "/checkout/cart/";
                  }
                });
            }
        }

        function addItemsToCart() {
            jQuery('#add-items-to-cart').hide();
            jQuery('#loading').show();
            jQuery('#add-items-to-cart-col').hide();
            jQuery('#loading-col').show();
            var addToCartUrls = [];
            jQuery('input.add').each(function(){
                if(jQuery(this).is(":checked")) {
                    var id = jQuery(this).attr('id').split('_')[1];
                    var urlo = jQuery("input[name='url_"+id+"']").val();
                    var qtylo = jQuery("input[name='qty_"+id+"']").val();
                    if (qtylo < 1) qtylo = 1;
                    addToCartUrls.push(urlo + 'qty/' + qtylo);

                }
            });
  if (addToCartUrls.size() > 0) {
                processNext(addToCartUrls, 0);
            } else {
                jQuery('add-items-to-cart').show();
                jQuery('waiting').hide();
            }
        }