Created
December 6, 2023 02:07
-
-
Save dantetesta/8dbc50882509675f2faad7d842e1e6a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="counter"></div> | |
<script> | |
jQuery(document).ready(function() { | |
function calcularSubtotalDoItem(itemElemento) { | |
var valorTextoElemento = jQuery(itemElemento).find('.sn_valor2 .jet-listing-dynamic-field__content').text().trim(); | |
var valorNumerico = parseFloat(valorTextoElemento); | |
var quantidadeInput = jQuery(itemElemento).find('.qtd-input.sn_qtd'); | |
var quantidadeNumerica = parseFloat(quantidadeInput.val()); | |
if (!isNaN(valorNumerico) && !isNaN(quantidadeNumerica)) { | |
var subtotalCalculado = valorNumerico * quantidadeNumerica; | |
subtotalCalculado = subtotalCalculado.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }); | |
} else { | |
subtotalCalculado = 'R$ 0,00'; | |
} | |
jQuery(itemElemento).find('.subtotal span').text(subtotalCalculado); | |
calcularTotal(); | |
} | |
function calcularTotal() { | |
var total = 0; | |
jQuery('.jet-listing-grid__item .subtotal span').each(function() { | |
var valorSubtotal = jQuery(this).text().trim(); | |
valorSubtotal = parseFloat(valorSubtotal.replace('R$', '').replace(/\./g, '').replace(',', '.')); | |
if (!isNaN(valorSubtotal)) { | |
total += valorSubtotal; | |
} | |
}); | |
total = total.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }); | |
jQuery('.total span').text(total); | |
} | |
jQuery('.jet-listing-grid__item').each(function() { | |
calcularSubtotalDoItem(this); | |
}); | |
jQuery('.jet-listing-grid__item').on('change', '.qtd-input.sn_qtd', function() { | |
var itemGrid = jQuery(this).closest('.jet-listing-grid__item'); | |
calcularSubtotalDoItem(itemGrid); | |
}); | |
jQuery('.jet-listing-grid__item').on('click', '.increment, .decrement', function() { | |
var itemElemento = jQuery(this).closest('.jet-listing-grid__item'); | |
var qtdInputElemento = jQuery(itemElemento).find('.qtd-input.sn_qtd'); | |
var passo = parseInt(qtdInputElemento.attr('data-step')); | |
var valorAtual = parseInt(qtdInputElemento.val()); | |
var novoValor; | |
if (jQuery(this).hasClass('increment')) { | |
novoValor = valorAtual + passo; | |
} else { | |
novoValor = Math.max(valorAtual - passo, parseInt(qtdInputElemento.attr('min'))); | |
} | |
qtdInputElemento.val(novoValor).trigger('change'); | |
}); | |
}); | |
</script> | |
<script> | |
//aqui escondemos o input carrinho | |
jQuery('#sn_carrinho').hide(); | |
//aqui cria o evento de click no submit | |
jQuery('.jet-form-builder__submit' ).click(function() { | |
//variáveis dos campos do form | |
var produtos = new Array(1); | |
var produtos_zap = new Array(1); | |
var sku_class = '.sn_sku .jet-listing-dynamic-field__content'; | |
var titulo_class = '.sn_titulo .jet-listing-dynamic-field__content'; | |
var quantidade_class = '.sn_qtd'; | |
var imagem_class = '.sn_imagem'; | |
var valor_class = '.sn_valor .jet-listing-dynamic-field__content'; | |
var carrinho_id = '#sn_carrinho'; | |
var carrinho_zap = '#sn_carrinho_zap'; | |
var observacao = '#sn_obs'; | |
var cliente_nome = "#sn_nome"; | |
var cliente_email = "#sn_email"; | |
var zap_destino = "input[name='url_zap']"; | |
var send_to_zap = "input[name='send_to_zap']"; | |
var endereco = jQuery("#sn_endereco").val(); | |
var nome_cliente = jQuery(cliente_nome).val(); | |
var email_cliente = jQuery(cliente_email).val(); | |
var zap = jQuery(zap_destino).val(); | |
var obs = jQuery(observacao).val(); | |
var zapsend = jQuery(send_to_zap).val(); | |
var subtotal_class = ".subtotal span"; | |
var total = jQuery(".total span").text(); | |
var intro_msg = ` | |
Olá meu nome é ${nome_cliente}, gostaria de solicitar o pedido dos itens abaixo: | |
<br> | |
<h5>Valor Total do Pedido</h5> | |
${total} | |
`; | |
var end_msg = ` | |
fico no aguardo do orçamento, segue abaixo o meu e-mail caso precise | |
${email_cliente} | |
Obrigado! | |
`; | |
//realizamos a limpeza do carrinho para ir zerado | |
jQuery(carrinho_id).html(''); | |
//usamos o each para trazer todos os dados | |
var listing = jQuery('.jet-listing-grid__items .jet-listing-grid__item'); | |
listing.each(function(i) { | |
//listamos dados do listing para montar a msg | |
var id_post = jQuery(this).data('post-id'); | |
var sku = jQuery('.jet-listing-dynamic-post-'+id_post + ' ' + sku_class).text(); | |
var titulo = jQuery('.jet-listing-dynamic-post-'+id_post + ' ' + titulo_class).text(); | |
var valor = jQuery('.jet-listing-dynamic-post-'+id_post + ' ' + valor_class).text(); | |
var qtd = jQuery('.jet-listing-dynamic-post-'+ id_post + ' ' + quantidade_class).val(); | |
var imagem = jQuery('.jet-listing-dynamic-post-'+ id_post + ' ' + imagem_class +' img').prop('src'); | |
var permalink = jQuery('.jet-listing-dynamic-post-'+ id_post + ' ' + imagem_class +' a').prop('href'); | |
var subvalor = jQuery('.jet-listing-dynamic-post-'+id_post + ' ' + subtotal_class).text(); | |
//aqui criamos o template a ser enviado por email | |
produtos[i] = `<div style="width:300px;"> | |
<div style="margin:10px;border-radius:30px;text-align:center;padding:20px 0 0 0;border: 3px solid #F51344;"> | |
<div> | |
<img src=${imagem} style="width:150px;"/> | |
</div> | |
<div> | |
<a href=${permalink} style="text-decoration:none;color:#fff;background-color:#F51344;padding:10px 30px;border-radius:10px;display:inline-block;margin:10px 0;">Link do Produto</a> | |
</div> | |
<div> | |
<p>${sku}</p> | |
</div> | |
<div> | |
<p><strong>Produto:</strong>${titulo}</p> | |
</div> | |
<div> | |
<p><strong>Quantidade:</strong>${qtd}</p> | |
</div> | |
<div> | |
<p> | |
<b>Valor:</b>${valor}<br> | |
<b>Subtotal:</b> ${subvalor} | |
</p> | |
</div> | |
</div></div>`; | |
//aqui criamos o template a ser enviado por whatsapp | |
produtos_zap[i] = `${sku} | |
*${titulo}* | |
*Qtd:* ${qtd} | |
*Valor*: ${valor} | |
*Subtotal*: ${subvalor} | |
*Link:* ${permalink} | |
-------------- | |
`; | |
//aqui validamos a quantidade que não pode ser vazia ou valor 0 | |
switch (qtd){ | |
case '': | |
alert('O campo de quantidade está vazio.'); | |
break; | |
case 0: | |
alert('Não é aceito o valor 0.'); | |
break; | |
case 'undefined': | |
alert('Ocorreu um erro!'); | |
break; | |
default: | |
jQuery(carrinho_id).html(produtos.join('').replace(/(\r\n|\n|\r)/gm," ")); | |
valor = valor +' cada'; | |
} | |
}); | |
if(zapsend=='true'){ | |
var checkSuccessInterval = setInterval(function() { | |
// Verifica se a mensagem de sucesso está visível | |
if (jQuery('.jet-form-builder-message--success').is(':visible')) { | |
// Limpa o intervalo para parar de verificar | |
clearInterval(checkSuccessInterval); | |
var message = `*PEDIDO DE ORÇAMENTO* | |
Olá, meu nome é ${nome_cliente} - ${email_cliente} | |
*Mensagem* | |
${obs} | |
*Endereço* | |
${endereco} | |
*Valor Total do Pedido* | |
${total} | |
\n\n${produtos_zap.join('\n')} | |
\nObrigado!`; | |
// Codifica a mensagem para URL | |
var encoded_message = encodeURIComponent(message); | |
// Verifica se é um dispositivo móvel | |
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) { | |
// Se for móvel, redireciona na mesma página | |
window.location.href = zap + "?text=" + encoded_message; | |
} else { | |
// Se for desktop, abre em uma nova aba | |
window.open(zap + "?text=" + encoded_message, '_blank'); | |
} | |
} | |
}, 500); // verifica a cada segundo | |
} | |
}); | |
function handleIncrementDecrement() { | |
var observer = new MutationObserver(function(mutations) { | |
if (jQuery('.jet-listing-grid__items').length) { | |
jQuery(".decrement").off().on("click", function() { | |
var input = jQuery(this).siblings('.qtd-input'); | |
var step = parseInt(input.data('step')); | |
var val = parseInt(input.val()); | |
var min = parseInt(input.attr('min')); | |
// Se o valor não for um número, definir como 0 | |
if(isNaN(val)) { | |
val = 0; | |
} | |
// Se min não for um número, definir como 0 | |
if(isNaN(min)) { | |
min = 0; | |
} | |
if(val > min) { | |
input.val(val - step); | |
} | |
}); | |
jQuery(".increment").off().on("click", function() { | |
var input = jQuery(this).siblings('.qtd-input'); | |
var step = parseInt(input.data('step')); | |
var val = parseInt(input.val()); | |
// Se o valor não for um número, definir como 0 | |
if(isNaN(val)) { | |
val = 0; | |
} | |
input.val(val + step); | |
}); | |
observer.disconnect(); | |
} | |
}); | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true | |
}); | |
} | |
jQuery(document).ready(function() { | |
handleIncrementDecrement(); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment