// Run on initial DOM load document.addEventListener("DOMContentLoaded", function () { utmsHandler(); attachCheckoutListeners(); }); // Quando modificar o carrinho, atualizar os listeners jQuery(document.body).on('updated_cart_totals updated_wc_div wc_fragments_loaded cart_page_refreshed wc_fragments_loaded added_to_cart removed_from_cart', function () { attachCheckoutListeners(); }); function attachCheckoutListeners() { const checkoutButtonQuery = checkoutConfigs.checkoutQuery; isElementLoaded(checkoutButtonQuery).then((selector) => { let checkoutButton = jQuery(checkoutButtonQuery); if (!checkoutButton.length) { checkoutButton = jQuery('a[href*="checkout"]'); } if (checkoutButton.length > 0) { checkoutButton.removeAttr("href"); checkoutButton.off('click.checkout').on('click.checkout', function (e) { e.preventDefault(); submitCheckout(); }); } }); isElementLoaded('form#buy-it-now').then((selector) => { let buyitnowButton = jQuery("form#buy-it-now button[name='buy-it-now']"); if (buyitnowButton.length > 0) { buyitnowButton.removeAttr("href"); buyitnowButton.off('click.buyitnow').on('click.buyitnow', function (e) { e.preventDefault(); submitCheckout("buyitnow"); }); } }); } const isElementLoaded = async item => { while (document.querySelector(item) === null) { await new Promise(resolve => requestAnimationFrame(resolve)) } return document.querySelector(item); }; function submitCheckout($type = "cart") { jQuery(document).ready(function (jQuery) { jQuery.ajax({ url: ajaxurl, method: 'POST', data: { action: 'get_cart_products' }, success: function (response) { if (response.success) { if ($type == "buyitnow") { butitNowForm(response.data); } else { createHiddenForm(response.data); } } else { if ($type == "buyitnow") { butitNowForm(response.data); } else { console.error('Error retrieving cart products:', response.data); } } }, error: function (xhr, status, error) { console.error('Error retrieving cart products:', error); } }); }); } function createHiddenForm(data) { // Create a form element var form = jQuery('
', { action: data.checkout, // Checkout URL method: 'POST' }); // Create a hidden input field to hold the JSON data const jsonDataInput = jQuery('', { type: 'hidden', name: 'cart', // Change to the desired name value: JSON.stringify({products: data.cart}) }); // Append the hidden input field to the form form.append(jsonDataInput); // Append the form to the body and submit it form.appendTo('body').submit(); } function butitNowForm(data) { if (jQuery(".variations").size() > 0) { let productVariations = jQuery(".variations_form").data("product_variations"); let attributeNames = {}; jQuery(".variations_form select").each(function () { attributeNames[jQuery(this).data("attribute_name")] = jQuery(this).val(); }); let emptyAttributes = []; for (let key in attributeNames) { if (!attributeNames[key] || attributeNames[key] === null || attributeNames[key] === "") { emptyAttributes.push(key); } } if (emptyAttributes.length > 0) { alert(`É Preciso selecionar as variantes do produto, campos: ${emptyAttributes.join(",")}`); return; } const selectedVariation = productVariations.find(variation => { return Object.keys(attributeNames).every(key => { return variation.attributes[key] === attributeNames[key]; }); }); const selectedVariantId = selectedVariation.variation_id if (!selectedVariantId) { primaryVariantCode = String(jQuery("input[name='variation_id']").first().val()); } else { primaryVariantCode = selectedVariantId } products = [ { code: String(jQuery("button[name='buy-it-now']").data("id")), primary_variant_code: primaryVariantCode, name: String(jQuery("button[name='buy-it-now']").data("name")), price: jQuery("button[name='buy-it-now']").data("price"), quantity: jQuery("input[name=quantity]").val() > 0 ? jQuery("input[name=quantity]").val() : 1, } ] } else { products = [ { code: String(jQuery("button[name='buy-it-now']").data("id")), name: String(jQuery("button[name='buy-it-now']").data("name")), price: jQuery("button[name='buy-it-now']").data("price"), quantity: jQuery("input[name=quantity]").val() > 0 ? jQuery("input[name=quantity]").val() : 1, } ] } var form = jQuery('', { action: data.checkout, // Checkout URL method: 'POST' }); // Create a hidden input field to hold the JSON data const jsonDataInput = jQuery('', { type: 'hidden', name: 'cart', // Change to the desired name value: JSON.stringify({products}) }); // Append the hidden input field to the form form.append(jsonDataInput); // Append the form to the body and submit it form.appendTo('body').submit(); } function utmsHandler() { var url_string = window.location.href; var url = new URL(url_string); var utmParams = { src: url.searchParams.get('src'), utm_source: url.searchParams.get('utm_source'), utm_medium: url.searchParams.get('utm_medium'), utm_campaign: url.searchParams.get('utm_campaign'), utm_term: url.searchParams.get('utm_term'), utm_content: url.searchParams.get('utm_content') } var cookieValue = ''; Object.keys(utmParams).forEach(function (key) { if (utmParams[key]) { if (cookieValue) { cookieValue += '|'; } cookieValue += key + '=' + utmParams[key]; } }); if (cookieValue) { var cookieName = '_sirius_track'; var myDate = new Date(); myDate.setMonth(myDate.getMonth() + 12); var hostname = window.location.hostname; var parts = hostname.split('.'); if (parts.length >= 3) { var tld = parts.pop(); var domain = parts.pop(); if (tld === 'br') { domain = parts.pop() + '.' + domain; } domain = domain + '.' + tld; } else { // Trata como dominio.tld tld = parts.pop(); domain = parts.pop(); domain = domain + '.' + tld; } document.cookie = cookieName + '=' + cookieValue + ';domain=.' + domain + ';path=/;expires=' + myDate.toUTCString(); } }