// VOLTAMOS PARA O 'ready'. É mais rápido para os cliques. jQuery(document).ready(function($) { // --- Nome do Cookie --- const COOKIE_NAME = 'cep_usuario_trator'; // --- Helpers para selecionar TODAS as ocorrências de ids duplicados --- const $all = (id) => $('[id="' + id + '"]'); // --- FUNÇÕES DE COOKIE (JavaScript Puro) --- function setCookie(name, value, days) { let expires = ""; if (days) { const date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/"; } function getCookie(name) { const nameEQ = name + "="; const ca = document.cookie.split(';'); for (let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; } // --- MÁSCARA DE CEP --- function aplicarMascaraCEP(event) { let valor = event.target.value || ''; valor = valor.replace(/\D/g, ''); valor = valor.replace(/^(\d{5})(\d)/, '$1-$2'); event.target.value = valor; } // Liga a máscara nos DOIS inputs (usar 'input' cobre teclados virtuais) $(document).on('input', '[id="cep-input-desktop"], [id="cep-input-mobile"]', aplicarMascaraCEP); // --- Atualização de UI (todas as cópias) --- function applyCepToUI(cep) { if (!cep) return; $all('cep-display-text').text(cep); $all('cep-input-desktop').val(cep); $all('cep-input-mobile').val(cep); } // --- Mostrar CEP salvo (vigia paciente) --- const cepSalvo = getCookie(COOKIE_NAME); if (cepSalvo) { let tentativas = 0; const maxTentativas = 50; // ~10s const vigia = setInterval(function() { const $displays = $all('cep-display-text'); if ($displays.length > 0) { applyCepToUI(cepSalvo); clearInterval(vigia); } else { tentativas++; } if (tentativas >= maxTentativas) { clearInterval(vigia); } }, 200); } // --- Abrir popups (smart) --- $(document).on('click touchstart', function(event) { const $target = $(event.target); const triggerHit = $target.is('[id="cep-trigger-link"]') || $target.closest('[id="cep-trigger-link"]').length; if (!triggerHit) return; event.preventDefault(); event.stopPropagation(); if (window.innerWidth <= 767) { $all('cep-modal-mobile').toggleClass('active'); $all('cep-popover-desktop').removeClass('active'); } else { $all('cep-popover-desktop').toggleClass('active'); $all('cep-modal-mobile').removeClass('active'); } return false; }); // --- Fechar popups --- function fecharPopups() { $all('cep-popover-desktop').removeClass('active'); $all('cep-modal-mobile').removeClass('active'); } $(document).on('click touchstart', '[id="cep-close-btn-mobile"], .cep-modal-overlay', fecharPopups); // Fecha popover desktop ao clicar fora $(document).on('click', function(event) { const $target = $(event.target); const $popovers = $all('cep-popover-desktop'); const algumAtivo = $popovers.filter('.active').length > 0; if (algumAtivo && !$target.closest('[id="cep-popover-desktop"]').length && !$target.closest('[id="cep-trigger-link"]').length) { fecharPopups(); } }); // --- Salvar CEP (unificada) --- function salvarCEP(cepValor) { if (cepValor && cepValor.length === 9) { setCookie(COOKIE_NAME, cepValor, 30); applyCepToUI(cepValor); fecharPopups(); // Tenta calcular no produto imediatamente (sem depender só do reload) const campoAlvo = document.getElementById('inputCep'); if (campoAlvo) { campoAlvo.value = cepValor; campoAlvo.dispatchEvent(new Event('change', { bubbles: true })); const enterEvent = new KeyboardEvent('keyup', { key: 'Enter', keyCode: 13, which: 13, bubbles: true, cancelable: true }); campoAlvo.dispatchEvent(enterEvent); } // Fallback para garantir sincronismo em páginas que precisem recarregar setTimeout(function() { location.reload(); }, 150); } else { alert('Digite um CEP válido (ex: 00000-000).'); } } // Botões OK (desktop e mobile) $(document).on('click touchstart', '[id="btn-salvar-cep-desktop"]', function() { salvarCEP($all('cep-input-desktop').first().val()); }); $(document).on('click touchstart', '[id="btn-salvar-cep-mobile"]', function() { salvarCEP($all('cep-input-mobile').first().val()); }); // --- SNIPER (auto-calcular no produto após carregar) --- if (cepSalvo) { setTimeout(function() { const campoAlvo = document.getElementById('inputCep'); if (campoAlvo) { campoAlvo.value = cepSalvo; campoAlvo.dispatchEvent(new Event('change', { bubbles: true })); const enterEvent = new KeyboardEvent('keyup', { key: 'Enter', keyCode: 13, which: 13, bubbles: true, cancelable: true }); campoAlvo.dispatchEvent(enterEvent); } // Preenche calculadoras de frete do WooCommerce (exceto o #inputCep principal) jQuery('#calc_shipping_postcode, .shipping-calculator-form .input-text[name="calc_shipping_postcode"]') .not('#inputCep') .val(cepSalvo); }, 2500); } });

TODO O SITE EM ATÉ 5X S/ JUROS – ENVIAMOS PARA TODO O BRASIL

Onde você quer receber?

Informe seu CEP para ver os prazos de entrega

Calcule o frete

Informe seu CEP para ver os prazos

Calcule o frete

Informe seu CEP para ver os prazos

ROLAMENTO MASSEY FERGUSON

Carrinho de compras
plugins premium WordPress
Rolar para cima
Onde você quer receber?

Informe seu CEP para ver os prazos de entrega