(function($) { $(document).ready(function() { initAddToCart(); //qtyProduct(); }); function convertToSlug(text) { return text.toLowerCase().replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-'); } window.convertToSlug=convertToSlug; function showPopup(selector) { $(selector).addClass('active'); } window.showPopup=showPopup; function hidePopup(selector) { $(selector).removeClass('active'); } window.hidePopup=hidePopup; /* ADD TO CART */ function doAjaxAddToCart(form, title, image) { $.ajax({ type: "post", url: "/cart/add", data: form.serialize(), dataType: 'json', success: function(msg) { //$('.product-popup').removeClass('wishlist-popup').addClass('cart-popup'); //$('.product-popup').find('.product-name').html(title); //$('.product-popup').find('.product-image img').attr('src', image); /*showPopup('.product-popup'); doUpdateMiniCart(msg);*/ console.log('producto agregado...'); window.location.href = '/cart'; }, error: function(xhr, text) { console.log('error al agregar al carrito...'); //hidePopup('.loading'); //$('.error-message').text($.parseJSON(xhr.responseText).description); //showPopup('.error-popup'); } }); }window.doAjaxAddToCart=doAjaxAddToCart; /* --------------------------------------------------------- */ /* Ajax AddtoCart */ /* Product item - Add To Cart */ function initAddToCart() { $('.btn-addToCart').click(function(event) { event.preventDefault(); var productItem = $(this).parents('.col-md-3'); var form = $(this).parents('form'); var title = $(productItem).find('.name').html(); var image = $(productItem).find('.img-responsive').attr('src'); var idpro = $(productItem).find('.name').attr('attr-id'); var color = $(productItem).find('.name').attr('attr-color'); form.append(' '); if(color != '') form.append(''); //console.log(form); doAjaxAddToCart(form, title, image); return false; }); }window.initAddToCart=initAddToCart; /* Do Update Mini Cart */ function doUpdateMiniCart(cart) { if(!cart.cart) { if(cart.total && cart.count) { var ctotal = cart.total; var ccount = cart.count; }else { var ctotal = 0; var ccount = 0; } }else { var ctotal = cart.cart.totalNumber; var ccount = cart.items; } $('.CartCount').text(ccount); $('.cartCount span').text(ccount); $('.mini-cart .summary .price').html(ctotal); $('#total').html(ctotal); $('.mini-cart .mini-products-list').html(''); $('#mini-cart').load('/cart/preview', function() { $('.mini-cart .btn-remove').click(function(event) { event.preventDefault(); var productId = $(this).parents('.item').attr('data-handle'); removeProductFromCart(productId); }); checkItemsInMiniCart(); }); }window.doUpdateMiniCart=doUpdateMiniCart; /* Check Item Mini Cart */ function checkItemsInMiniCart() { if($('.mini-cart .mini-products-list').children().length > 0) { $('.mini-cart').addClass('hasItem'); } else{ $('.mini-cart').removeClass('hasItem'); } }window.checkItemsInMiniCart=checkItemsInMiniCart; function qtyProduct() { $('.qtyplus').click(function(e){ var fieldName = $(this).attr('data-field'); var currentVal = parseInt($('input[name='+fieldName+']').val()); if (!isNaN(currentVal)) { $('input[name='+fieldName+']').val(currentVal + 1); } else { $('input[name='+fieldName+']').val(1); } e.preventDefault(); }); $(".qtyminus").click(function(e) { var fieldName = $(this).attr('data-field'); var currentVal = parseInt($('input[name='+fieldName+']').val()); if (!isNaN(currentVal) && currentVal > 1) { $('input[name='+fieldName+']').val(currentVal - 1); } else { $('input[name='+fieldName+']').val(1); } e.preventDefault(); }); }window.qtyProduct=qtyProduct; function removeProductFromCart(slug) { $('body').css('cursor', 'progress'); $.ajax({ type: 'POST', dataType: 'json', url: '/cart/update', data: {slug:slug, delete:true}, success: function(data){ if(!data.error) { $('body').css('cursor', 'auto'); doUpdateMiniCart(data); } }, error: function(){ $('body').css('cursor', 'auto'); return false; } }) }window.removeProductFromCart=removeProductFromCart; })(jQuery);