jaimex8
12/9/2015 - 10:54 PM

AJAX add to cart CTGY

AJAX add to cart CTGY

        // ---- AJAX Add To Cart CTGY ---- //
        function addToCart () {
            $('.js-add-to-cart').on('click', function (e) {
                var purchaseForm = $(this.form);
                // Check the form is not currently submitting
                if (purchaseForm.data('formstatus') !== 'submitting') {
                    // Set up variables
                    var form = purchaseForm,
                        formData = form.serialize(),
                        randomNo = Math.ceil(Math.random() * 1000000), // IE Hack: Creating random number to refresh ajax call
                        formUrl = form.attr('action') + '&v=' + randomNo,
                        formMethod = form.attr('method'),
                        responseMessage = $('#js-purchase-message'),
                        miniBasket = $('#js-mini-basket-container'),
                        processingImage = $('#js-processing-purchase'),
                        purchaseButton = $(this);
                    
                    // Add status data to form
                    form.data('formstatus', 'submitting');
                    
                    // Show processing message
                    processingImage.show();
                    purchaseButton.toggleDisabled().val('Processing...');
                    responseMessage.html('').hide();
                    
                    // Send data to server for validation
                    $.ajax({
                        url: formUrl,
                        type: formMethod,
                        data: formData,
                        success: function(data, textStatus, jqXHR) {
                            if (data.search(/id="js-BASK"/i) != -1) {
                                $('html, body').animate({scrollTop: '0px'}, 250);
                                var responseMiniBasket = $(data).find('#js-mini-basket-container'),
                                    miniBasketCount = responseMiniBasket.contents()[1].getAttribute('data-itemcount'),
                                    miniBasketSubtotal = ' ' + responseMiniBasket.contents()[1].getAttribute('data-subtotal'),
                                    miniBasketLinkCount = $('#js-mini-basket-count, #js-mobile-basket-button .notification'),
                                    miniBasketLinkSubtotal = $('#js-mini-basket-subtotal');
                                
                                miniBasketLinkCount.text(miniBasketCount); // Update basket quantity (display only)
                                miniBasketLinkSubtotal.text(miniBasketSubtotal); // Update basket subtotal (display only)
                                
                                miniBasket.html(responseMiniBasket.contents()).addClass('open');
                                setTimeout(function () {
                                    miniBasket.removeClass('open');
                                }, 5000);

                                // Re-Initialize Attribute Machine (if it is active)
                                if (typeof attrMachCall !== 'undefined') {
                                    attrMachCall.Initialize();
                                };
                            }
                            else if(data.search(/id="js-PATR"/i) != -1) {
                                var missingAttrs = [];
                                form.find('.required').each(function () {
                                    missingAttrs.push(' ' + $(this).attr('title'));
                                });
                                responseMessage.html('All <em class="red">Required</em> options have not been selected.<br />Please review the following options: <span class="red">' + missingAttrs + '</span>.').fadeIn().delay(5000).fadeOut();
                            }
                            else if(data.search(/id="js-PLMT"/i) != -1) {
                                responseMessage.html('We do not have enough of the Size/Color you have selected.<br />Please adjust your quantity.').fadeIn().delay(3000).fadeOut();
                            }
                            else if(data.search(/id="js-POUT"/i) != -1) {
                                responseMessage.html('The Size/Color you have selected is out of stock.<br />Please review your options or check back later.').fadeIn().delay(3000).fadeOut(); 
                            }
                            else {
                                responseMessage.html('Please review options.').fadeIn().delay(3000).fadeOut();
                            };
                            
                            // Hide processing message and reset formstatus
                            processingImage.hide();
                            purchaseButton.toggleDisabled().val('Add to Cart');
                            form.data('formstatus', 'idle');
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        }
                    });
                };
                // Prevent form from submitting
                e.preventDefault();
            });
        };
        var addToCart = new addToCart;