$(document).ready(function() { /* HOME E CART SUMARY */ $('.flexy-add-to-cart-custom').on('click', function(){ car = $('.shopping-cart-content'); button = $(this); input = button.parent().find('input[type="number"]') id = button.data('variant-id'); quantity = input.val(); totalPriceWithDiscount = 0; $.ajax({ url: '/cart/add', type: 'post', dataType: 'json', data: { id: id, quantity: quantity, }, success: function (data) { car.text(''); itens = ''; $.each( data.cart.items, function( i, v ) { totalPriceWithDiscount += v.totalPriceWithDiscount itens += modelCart(v); }); /* Buttons */ itens +='


* Valor do frete não incluso. Ele será inserido na finalização.

'; car.append(itens); price = (SHOW_PRODUCT_PRICE) ? totalPriceWithDiscount : 0.00; sumTotal = ' '+numberToReal(price, true)+''; $('span.cart-sumary-total2').text('').append(sumTotal) $('span.cart-sumary-total').text('').append(sumTotal) $('.quantityCart').on('click',function(){ updateCart(this)}); $('.header-mini-cart').addClass('open'); }, error: function (data) { console.log('erro'); } }); }); function modelCart(v) { let c = '
'+ '
'+ '
'+ '
'+ '
'+ '

{VARIANT_NAME}

'+ '
'+ ''+ '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ ''+ '
'+ '
+
'+ '
-
'+ '
'+ '
'+ '
'+ '
'; c +=(SHOW_PRODUCT_PRICE) ? '
{VARIANT_PRICE}
' : ''; c += '
'+ '
'+ '
'+ '
'; c = c.replace("{PRODUCT_IMAGE}", v.image).replace("{VARIANT_NAME}", v.name).replace("{ITEM_ID}", v.id).replace("{VARIANT_QUANTITY_PRESENTATION}",v.quantity).replace("{VARIANT_ID}", v.variant.id).replace("{VARIANT_ID}", v.variant.id).replace("{VARIANT_ID}", v.variant.id).replace("{VARIANT_PRICE}", numberToReal(v.totalPriceWithDiscount,true)); return c; } function numberToReal(price, coin){ var price = price.toFixed(2).split('.'); price[0] = (coin ? 'R$ ' : '') + price[0].split(/(?=(?:...)*$)/).join('.'); return price.join(','); } function updateCart(v) { console.log('update'); var $this = $(v); refereCode = $this.data('item-id'); inputCart = $('#quantityCart-'+refereCode); min = inputCart.attr('min'), max = inputCart.attr('max'); max = (max !=undefined) ? max : 9999; min = (min !=undefined) ? min : 1; // Verifica operação add ou sub var oldValue = parseFloat(inputCart.val()); if($this.hasClass('quantityCartUp')) { var newVal = (oldValue >= max ) ? oldValue : oldValue + 1; } else { var newVal = ( oldValue == min ) ? oldValue : oldValue - 1; } if(oldValue== newVal) {exit;} inputCart.val(newVal); $.ajax({ url: '/cart/add', type: 'post', dataType: 'json', data: { id: refereCode, quantity: inputCart.val(), additionalVariantId: [], distributionCenter: [] }, beforeSend: function () { $this.parent().html('
Adicionando...
'); }, success: function () { location.reload(); }, error: function (data) { $('#adding-to-cart').html(' '+data.responseJSON.error); } }); } $('.quantityCart').on('click',function(){ updateCart(this) }); /* CARRINHO */ $('.quantity-update-cart').on('click', function(){ v = $(this).parent().parent() input = v.find('input.flexy-cart-product-quantity')[0] vl = input.value variantId = input.getAttribute('data-variant-id'); itemId = input.getAttribute('data-cart-item-id'); $.ajax({ url: '/cart/add', type: 'post', dataType: 'json', data: { id: variantId, quantity: vl, }, beforeSend: function () { $("#card-product-total-"+itemId).html('
'); }, success: function () { location.reload(); }, error: function (data) { console.log('Erro') } }); }); });