').html(html);
var prods = [], vistos = {};
doc.find('.showcase.list-slide .product').each(function() {
var id = jQuery(this).find('form.list-variants').data('id');
if (!id || vistos[id]) return;
vistos[id] = true;
var dado = self._extrairDadosProduto(jQuery(this), ids);
if (dado) prods.push(dado);
});
return prods;
},
_renderCJ: function(prods) {
if (!prods.length) {
var cj = document.getElementById('hivahCompreJunto');
cj.style.display = 'none';
cj.classList.remove('visivel');
return;
}
var html = '';
prods.slice(0, 4).forEach(function(pr) {
html += '
' +
'
' + (pr.img ? '

' : '') + '
' +
'
' +
'
' + pr.nome + '
' +
'
' +
(pr.precoOriginal ? '
' + pr.precoOriginal + '
' : '') +
'
' + pr.preco + '
' +
'
' +
'
' +
'
';
});
var cj = document.getElementById('hivahCompreJunto');
document.getElementById('hivahCjScroll').innerHTML = html;
cj.style.display = 'block';
setTimeout(function() { cj.classList.add('visivel'); }, 30);
},
loadCompreJunto: function(ids, urls) {
if (!ids.length) {
var cj = document.getElementById('hivahCompreJunto');
cj.style.display = 'none';
cj.classList.remove('visivel');
return;
}
var self = this;
var listaPagos = (self._lastList || []).filter(function(i) {
return parseFloat(i.Cart.price) > 0;
});
var refItem = listaPagos.length ? listaPagos[listaPagos.length - 1] : self._lastList[self._lastList.length - 1];
var refId = parseInt(refItem.Cart.product_id);
var refUrl = urls[refId];
if (jQuery('html').hasClass('page-product')) {
var domProds = [], vistos = {};
jQuery('.showcase.list-slide .product').each(function() {
var id = jQuery(this).find('form.list-variants').data('id');
if (!id || vistos[id]) return;
vistos[id] = true;
var dado = self._extrairDadosProduto(jQuery(this), ids);
if (dado) domProds.push(dado);
});
if (domProds.length) { self._renderCJ(domProds); return; }
}
if (self._cjCache && (self._cjCache.id === refId || (refUrl && self._cjCache.url === refUrl))) {
var cached = self._cjCache.prods.filter(function(p) {
return ids.indexOf(parseInt(p.id)) === -1;
});
if (cached.length) { self._renderCJ(cached); return; }
}
if (refUrl) {
jQuery.get(refUrl, function(html) {
var prods = self._extrairRelacionados(html, ids);
self._cjCache = { id: refId, url: refUrl, prods: prods };
if (prods.length) { self._renderCJ(prods); return; }
self._fallbackCategoria(ids);
}).fail(function() { self._fallbackCategoria(ids); });
return;
}
self._fallbackCategoria(ids);
},
_fallbackCategoria: function(ids) {
var self = this;
var listaPagos = (self._lastList || []).filter(function(i) {
return parseFloat(i.Cart.price) > 0;
});
var refItem = listaPagos.length ? listaPagos[listaPagos.length - 1] : self._lastList[self._lastList.length - 1];
var refId = parseInt(refItem.Cart.product_id);
jQuery.ajax({
method: 'GET', url: '/web_api/products/' + refId,
success: function(r) {
var prod = r && r.Product ? r.Product : null;
if (!prod) {
var cj = document.getElementById('hivahCompreJunto');
cj.style.display = 'none'; cj.classList.remove('visivel'); return;
}
jQuery.ajax({
method: 'GET', url: '/web_api/products?category_id=' + prod.category_id + '&limit=8&available=1',
success: function(r2) {
var prods = r2 && r2.Products ? r2.Products : [];
var sugs = prods.filter(function(p) { return ids.indexOf(parseInt(p.Product.id)) === -1; }).slice(0, 4);
if (!sugs.length) {
var cj = document.getElementById('hivahCompreJunto');
cj.style.display = 'none'; cj.classList.remove('visivel'); return;
}
var domProds = sugs.map(function(p) {
var pr = p.Product;
var thumbs = pr.ProductImage && pr.ProductImage[0] && pr.ProductImage[0].thumbs ? pr.ProductImage[0].thumbs : null;
var precoPromo = pr.promotional_price && parseFloat(pr.promotional_price) > 0 ? parseFloat(pr.promotional_price) : null;
var precoCheio = parseFloat(pr.price);
return {
id: pr.id,
nome: pr.name,
url: pr.url ? pr.url.https : '',
img: thumbs ? (thumbs['180'] ? thumbs['180'].https : (thumbs['90'] ? thumbs['90'].https : '')) : '',
preco: self.fmt(precoPromo || precoCheio),
precoOriginal: (precoPromo && precoPromo !== precoCheio) ? self.fmt(precoCheio) : null
};
});
self._renderCJ(domProds);
},
error: function() {
var cj = document.getElementById('hivahCompreJunto');
cj.style.display = 'none'; cj.classList.remove('visivel');
}
});
},
error: function() {
var cj = document.getElementById('hivahCompreJunto');
cj.style.display = 'none'; cj.classList.remove('visivel');
}
});
},
addCJ: function(productId, variantId, href, btn) {
btn.textContent = 'Adicionando...';
btn.disabled = true;
cart.addToCart(productId, 1, variantId, href, 1);
var self = this;
setTimeout(function() { self.reload(); }, 800);
},
changeQty: function(idx, delta, productId, variantId, addInfo, boughtTogetherId) {
var self = this;
var qtdEl = document.querySelectorAll('.hivah-qty-num')[idx];
if (!qtdEl) { self.reload(); return; }
var novaQtd = (parseInt(qtdEl.textContent) || 1) + delta;
if (novaQtd <= 0) {
self.removeItem(productId, variantId, addInfo, boughtTogetherId); // <-- passa boughtTogetherId
return;
}
var variant = parseInt(variantId) === 0 ? '' : '/' + variantId;
var addText = addInfo ? '/?additional_information=' + addInfo.replace(/br>/g, 'br/>') : '';
jQuery.ajax({
method: 'DELETE', url: '/web_api/carts/' + cart.session() + '/' + productId + variant + addText,
success: function() {
cart.loadCustomerId();
jQuery.ajax({
method: 'POST', url: '/web_api/cart/', dataType: 'json',
data: { Cart: { session_id: cart.session(), product_id: productId, variant_id: variantId || 0, quantity: novaQtd }},
success: function() { self.reload(); }, error: function() { self.reload(); }
});
},
error: function() { self.reload(); }
});
},
removeItem: function(productId, variantId, addInfo, boughtTogetherId) {
var self = this;
var vid = parseInt(variantId) === 0 ? 0 : variantId;
var addText = addInfo ? 'additional_information=' + addInfo.replace(/br>/g, 'br/>') : '';
var btid = boughtTogetherId && parseInt(boughtTogetherId) !== 0;
var url;
if (btid) {
url = '/web_api/carts/' + cart.session() + '/' + productId + '/' + (vid || 0) + '/' + boughtTogetherId;
if (addText) url += '?' + addText;
} else {
url = '/web_api/carts/' + cart.session() + '/' + productId;
if (vid) url += '/' + vid;
if (addText) url += '/?' + addText;
}
jQuery.ajax({
method: 'DELETE', url: url,
success: function() { self.reload(); }, error: function() { self.reload(); }
});
}
};
function hivahWppCarrinho() {
var session = jQuery('html').attr('data-session');
var storeId = jQuery('html').attr('data-store');
var itens = '', total = 0;
document.querySelectorAll('.hivah-cart-item').forEach(function(el) {
var nome = el.querySelector('.hivah-cart-item-name');
var preco = el.querySelector('.hivah-cart-item-price');
var qty = el.querySelector('.hivah-qty-num');
if (!nome || !preco || !qty) return;
var qtyNum = parseInt(qty.textContent) || 1;
var precoUnit = preco.textContent.replace('R$ ', '').replace(',', '.');
itens += '* ' + nome.textContent + ' (x' + qtyNum + ') - R$ ' + (parseFloat(precoUnit) * qtyNum).toFixed(2).replace('.', ',') + '\n';
total += parseFloat(precoUnit) * qtyNum;
});
if (!itens) return;
var checkoutUrl = 'https://www.hivahoficial.com.br/checkout?session_id=' + session + '&store_id=' + storeId;
var msg = 'Ola! Gostaria de finalizar minha compra pelo WhatsApp\n\n' + itens + '\nTotal: R$ ' + total.toFixed(2).replace('.', ',') + '\n\nLink para finalizar: ' + checkoutUrl;
location.href = 'https://wa.me/5521981978217?text=' + encodeURIComponent(msg);
}
function hivahInitListeners() {
if (typeof jQuery === 'undefined') { setTimeout(hivahInitListeners, 50); return; }
jQuery('.cart-header').off('click').on('click', function(e) { e.preventDefault(); e.stopPropagation(); hivahDrawer.open(); });
jQuery('.shadow-cart').off('click').on('click', function(e) { e.stopPropagation(); hivahDrawer.close(); });
jQuery('#hivahOverlay').off('click').on('click', function(e) { e.stopPropagation(); hivahDrawer.close(); });
}
hivahInitListeners();
Conheça a nossa História 