var mag = []; var mag_cod_iso = []; var currency = ''; const range_increment = 0.05; const max_qta_ordered = 10.00; const bypassMaxCheck = false; let timerInfoAggiornate; const timerValueInfoAggiornate = 900; const min_qta_prod = 0.05; let delayTimerGestioneIncrementiDecrementi = null; let timeoutRemoveProduct; function changeDestination(){ $('.js-destinations-products').on('change', function() { toogleWaitingOn(0.2); var quantity = []; var id_address = ''; id_address = $(this).val(); var product_id = ''; product_id = $(this).data('product-id'); var warehouse_id = ''; warehouse_id = $(this).data('warehouse-id'); var warehouse_qta = ''; warehouse_qta = $(this).data('warehouse-qta'); var id_cart_row = ''; id_cart_row = $(this).data('id-cart-row'); var json = {}; json = { "warehouse_id": warehouse_id, "quantity": warehouse_qta, "company_address_id": id_address } quantity.push(json); updateAddressProduct(product_id, quantity, id_cart_row); }); } function prepareQuantity(sweetAlertMessage = false, timerUpdate = true, product_id, nota = '', el, add_product = 0, id_cart_row = '', isoCodOnlyOneMagToUpdate = ''){ var quantity = []; var json = {}; if(isoCodOnlyOneMagToUpdate == '') { mag.forEach(function (single_mag) { let id_rows_cart = getIdRowsCart(el, product_id, single_mag.iso.toLowerCase(), id_cart_row); json = { "warehouse_id": single_mag.id, "quantity": parseFloat($(id_rows_cart).val()).toFixed(2), "company_address_id": -1 } if ($('.js-add-cart-note').length > 0 && nota != null && nota != ' ' && nota != '' && nota != undefined) { Object.assign(json, { "note": nota }); } quantity.push(json); }); } else if(isoCodOnlyOneMagToUpdate != '') { let dest_prod = $("#dest-prod-"+product_id+"-mag-"+isoCodOnlyOneMagToUpdate+"-"+id_cart_row).val(); let company_address_id = ""; if(typeof dest_prod != "undefined" && dest_prod != null && dest_prod.trim() != ""){ company_address_id = dest_prod; } else { company_address_id = -1; } let magToUpdate = (Array.isArray(mag) ? mag : Array.from(mag)).find(o => String(o?.iso?? "").trim().toUpperCase() === isoCodOnlyOneMagToUpdate.toUpperCase()) || null; let id_rows_cart = getIdRowsCart(el, product_id, isoCodOnlyOneMagToUpdate, id_cart_row); json = { "warehouse_id": magToUpdate.id, "quantity": parseFloat($(id_rows_cart).val()).toFixed(2), "company_address_id": company_address_id } if ($('.js-add-cart-note').length > 0 && nota != null && nota != ' ' && nota != '' && nota != undefined) { Object.assign(json, { "note": nota }); } quantity.push(json); } updateCart(product_id, quantity, '', '', timerUpdate, sweetAlertMessage, add_product, el, id_cart_row); } function gestione_incrementi_decrementi(el, sweetAlertMessage = false, timerUpdate = true, product_id, class_context = '', id_cart_rows = '', isoCodMagToUpdate = 'it'){ let float_value_is_ok = false; if(el != null && el != '' && el != ' ' & el != undefined) { /* mag_cod_iso.forEach(function (mag) { */ if($(el).hasClass(isoCodMagToUpdate)) { let current_value_qta = 0.00; if(class_context != ''){ if(id_cart_rows != '') { current_value_qta = $(class_context + ' #js-input-text-prod-' + product_id + '-mag-' + isoCodMagToUpdate + '-' + id_cart_rows).val(); } else { current_value_qta = $(class_context + ' #js-input-text-prod-' + product_id + '-mag-' + isoCodMagToUpdate).val(); } } else { if(id_cart_rows != '') { current_value_qta = $('#js-input-text-prod-' + product_id + '-mag-' + isoCodMagToUpdate + '-' + id_cart_rows).val(); } else { current_value_qta = $('#js-input-text-prod-' + product_id + '-mag-' + isoCodMagToUpdate).val(); } } if($(el).hasClass(isoCodMagToUpdate)) { newValue = 0.00; if($(el).find('i').hasClass('fa-plus') || $(el).find('i').hasClass('fa-angle-up')){ var newValue = (parseFloat(current_value_qta) + parseFloat(range_increment)).toFixed(2); } else if ($(el).find('i').hasClass('fa-minus') || $(el).find('i').hasClass('fa-angle-down')){ var newValue = (parseFloat(current_value_qta) - parseFloat(range_increment)).toFixed(2); } var newAllowedValue = adjustQuantity(newValue, max_qta_ordered, range_increment); if(class_context != ''){ if(id_cart_rows != '') { $(class_context + ' #js-input-text-prod-' + product_id + '-mag-' + isoCodMagToUpdate + '-' + id_cart_rows).val(newAllowedValue); } else { $(class_context + ' #js-input-text-prod-' + product_id + '-mag-' + isoCodMagToUpdate).val(newAllowedValue); } } else { if(id_cart_rows != '') { $('#js-input-text-prod-' + product_id + '-mag-' + isoCodMagToUpdate + '-' + id_cart_rows).val(newAllowedValue); } else { $('#js-input-text-prod-' + product_id + '-mag-' + isoCodMagToUpdate).val(newAllowedValue); } } if(newAllowedValue != undefined && newAllowedValue != '' && newAllowedValue != ' ' && newAllowedValue != null && newAllowedValue > 0.00 && newValue > 0.00){ float_value_is_ok = true; } } } /*});*/ if(!$(el).hasClass('js-not-update-qta') && float_value_is_ok){ if (delayTimerGestioneIncrementiDecrementi !== null) { clearTimeout(delayTimerGestioneIncrementiDecrementi); } delayTimerGestioneIncrementiDecrementi = setTimeout(() => { prepareQuantity(sweetAlertMessage, timerUpdate, product_id, '', el, 0, id_cart_rows, el.classList[1]); delayTimerGestioneIncrementiDecrementi = null; }, 500); } } } function get_value_add_product_variable(el){ let add_product = 0; if(el && typeof el !== "undefined" && el != ''){ add_product = el.data('add-product'); } return add_product; } function componiAggiuntaProdotto(fromMag, quantity, el, product_id, fromMagId){ var json = {}; let id_rows_cart = getIdRowsCart(el, product_id, fromMag); json = { "warehouse_id": fromMagId, "quantity": parseFloat($(id_rows_cart).val()).toFixed(2), "company_address_id": -1 } if ($('.js-add-cart-note').length > 0 && nota != null && nota != ' ' && nota != '' && nota != undefined) { Object.assign(json, { "note": nota }); } quantity.push(json); return quantity; } function aggiungi_prodotto(el = '', fromMag = '', fromMagId){ let add_product = get_value_add_product_variable(el); var quantity = []; let product_id; product_id = el.closest('.js-add-cart-container-btn').find('.js-product-id').val(); if( fromMag != null && typeof fromMag != "undefined" && fromMag != ''){ quantity = componiAggiuntaProdotto(fromMag, quantity, el, product_id, fromMagId); } else { mag.forEach(function (single_mag){ quantity = componiAggiuntaProdotto(single_mag.iso.toLowerCase(), quantity, el, product_id, fromMagId); }); } if(quantity.length == 1 && quantity[0]['quantity'] == 0){ swal({ text: "Select a quantity", type: "error", showConfirmButton: true }); } else { updateCart(product_id, quantity, '', '', false, true, add_product); } } /** * CHANGE QTA INPUT TEXT */ function changeQtaInputText() { if ($('.js-add-cart-container').length > 0) { /* Element */ if ($('.js-add-cart-element').length > 0) { $('.js-add-cart-container .js-change-input-number.js-add-cart-element').on('change', function () { var inputQta = $(this); if (inputQta.closest('.single-product').length === 0) { var quantity = []; var json = {}; json = { "warehouse_id": inputQta.data('warehouse-id'), "quantity": arrotondamentoValore( adjustQuantity(parseFloat($(this).val()), max_qta_ordered, range_increment) ), "company_address_id": -1 } if ($('.js-add-cart-note-btn').length > 0) { json.note = $('.js-add-cart-container .js-add-cart-note-btn').val(); } quantity.push(json); var product_id = $(this).data('product-id'); updateCart(product_id, quantity, '', '', true, false, 0, $(this)); } }); } } } /** * CHANGE QTA INPUT TEXT */ /** * CONTROLLO MULTIPLI DI 0.05 NEI CAMPI DI INPUT AL CAMBIAMENTO DEL VALORE * @type {NodeListOf} */ function arrotondamentoValore(valoreInput){ valoreInput = parseFloat(valoreInput.replace(",", ".")); if (!isNaN(valoreInput)) { if (valoreInput % range_increment !== 0) { let valoreCorretto = arrotondaAlMultiplo(valoreInput); $(this).val(valoreCorretto.toFixed(2)); return valoreCorretto.toFixed(2); } else { $(this).val(valoreInput.toFixed(2)); return valoreInput.toFixed(2); } } } function arrotondaAlMultiplo(valore) { return Math.ceil(valore / range_increment) * range_increment; } function checkValueQtaIncrementRange() { $('.js-add-cart-element').on('change', function () { let valoreInput = $(this).val(); arrotondamentoValore(valoreInput); }); } /** * CONTROLLO MULTIPLI DI 0.05 NEI CAMPI DI INPUT AL CAMBIAMENTO DEL VALORE * @type {NodeListOf} */ /** * ################################################################################################################################### */ function checkNumericType(el){ if( el && !isNaN(el) ) { return el; } else { swal({ text: 'The entered value is not valid.
Please enter a numeric value.', type: "error", showCancelButton: false, showConfirmButton: false, timer: 5000 }); return min_qta_prod; } } /** * ################################################################################################################################### */ function adjustQuantity(qta, max_qta, range_increment = 0.05) { if (qta == null || qta == undefined || qta == '' || qta == ' ' || qta < min_qta_prod) { qta = min_qta_prod; } else { if (parseFloat(qta) == -0.05) { qta = min_qta_prod; } qta = parseFloat(qta); max_qta = parseFloat(max_qta); if (qta == 0 || qta == 0.00 || qta == "0" || qta == "0.00") { qta = min_qta_prod; } else if (qta < range_increment) { qta = range_increment; } if (!bypassMaxCheck && parseFloat(qta.toFixed(2)) > parseFloat(max_qta.toFixed(2))) { qta = max_qta; } } return checkNumericType(qta.toFixed(2)); } /** * ################################################################################################################################### */ /** * COUNT ITEMS ON CART */ function aggiornaCountElementi(){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/get-cart.php", type: "POST", success: function (data) { if (data.status == "success") { $('.js-count-items-cart').html(data.count_elementi); } else if (data.status == "error"){ if (data.reload != ''){ setTimeout(function (){ location.reload() }, 1000); } } }, }); } /** * COUNT ITEMS ON CART */ /** * ################################################################################################################################### */ function formatPrice(num) { if (typeof num !== 'number') { return '0.00'; } return num.toFixed(2); } /** * AGGIORNA I PREZZI POST CHIAMATA AJAX DI AGGIORNAMENTO CARRELLO */ function aggiornaPrezziAvideo(data, product_id, id_cart_row){ if($('.js-totale-parziale').length > 0) { if(data.partial_price_prod_mag != undefined && data.partial_price_prod_mag != null && data.partial_price_prod_mag != '' && data.partial_price_prod_mag != '' && id_cart_row in data.partial_price_prod_mag) { $('#father_container_list_product_' + product_id + '.js-cart-container-id-cart-row-'+ id_cart_row +' .js-totale-parziale').html(currency+' '+ formatPrice(data.partial_price_prod_mag[id_cart_row])); $('#father_container_li_cart_' + product_id + '.js-cart-container-id-cart-row-'+ id_cart_row +' .js-totale-parziale').html(currency+' '+ formatPrice(data.partial_price_prod_mag[id_cart_row])); $('#father_container_li_cart_' + product_id + '.js-cart-container-id-cart-row-'+ id_cart_row +' .js-totale-parziale-cart').html('('+currency+' '+ formatPrice(data.partial_price_prod_mag[id_cart_row]) + '/m.)'); } if(data.qta_complessiva != undefined && data.qta_complessiva != null && data.qta_complessiva != '' && data.qta_complessiva != '' && product_id in data.qta_complessiva) { $('#js-qta-complessiva-prod-' + product_id + '-' + id_cart_row).html(data.qta_complessiva[id_cart_row] + ' m.'); } } if($('.js-total-price').length > 0){ $('.js-total-price').html(currency+' '+formatPrice(data.total_price)); } if($('.js-input-text-prod').length > 0){ if(data.n_prod_mag_it) { if (product_id in data.n_prod_mag_it) { $('#js-input-text-prod-' + product_id + '-mag-it-'+id_cart_row).val( adjustQuantity(data.n_prod_mag_it[id_cart_row], max_qta_ordered, range_increment) ); } } if(data.n_prod_mag_hk) { if (product_id in data.n_prod_mag_hk) { $('#js-input-text-prod-' + product_id + '-mag-hk-'+id_cart_row).val( adjustQuantity(data.n_prod_mag_hk[id_cart_row], max_qta_ordered, range_increment) ); } } } } /** * AGGIORNA I PREZZI POST CHIAMATA AJAX DI AGGIORNAMENTO CARRELLO */ /** * ################################################################################################################################### */ /** * AGGIORNA LE QTA POST CHIAMATA AJAX DI AGGIORNAMENTO CARRELLO */ /* function aggiornaQtaAvideo(data, product_id){ if($('.single-product .js-add-cart-element').length > 0){ if ( (data.qta_x_mag != undefined && data.qta_x_mag != null && data.qta_x_mag != '' && data.qta_x_mag != ' ') || (data.qta_x_mag != undefined && data.qta_x_mag[product_id] && data.qta_x_mag[product_id]['it'] !== undefined && data.qta_x_mag[product_id]['it'] != '' && data.qta_x_mag[product_id]['it'] != ' ' && data.qta_x_mag[product_id]['it'] != null) ){ if(typeof data.qta_x_mag == 'object' && product_id in data.qta_x_mag && data.qta_x_mag[product_id]['it'] !== undefined && data.qta_x_mag[product_id]['it'] != '' && data.qta_x_mag[product_id]['it'] != ' ' && data.qta_x_mag[product_id]['it'] != null){ $('.single-product .js-add-cart-container-it .js-add-cart-element').val(data.qta_x_mag[product_id]['it'].toFixed(2)); } else { $('.single-product .js-add-cart-container-it .js-add-cart-element').val(min_qta_prod.toFixed(2)); } } else { $('.single-product .js-add-cart-container-it .js-add-cart-element').val(min_qta_prod.toFixed(2)) } if( (data.qta_x_mag != undefined && data.qta_x_mag != null && data.qta_x_mag != '' && data.qta_x_mag != ' ') || (data.qta_x_mag != undefined && data.qta_x_mag[product_id] && data.qta_x_mag[product_id]['hk'] !== undefined && data.qta_x_mag[product_id]['hk'] != '' && data.qta_x_mag[product_id]['hk'] != ' ' && data.qta_x_mag[product_id]['hk'] != null) ){ if(typeof data.qta_x_mag == 'object' && product_id in data.qta_x_mag && data.qta_x_mag[product_id]['hk'] !== undefined && data.qta_x_mag[product_id]['hk'] != '' && data.qta_x_mag[product_id]['hk'] != ' ' && data.qta_x_mag[product_id]['hk'] != null){ $('.single-product .js-add-cart-container-hk .js-add-cart-element').val(data.qta_x_mag[product_id]['hk'].toFixed(2)); } else { $('.single-product .js-add-cart-container-hk .js-add-cart-element').val(min_qta_prod.toFixed(2)); } } else { $('.single-product .js-add-cart-container-hk .js-add-cart-element').val(min_qta_prod.toFixed(2)); } } }*/ /** * AGGIORNA LE QTA POST CHIAMATA AJAX DI AGGIORNAMENTO CARRELLO */ /** * ################################################################################################################################### */ function deleteRowCartId(el, product_id, container, row){ let trToDeleted = $(el).closest(container+' '+row); if(trToDeleted && trToDeleted != '' && typeof trToDeleted !== "undefined" && trToDeleted.length > 0){ trToDeleted.remove(); } } function ifZeroRowsRedirectHomepage(el = ''){ if( $(el).length == 0 ){ window.location.href = " https://staging-fabrics.piacenza1733.com/ "; } } function rimuoviProdottiAvideo(data, product_id, cart_rows_ids_deleted){ /** * Remove ( and if products == 0, redirect ) product in page "destinazione merce" */ if( $('.js-dest-cart #father_container_list_product_'+product_id+' #js-input-text-prod-'+product_id+'-mag-it-'+cart_rows_ids_deleted).length > 0 ){ deleteRowCartId('.js-dest-cart #father_container_list_product_'+product_id+' #js-input-text-prod-'+product_id+'-mag-it-'+cart_rows_ids_deleted, product_id, '.js-dest-cart', '#father_container_list_product_'+product_id); ifZeroRowsRedirectHomepage('.js-dest-cart .father_container'); } /** * Remove ( and if products == 0, redirect ) product in page "destinazione merce" */ /** * Remove ( and if products == 0, redirect ) product in page "carrello" */ if( $('.js-table-listing-cart #father_container_list_product_'+product_id+' #js-input-text-prod-'+product_id+'-mag-it-'+cart_rows_ids_deleted).length > 0 ){ deleteRowCartId('.js-table-listing-cart #father_container_list_product_'+product_id+' #js-input-text-prod-'+product_id+'-mag-it-'+cart_rows_ids_deleted, product_id, '.js-table-listing-cart', '#father_container_list_product_'+product_id); ifZeroRowsRedirectHomepage('.js-table-listing-cart .father_container'); } /** * Remove ( and if products == 0, redirect ) product in page "carrello" */ /** * Remove product in page "quick order" */ if( $('.js-table-quick-order #js-quick-order-tr-'+product_id+' #js-input-text-prod-'+product_id+'-mag-it-'+cart_rows_ids_deleted).length > 0 ){ deleteRowCartId('.js-table-quick-order #js-quick-order-tr-'+product_id+' #js-input-text-prod-'+product_id+'-mag-it-'+cart_rows_ids_deleted, product_id, '.js-table-quick-order', '#js-quick-order-tr-'+product_id); } /** * Remove product in page "quick order" */ } /** * ################################################################################################################################### */ /** * OTTIENI INFO CARRELLO AGGIORNATO */ function ottieniInfoCarrello(product_id, product_cart_ids){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/get-cart.php", type: "POST", success: function (data) { if(data.status == "success") { aggiornaPrezziAvideo(data, product_id, product_cart_ids); } else if (data.status == "error"){ if (data.reload != ''){ setTimeout(function (){ location.reload() }, 1000); } } } }); } /** * OTTIENI INFO CARRELLO AGGIORNATO */ /** * ################################################################################################################################### */ /** * AGGIORNAMENTI POST UPDATE CART */ function aggiornamentiPostUpdateCart(data, product_id, sweetAlertMessage, product_cart_ids){ if ($('.js-table-quick-order').length > 0) { getCartOnQuickOrder(); /*ottieniInfoCarrello(product_id, product_cart_ids);*/ $('.js-count-items-cart').html(data.count_elementi); } else { if (sweetAlertMessage) { infoAggiornate(data.message); } ottieniInfoCarrello(product_id, product_cart_ids); //ricalcola_totale_parziale(); $('.js-count-items-cart').html(data.count_elementi); modalHide(); } } /** * AGGIORNAMENTI POST UPDATE CART */ /** * ################################################################################################################################### */ /** * UPDATE CART */ function updateCart(product_id, quantity, newValue = '', $input = '', timer = true, sweetAlertMessage = false, add_product = 0, el = '', id_cart_row = ''){ let product_cart_ids = 0; let magToUpdateIso = (Array.isArray(mag) ? mag : Array.from(mag)).find(o => parseInt(o?.id?? "") === parseInt(quantity[0]['warehouse_id'])) || null; product_cart_ids = getValueIdRowCart(el, product_id, magToUpdateIso.iso.toLowerCase()); $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/cartProductUpdate.php", type: "POST", data: { product_id: product_id, quantity:quantity, add_product: add_product, product_cart_ids: product_cart_ids }, beforeSend: function (){ if ( $('#father_container_list_product_' + product_id + '.js-cart-container-id-cart-row-'+ product_cart_ids +' .js-totale-parziale').length > 0 ) { $('#father_container_list_product_' + product_id + '.js-cart-container-id-cart-row-'+ product_cart_ids +' .js-totale-parziale').html('
Loading...
'); } if ( $('#father_container_li_cart_' + product_id + '.js-cart-container-id-cart-row-'+ product_cart_ids +' .js-totale-parziale').length > 0 ) { $('#father_container_li_cart_' + product_id + '.js-cart-container-id-cart-row-'+ product_cart_ids +' .js-totale-parziale').html('
Loading...
'); } if ( $('#father_container_li_cart_' + product_id + '.js-cart-container-id-cart-row-'+ product_cart_ids +' .js-totale-parziale-cart').length > 0 ) { $('#father_container_li_cart_' + product_id + '.js-cart-container-id-cart-row-'+ product_cart_ids +' .js-totale-parziale-cart').html('
Loading...
'); } if( $('.js-cart-container-id-cart-row-'+product_cart_ids+' .js-totale-parziale').length > 0 ){ $('.js-cart-container-id-cart-row-'+product_cart_ids+' .js-totale-parziale').html('
Loading...
'); } if( $('.js-total-price').length > 0 ){ $('.js-total-price').html('
Loading...
'); } }, success: function (data) { if (data.status == "success") { if(timer) { clearTimeout(timerInfoAggiornate); timerInfoAggiornate = setTimeout(() => { aggiornamentiPostUpdateCart(data, product_id, sweetAlertMessage, product_cart_ids); }, timerValueInfoAggiornate); } else { aggiornamentiPostUpdateCart(data, product_id, sweetAlertMessage, product_cart_ids); } } else if (data.status == "error") { if(newValue != '' && newValue != ' ' && newValue != null && newValue != undefined && $input != '' && $input != ' ' && $input != null && $input != undefined){ $input.val( adjustQuantity( (newValue - range_increment), max_qta_ordered, range_increment) ); } if(data.error_code == 409 && data.error_text.toLowerCase().trim() == 'this cart have a different type. you must empty it before adding'){ swal({ text: data.message, type: 'error', showConfirmButton: true, showCancelButton: true, confirmButtonText: 'Empty the cart and add the selected product', cancelButtonText: 'Close', buttonsStyling: false }).then(function(result) { if (result) { emptyCartAndAddItem(data, 'product', product_id, null, quantity, newValue, $input, timer, sweetAlertMessage, add_product, el, id_cart_row); } }); setTimeout(function() { var btnStyle = 'padding:12px 28px; border:1px solid #111; background:#fff; color:#111; font-family:serif; font-size:14px; letter-spacing:0.08em; text-transform:uppercase; cursor:pointer; margin:0 8px;font-size:11px;font-family:"Helvetica Neue", Helvetica, Arial, sans-serif'; var confirmBtn = document.querySelector('.swal2-confirm'); var cancelBtn = document.querySelector('.swal2-cancel'); $('.swal2-spacer').css('height', '0px'); $('.swal2-modal').css('width', '550px'); if (confirmBtn) confirmBtn.style.cssText = btnStyle; if (cancelBtn) cancelBtn.style.cssText = btnStyle; }, 50); return; } else { swal({ text: data.message, type: "error", showConfirmButton: true }); } if(data.message == 'Unable to order this product'){ if(timer) { clearTimeout(timerInfoAggiornate); timerInfoAggiornate = setTimeout(() => { aggiornamentiPostUpdateCart(data, product_id, sweetAlertMessage, product_cart_ids); }, timerValueInfoAggiornate); } else { aggiornamentiPostUpdateCart(data, product_id, sweetAlertMessage, product_cart_ids); } } if (data.reload != ''){ setTimeout(function (){ location.reload() }, 1000); } } }, }); } /** * UPDATE CART */ /** * ################################################################################################################################### */ /** * SET JSON QUANTITY * @param id_father * @param newValue * @param $input */ function setDataProductQuantityForMag(id_father, newValue, $input){ var quantity = []; $('#'+id_father+' .js-add-cart-container-it .js-add-cart-element').each(function() { var json = {}; json = { "warehouse_id": $(this).data('warehouse-id'), "quantity": parseFloat( $('#'+id_father+' .js-add-cart-container-it .js-add-cart-element').val() ), "company_address_id": -1 } if ($('.js-add-cart-note').length > 0) { Object.assign(json, { "note": $('#'+id_father+' .js-add-cart-container-it .js-add-cart-note').val() }); } quantity.push(json); }); $('#'+id_father+' .js-add-cart-container-hk .js-add-cart-element').each(function() { var json = {}; json = { "warehouse_id": $(this).data('warehouse-id'), "quantity": parseFloat( $('#'+id_father+' .js-add-cart-container-hk .js-add-cart-element').val() ), "company_address_id": -1 } if ($('.js-add-cart-note').length > 0) { Object.assign(json, { "note": $('#'+id_father+' .js-add-cart-container-hk .js-add-cart-note').val() }); } quantity.push(json); }); product_id = $('#'+id_father+' .js-product-id').val(); updateCart(product_id, quantity, newValue, $input); } /** * SET JSON QUANTITY * @param id_father * @param newValue * @param $input */ /** * ################################################################################################################################### */ function cartProduct(data, cdn_media, html, product_id, cart_rows_ids_deleted){ let total_price = 0; $.each(data.products, function (key_p, value) { let subtotal = 0 if( value.qta_warehouse_1 != null && value.qta_warehouse_1 != undefined && value.qta_warehouse_1 != '' && value.qta_warehouse_1 != ' ' && value.qta_warehouse_2 != null && value.qta_warehouse_2 != undefined && value.qta_warehouse_2 != '' && value.qta_warehouse_2 != ' ' ) { subtotal = ((value.qta_warehouse_1 + value.qta_warehouse_2) * value.unit_price); total_price = total_price + subtotal; } else if (value.qta_warehouse_1 != null && value.qta_warehouse_1 != undefined && value.qta_warehouse_1 != '' && value.qta_warehouse_1 != ' '){ subtotal = (value.qta_warehouse_1 * value.unit_price); total_price = total_price + subtotal; } else if (value.qta_warehouse_2 != null && value.qta_warehouse_2 != undefined && value.qta_warehouse_2 != '' && value.qta_warehouse_2 != ' '){ subtotal = (value.qta_warehouse_2 * value.unit_price); total_price = total_price + subtotal; } let product_name = value.product_name; let product_id = value.product_id; let url_prodotto = trovaUrlProdotto(product_id, Object.values(data.urls_prodotti)); let qta_mag = []; if( (value.warehouse_1_country_iso_2 != '' && value.warehouse_1_country_iso_2 != ' ' && value.warehouse_1_country_iso_2 != null && value.warehouse_1_country_iso_2 != undefined) && (value.qta_warehouse_1 != '' && value.qta_warehouse_1 != ' ' && value.qta_warehouse_1 != null && value.qta_warehouse_1 != undefined) ){ let mag_1 = [value.warehouse_1_country_iso_2, value.qta_warehouse_1, 1]; qta_mag.push(mag_1); } if( (value.warehouse_2_country_iso_2 != '' && value.warehouse_2_country_iso_2 != ' ' && value.warehouse_2_country_iso_2 != null && value.warehouse_2_country_iso_2 != undefined) && (value.qta_warehouse_2 != '' && value.qta_warehouse_2 != ' ' && value.qta_warehouse_2 != null && value.qta_warehouse_2 != undefined) ){ let mag_2 = [value.warehouse_2_country_iso_2, value.qta_warehouse_2, 2]; qta_mag.push(mag_2); } html += '
  • '; html += '
    '; html += '
    '; html += ' product '; html += '
    '; html += '
    '; html += '
    '; html += '
    '; html += ''; html += '
    '+product_name+'
    '; html += '
    '; html += 'Total: '+currency+' '+ formatPrice(subtotal) +' ('+currency+ ' '+ formatPrice(value.unit_price) +'/m)'; $.each(qta_mag, function (key, value) { /* mag.forEach(function (single_mag) { */ /* if(value[0].toLowerCase() == single_mag.iso.toLowerCase()) { */ let qta_single_mag = min_qta_prod; html += '
    '; html += '' + value[0] + ''; qta_single_mag = value[1]; /*if (value[0].toLowerCase() == single_mag.iso.toLowerCase()) { qta_single_mag = value[1]; } else { qta_single_mag = min_qta_prod; }*/ html += ''; html += ''; html += ''; html += '
    '; html += '
    '; html += '
    '; html += ''; if (value[1] > 1) { html += ''; } else { html += ''; } html += '
    '; html += '
    '; html += '
    '; html += 'm.'; html += '
    '; html += '
    '; html += '
    '; html += ''; /* } */ /* }); */ }); html += '
    '; html += '
    '; html += ''; html += '
    '; html += '
    '; html += '
  • '; }); let il_tuo_carrello = data.il_tuo_carrello + ' (' + data.count_elementi + ' ' + data.elementi + ')'; if( $('.js-cart-container').length > 0){ $('.js-cart-container').css("opacity", 1); $('.js-cart-container').css("pointer-events", "auto"); } if( $('.js-cart-container-id-cart-rows').length > 0){ $('.js-cart-container-id-cart-rows').css("opacity", 1); $('.js-cart-container-id-cart-rows').css("pointer-events", "auto"); } $('.js-cart-container').html(html); $('.js-father-container-cart').addClass('active'); $('.js-il-tuo-carello-elementi').html(il_tuo_carrello); $('.js-total-price').html(currency+ formatPrice(total_price)); $('#js-btn-vai-al-carrello').html(data.btn_vai_al_carrello); if( $(".js-totale-label").length >= 1 ){ $(".js-totale-label").show(); $("#js-btn-vai-al-carrello a").removeClass('mt-0'); $("#js-btn-vai-al-carrello a").addClass('mt-4'); } toogleBtnCarrello(data); adjustQuantityOnChangeInput(); changeQtaInputText(); focusOnClick(); if(html != '' && html != ' ' && html != undefined){ $('.x-remove-product').on('click', function() { var product_id = $(this).data('product-id'); let cart_rows_ids = getCartRowsIdsFromProductId($(this), product_id); remove_product(product_id, 'cart_lateral', cart_rows_ids); }); } $('.js-count-items-cart').html(data.count_elementi); aggiornaPrezziAvideo(data, product_id); rimuoviProdottiAvideo(data, product_id, cart_rows_ids_deleted); } function cartBook(data, cdn_media, html, $page_type){ let order_to_be_processed = false; let bookUnvaiable = []; $.each(data.bunchs, function (key_p, value) { let book_type = ""; if(value.book_type == 'travel_set'){ book_type = "Travel Set"; } else { book_type = "Book"; } if(!order_to_be_processed){ if(!value.quantity_is_orderable){ bookUnvaiable.push(value.book_name + ' (' + book_type + ')'); order_to_be_processed = true; } } html += '
  • '; html += '
    '; html += '
    '; html += ''; html += '
    '; html += '
    '; html += ' book '; html += '
    '; html += '
    '; html += ' ' + value.book_name + ' (' + book_type + ')' + ' '; html += '
    '; html += ''; html += ''; html += ''; html += '
    '; html += '
    '; html += '
    '; html += '
  • '; }); let il_tuo_carrello = data.il_tuo_carrello + ' (' + data.count_elementi + ' ' + data.elementi + ')'; if( $('.js-cart-container').length > 0){ $('.js-cart-container').css("opacity", 1); $('.js-cart-container').css("pointer-events", "auto"); } if( $('.js-cart-container-id-cart-rows').length > 0){ $('.js-cart-container-id-cart-rows').css("opacity", 1); $('.js-cart-container-id-cart-rows').css("pointer-events", "auto"); } $('.js-cart-container').html(html); $('.js-father-container-cart').addClass('active'); $('.js-il-tuo-carello-elementi').html(il_tuo_carrello); $('#js-btn-vai-al-carrello').html(data.btn_vai_al_carrello); if( $(".js-totale-label").length >= 1 ){ $(".js-totale-label").hide(); $("#js-btn-vai-al-carrello a").removeClass('mt-4'); $("#js-btn-vai-al-carrello a").addClass('mt-0'); } if( $('.js-total-price').length > 0 ){ $('.js-total-price').html(''); } enable_disable_buttons_books(); toogleBtnCarrello(data, order_to_be_processed, bookUnvaiable); } function clearCartButton(count_elementi){ if(count_elementi > 0){ $('#clearCart').removeClass('d-none'); } else { $('#clearCart').addClass('d-none'); } } function cartEmpty(data, cdn_media, html){ let il_tuo_carrello = data.il_tuo_carrello + ' (' + data.count_elementi + ' ' + data.elementi + ')'; if( $('.js-cart-container').length > 0){ $('.js-cart-container').css("opacity", 1); $('.js-cart-container').css("pointer-events", "auto"); } if( $('.js-cart-container-id-cart-rows').length > 0){ $('.js-cart-container-id-cart-rows').css("opacity", 1); $('.js-cart-container-id-cart-rows').css("pointer-events", "auto"); } $('.js-cart-container').html(html); $('.js-father-container-cart').addClass('active'); $('.js-il-tuo-carello-elementi').html(il_tuo_carrello); $('#js-btn-vai-al-carrello').html(data.btn_vai_al_carrello); toogleBtnCarrello(data); if( $(".js-totale-label").length >= 1 ){ $(".js-totale-label").hide(); $("#js-btn-vai-al-carrello a").removeClass('mt-4'); $("#js-btn-vai-al-carrello a").addClass('mt-0'); } } function toogleBtnCarrello(data, order_to_be_processed = false, bookUnvaiable = []){ if(data.count_elementi > 0 && !order_to_be_processed){ $("#js-btn-vai-al-carrello").css("opacity", "1"); } else { $("#js-btn-vai-al-carrello").css("opacity", "0.4"); $("#js-btn-vai-al-carrello a").attr("href", "javascript:void(0)"); } if(order_to_be_processed){ let message = ""; bookUnvaiable.forEach(function (item){ message += item + "\n"; }); $("#js-btn-vai-al-carrello a span").text("Insufficient book availability"); $("#messageCart").html("More information "); $('#messageCart').attr('data-tooltip', 'true'); $('#messageCart').attr('data-tooltip-message', "The quantity in your cart for the following books is no longer available. Please modify the quantity or remove the books without the available quantity to proceed." + "\n \n \n" + message); } else { $("#messageCart").html(""); $('#messageCart').attr('data-tooltip', 'false'); $('#messageCart').attr('data-tooltip-message', ""); } tooltip(); } /** * COMPONE IL CARRELLO LATERALE A COMPARSA */ function getCart(product_id = '', cart_rows_ids_deleted, $page_type){ if($('.js-cart-container').length > 0){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/get-cart.php", type: "POST", success: function (data) { if (data.status == "success") { var html = ''; let cdn_media = data.cdn_media; if(data.cart_type == 'product'){ cartProduct(data, cdn_media, html, product_id, cart_rows_ids_deleted); } else if(data.cart_type == 'book'){ cartBook(data, cdn_media, html, $page_type); } else { cartEmpty(data, cdn_media, html); } clearCartButton(data.count_elementi); } else if (data.status == "error") { if(typeof data.message !== 'undefined' && data.message !== '') { swal({ text: data.message, type: "error", showConfirmButton: true }); } if (data.reload != ''){ setTimeout(function (){ location.reload() }, 1000); } } }, }); } } /** * COMPONE IL CARRELLO LATERALE A COMPARSA */ /** * ################################################################################################################################### */ /* COMPONE IL CARRELLO NEL LISTING PRODOTTI */ function getCartOnListingCart(cart_rows_ids_deleted){ if ($('.js-table-listing-cart').length > 0) { $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/get-cart.php", type: "POST", success: function (data) { if (data.status == "success") { var html = ''; let cdn_media = data.cdn_media; $.each(data.products, function (key_p, product) { let partial_price = 0; if(product.qta_warehouse_1 != '' && product.qta_warehouse_1 != '' && product.qta_warehouse_1 != null && product.qta_warehouse_1 != undefined) { partial_price = product.unit_price * product.qta_warehouse_1; } if(product.qta_warehouse_2 != '' && product.qta_warehouse_2 != '' && product.qta_warehouse_2 != null && product.qta_warehouse_2 != undefined) { partial_price = product.unit_price * product.qta_warehouse_2; } if( product.qta_warehouse_1 != '' && product.qta_warehouse_1 != '' && product.qta_warehouse_1 != null && product.qta_warehouse_1 != undefined && product.qta_warehouse_2 != '' && product.qta_warehouse_2 != '' && product.qta_warehouse_2 != null && product.qta_warehouse_2 != undefined ) { partial_price = product.unit_price * (product.qta_warehouse_1 + product.qta_warehouse_2); } html += ''; html += ''; html += '
    '; html += ''; html += '
    '; html += ' product '; html += '
    '; html += '
    '; html += ''+product.product_name+''; html += '
    '; html += ''; html += '
    '; html += ''; html += ''; html += ''+currency+' '+formatPrice(product.unit_price)+'/m.'; html += ''; mag.forEach(function (single_mag) { html += ''; html += '
    '; html += '
    '; html += ''; html += '
    '; html += ''; html += ''; html += ''; html += ''; html += '
    '; html += '
    '; html += '
    '; html += 'm.'; html += '
    '; html += '
    '; html += ''; }) html += ''; html += ''+currency+' '+partial_price.toFixed(2)+''; html += ''; html += ''; }); if( $('.js-cart-container').length > 0){ $('.js-cart-container').css("opacity", 1); $('.js-cart-container').css("pointer-events", "auto"); } if( $('.js-cart-container-id-cart-rows').length > 0){ $('.js-cart-container-id-cart-rows').css("opacity", 1); $('.js-cart-container-id-cart-rows').css("pointer-events", "auto"); } $('#js-listing-cart').html(html); $('.js-total-price').html(currency+' '+formatPrice(data.total_price)); focusOnClick(); $('.x-remove-product').on('click', function() { var product_id = $(this).data('product-id'); let cart_rows_ids = getCartRowsIdsFromProductId($(this), product_id); remove_product(product_id, 'listing_carrello', cart_rows_ids); }); } else if (data.status == "error"){ if (data.reload != ''){ setTimeout(function (){ location.reload() }, 1000); } } } }); } } /* COMPONE IL CARRELLO NEL LISTING PRODOTTI */ /** * ################################################################################################################################### */ function getCartOnDest(){ if($('.js-dest-cart').length > 0){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/get-cart.php", type: "POST", data: {grouped:1}, success: function (data) { if (data.status == "success") { var html = ''; let cdn_media = data.cdn_media; $.each(data.products, function (key_departure_dest, value) { $.each(value.products, function (key, value) { let partial_total = 0; let departure_mag = 0; let company_id_address = 0; let mag = ''; let mag_iso = ''; departure_mag = key_departure_dest.substring(0, key_departure_dest.indexOf('-')); company_id_address = key_departure_dest.substring(key_departure_dest.indexOf('-') + 1); if(departure_mag == 1){ mag = "Magazzino Italia"; mag_iso = 'it'; } if(departure_mag == 2){ mag = "Magazzino Honk Kong"; mag_iso = 'hk'; } if( value.warehouse_qta != undefined && value.warehouse_qta != '' && value.warehouse_qta != ' ' && value.warehouse_qta != null ) { partial_total = value.unit_price * value.warehouse_qta; } html += ''; html += ''; html += '' html += ''; html += '
    '; html += ''; html += '
    '; html += ' product '; html += '
    '; html += '
    '; html += ' '+value.product_name+' '; html += '
    '; html += ''; html += '
    '; html += ''; html += ''; if( value.warehouse_note != '' && value.warehouse_note != ' ' && value.warehouse_note != null && value.warehouse_note != null && value.warehouse_note != undefined ) { html += 'Edit'; } else { html += 'Add'; } html += ''; html += ''; html += ''; html += mag; html += ''; html += ''; html += '
    '; html += ''; html += '
    '; html += ''; html += ''; html += '
    '; html += ' '+value.warehouse_qta.toFixed(2)+' m.'; html += '
    '; html += ''; html += ''; html += ''+currency+' '+formatPrice(partial_total)+''; html += ''; html += ''; }); }); $('.js-dest-cart .js-dest-container-cart').html(html); $('.js-dest-cart .x-remove-product').on('click', function() { var product_id = $(this).data('product-id'); let cart_rows_ids = getCartRowsIdsFromProductId($(this), product_id); remove_product(product_id, 'destinazione_merce', cart_rows_ids); }); $('.js-add-cart-note i.it').each(function () { var $this = $(this); $this.on('click', function () { const product_id = $(this).closest('.js-add-cart-note').data('product-id'); const warehouse_id = $(this).closest('.js-add-cart-note').data('warehouse-id'); const company_address_id = $(this).closest('.js-add-cart-note').data('company-address-id'); const id_cart_row = $(this).closest('.js-add-cart-note').data('id-cart-row'); const qta = $(this).closest('.js-add-cart-note').data('qta'); $('#note_product_id').val(product_id); $('#note_mag').val('it'); getNotesFromIdProduct(product_id, 'it', warehouse_id, company_address_id, qta, id_cart_row, $this); }); }) $('.js-add-cart-note i.hk').each(function () { var $this = $(this); $this.on('click', function () { const product_id = $(this).closest('.js-add-cart-note').data('product-id'); const warehouse_id = $(this).closest('.js-add-cart-note').data('warehouse-id'); const company_address_id = $(this).closest('.js-add-cart-note').data('company-address-id'); const id_cart_row = $(this).closest('.js-add-cart-note').data('id-cart-row'); const qta = $(this).closest('.js-add-cart-note').data('qta'); $('#note_product_id').val(product_id); $('#note_mag').val('hk'); getNotesFromIdProduct(product_id, 'hk', warehouse_id, company_address_id, qta, id_cart_row, $this); }); }) ifZeroRowsRedirectHomepage('.js-dest-cart .father_container'); changeDestination(); recalculate_shipping_methods(); focusOnClick(); } else if (data.status == "error"){ if (data.reload != ''){ setTimeout(function (){ location.reload() }, 1000); } } } }); } } /** * ################################################################################################################################### */ /** * RIMUOVE UN PRODOTTO E RESTITUISCE IL CARRELLO AGGIORNATO * @param product_id */ function remove_product(product_id, partial = '', cart_rows_ids = []){ if(Array.isArray(cart_rows_ids) && cart_rows_ids.length > 0){ if( $('.js-cart-container-id-cart-row-'+cart_rows_ids[0] + ' .js-toogle-remove-product').length > 0 ) { $('.js-cart-container-id-cart-row-' + cart_rows_ids[0] + ' .js-toogle-remove-product').removeClass('x-remove-product'); } } if( $('.js-cart-container').length > 0){ $('.js-cart-container').css("opacity", 0.3); $('.js-cart-container').css("pointer-events", "none"); } if( $('.js-cart-container-id-cart-rows').length > 0 ){ $('.js-cart-container-id-cart-rows').css("opacity", 0.3); $('.js-cart-container-id-cart-rows').css("pointer-events", "none"); } clearTimeout(timeoutRemoveProduct); timeoutRemoveProduct = setTimeout(function (){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/cartProductRemove.php", type: "POST", data: { product_id: product_id, cart_rows_ids: cart_rows_ids }, beforeSend: function (data){ // disable remove product if(Array.isArray(cart_rows_ids) && cart_rows_ids.length > 0){ if( $('.js-cart-container-id-cart-row-'+cart_rows_ids[0] + ' .js-toogle-remove-product').length > 0 ) { $('.js-cart-container-id-cart-row-' + cart_rows_ids[0] + ' .js-toogle-remove-product').removeClass('x-remove-product'); } } }, success: function (data) { if (data.status == "success") { switch (partial){ case 'cart_lateral': getCart(product_id, data.cart_rows_ids_deleted); break; case 'quick_order': getCartOnQuickOrder(data.cart_rows_ids_deleted); break; case 'listing_carrello': getCartOnListingCart(data.cart_rows_ids_deleted); break; case 'destinazione_merce': getCartOnDest(); break; } $('.js-count-items-cart').html(data.count_elementi); } else if (data.status == "error") { swal({ text: data.message, type: "error", showConfirmButton: true }); if (data.reload != ''){ setTimeout(function (){ location.reload() }, 1000); } } }, error: function (data){ // enable remove product if(Array.isArray(cart_rows_ids) && cart_rows_ids.length > 0){ if( $('.js-cart-container-id-cart-row-'+cart_rows_ids[0] + ' .js-toogle-remove-product').length > 0 ) { $('.js-cart-container-id-cart-row-' + cart_rows_ids[0] + ' .js-toogle-remove-product').addClass('x-remove-product'); } } } }); }, 1000); } /** * RIMUOVE UN PRODOTTO E RESTITUISCE IL CARRELLO AGGIORNATO * @param product_id */ /** * ################################################################################################################################### */ /** * CHECKOUT 1 */ function checkout_1(){ let stop_call_ajax = false; var shipment_method_json = { shipments: [] }; const shippingElements = document.querySelectorAll('.js-shipping-container'); shippingElements.forEach(function(container) { var inputs = container.querySelectorAll('.js-shipping'); inputs.forEach(function (element) { if(element.checked) { const warehouse_id = element.dataset.warehouseId; const company_address_id = element.dataset.companyAddressId; const shipment_type = element.dataset.shipmentType; const shipment_code_element = container.querySelector('.js-shipment-code'); const shipment_code = shipment_code_element ? shipment_code_element.value : null; if (shipment_type == 'custom') { if (shipment_code == '' || shipment_code == ' ' || shipment_code == null || shipment_code == undefined) { stop_call_ajax = true; $('.js-procedi-al-pagamento').css('opacity', '0.2'); $('.js-procedi-al-pagamento').attr('disabled', 'disabled'); $('.js-procedi-al-pagamento').attr('data-disabled', 'true'); $('.js-procedi-al-pagamento').attr('href', 'javascript:void(0)'); } } shipment_method_json.shipments.push({ "warehouse_id": warehouse_id, "company_address_id": company_address_id, "shipment_type": shipment_type, "shipment_code": shipment_code }); } }) }); if(stop_call_ajax == false){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/cartCheckOut1.php", type: "POST", beforeSend: function(xhr, settings) { $('.js-procedi-al-pagamento span').html('Proceed to payment  
    Loading...
    '); }, data: { shipment_method_json: shipment_method_json }, success: function (data) { if (data.status == "success") { $('.js-procedi-al-pagamento span').text('Proceed to payment'); $('.js-procedi-al-pagamento').removeAttr('disabled'); $('.js-procedi-al-pagamento').attr('data-disabled', 'false'); $('.js-procedi-al-pagamento').css('opacity', '1'); } else if (data.status == "error") { $('.js-procedi-al-pagamento span').text('Proceed to payment'); $('.js-procedi-al-pagamento').css('opacity', '0.2'); $('.js-procedi-al-pagamento').attr('disabled', 'disabled'); $('.js-procedi-al-pagamento').attr('data-disabled', 'true'); $('.js-procedi-al-pagamento').attr('href', 'javascript:void(0)'); } }, }); } hideSpinner(); } /** * CHECKOUT 1 */ /** * ################################################################################################################################### */ /** * RICALCOLA IL BOX DEI METODI DI SPEDIZIONE */ function recalculate_shipping_methods(){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/recalculateShippingMethods.php", type: "POST", dataType: "html", success: function (data) { $('#shpping_method').html(data); checkout_1(); let keyupTimer; $('.js-shipment-code').on('keyup', function() { const element = $(this); clearTimeout(keyupTimer); keyupTimer = setTimeout(function() { const shipment_code = element.val(); checkout_1(); }, 700); }); //is_address_in_blacklist(); } }); } /** * RICALCOLA IL BOX DEI METODI DI SPEDIZIONE */ /** * ################################################################################################################################### */ /** * CONTROLLA SE GLI INDIRIZZI SCELTI SONO IN BLACKLIST */ function is_address_in_blacklist(){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/isAddressInBlackList.php", type: "POST", data: { type_cart: "products" }, dataType: "json", success: function (data) { if(data.status == "success"){ if(data.popup == "success") { hideSpinner(); let message = ""; let address = ""; if (data.dest_in_blacklist && Object.keys(data.dest_in_blacklist).length > 0) { data.dest_in_blacklist.forEach(function (dest) { dest.addresses.forEach(function (address) { message += "" + address + ''; }); message += dest.message + " \n \n "; }); } if (message != '' && message != ' ') { let message_popup = message.slice(0, -4); swal({ text: message_popup, showConfirmButton: true, confirmButtonText: data.text_button_procedi_al_pagamento }).then((result) => { if (result) { window.location.href = data.url_procedi_al_pagamento; } }); if( document.querySelector('.swal2-confirm') ){ $('.swal2-confirm').removeClass('styled'); $('.swal2-confirm').removeAttr('style'); $('.swal2-confirm').addClass('template-btn outline-btn outline-dark text-uppercase mt-0 cursor-pointer bg-white'); $('.swal2-confirm').removeAttr('style'); } } } else { window.location.href = data.url_procedi_al_pagamento; } } } }); } /** * CONTROLLA SE GLI INDIRIZZI SCELTI SONO IN BLACKLIST */ /** * ################################################################################################################################### */ /** * AGGIORNA LA DESTINAZIONE DEL PRODOTTO */ function updateAddressProduct(product_id, quantity, id_cart_row, is_first_time){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/cartProductUpdate.php", type: "POST", data: { product_id: product_id, quantity:quantity, add_product: 0, product_cart_ids:id_cart_row }, success: function (data) { if (data.status == "success") { recalculate_shipping_methods(); } else if (data.status == "error") { swal({ text: data.message, type: "error", showConfirmButton: true }); if (data.reload != ''){ setTimeout(function (){ location.reload() }, 1000); } } }, }); } /** * AGGIORNA LA DESTINAZIONE DEL PRODOTTO */ function trovaUrlProdotto(idProdotto, prodotti) { for (let i = 0; i < prodotti.length; i++) { if (prodotti[i].id_prodotto === idProdotto) { return prodotti[i].url_prodotto; } } return null; } /** * */ function id_mag_to_mag_iso_code(mag){ switch (mag){ case 1: return 'it'; break; case 2: return 'hk'; break; } } function adjustQuantityOnChangeInput(){ $('.js-table-quick-order .js-add-cart-element, .cart-drawer .js-change-input-number, .js-contenitore-aggiunta-carrello .js-add-cart-element').off().on( "focusout", function() { const prod_id = $(this).data('product-id'); const mag = $(this).data('warehouse-id'); const mag_iso_2 = id_mag_to_mag_iso_code(mag); const cart_id = getIdRowsCart($(this), prod_id, mag_iso_2); const qta_prod = $(this).val(); let qta_adjusted = adjustQuantity(qta_prod, max_qta_ordered, range_increment); if(parseFloat(qta_prod) < parseFloat(qta_adjusted)){ if( $('.message.message-qta-min').length > 0 ) { $('.message.message-qta-min').show(); } if( $('.message.message-qta-min-'+prod_id).length > 0 ) { $('.message.message-qta-min-'+prod_id).show(); } } else { if( $('.message.message-qta-min').length > 0 ) { $('.message.message-qta-min').hide(); } if( $('.message.message-qta-min-'+prod_id).length > 0 ) { $('.message.message-qta-min-'+prod_id).hide(); } } if( $( cart_id ).length > 0) { $( cart_id ).val(qta_adjusted); } else if($('.js-contenitore-aggiunta-carrello .js-add-cart-element').length > 0) { $('.js-contenitore-aggiunta-carrello .js-add-cart-element').val(qta_adjusted); } /* Arrotondamento 0.05 */ let valoreInput = qta_adjusted; valoreInput = parseFloat(valoreInput.replace(",", ".")); if (!isNaN(valoreInput)) { if (valoreInput % range_increment !== 0) { let valoreCorretto = arrotondaAlMultiplo(valoreInput); $(this).val(valoreCorretto.toFixed(2)); } else { $(this).val(valoreInput.toFixed(2)); } } /* Arrotondamento 0.05 */ }); } function getIdRowsCart(el, product_id, iso_cod, id_cart_row_note = '') { let id_cart_row = '#js-input-text-prod-' + product_id + '-mag-' + iso_cod.toLowerCase() + '-'; let cart_container_id_cart_rows = $(el).closest('.js-cart-container-id-cart-rows'); if (cart_container_id_cart_rows.length > 0) { if (product_id != '' && typeof product_id !== "undefined" && product_id != null) { let cart_row_id = cart_container_id_cart_rows.find('.js-cart-rows-ids-' + product_id + '-' + iso_cod); if (cart_row_id.length > 0) { id_cart_row += cart_row_id.val(); } } } else { if(id_cart_row_note != ''){ id_cart_row = '#js-input-text-prod-' + product_id + '-mag-' + iso_cod.toLowerCase() + '-' + id_cart_row_note; } else { id_cart_row = '#js-input-text-prod-' + product_id + '-mag-' + iso_cod.toLowerCase(); } } return id_cart_row; } function getValueIdRowCart(el, product_id, iso_cod) { let value_cart_row_id = 0; let $el = $(el); let cart_container_id_cart_rows = $el.closest('.js-cart-container-id-cart-rows'); if (cart_container_id_cart_rows.length > 0) { if (product_id != '' && typeof product_id !== "undefined" && product_id != null) { let cart_row_id = cart_container_id_cart_rows.find('.js-cart-rows-ids-' + product_id + '-' + iso_cod); if (cart_row_id.length > 0) { value_cart_row_id = cart_row_id.val(); } } } return value_cart_row_id; } function getCartRowsIdsFromProductId(el, product_id = ''){ let cart_rows_ids = []; let cart_container_id_cart_rows = el.closest('.js-cart-container-id-cart-rows'); if(product_id != '' && typeof product_id !== "undefined" && product_id != null) { let cart_row_id_it = cart_container_id_cart_rows.find('.js-cart-rows-ids-' + product_id + '-it'); if (cart_row_id_it && typeof cart_row_id_it !== "undefined" && cart_row_id_it.length > 0) { cart_rows_ids.push(cart_row_id_it.val()); } let cart_row_id_hk = cart_container_id_cart_rows.find('.js-cart-rows-ids-' + product_id + '-hk'); if (cart_row_id_hk && typeof cart_row_id_hk !== "undefined" && cart_row_id_hk.length > 0) { cart_rows_ids.push(cart_row_id_hk.val()); } } return cart_rows_ids; } function counterAndLimiter() { const textarea = document.getElementById('js-note-order-textarea'); const counter = document.getElementById('char-counter'); const maxChars = 500; if (textarea && counter) { textarea.value = ''; counter.textContent = `0 / ${maxChars}`; textarea.addEventListener('input', () => { textarea.value = textarea.value.replace(/^\s+/, ''); if (textarea.value.length > maxChars) { textarea.value = textarea.value.slice(0, maxChars); } counter.textContent = `${textarea.value.length} / ${maxChars}`; }); } } function chooseCreditCart(){ if($("#axerve").length > 0){ if( $("#axerve").is(":checked") ){ if($(".choose_credit_card").length > 0) { $(".choose_credit_card").removeClass('d-none'); } } else { if($(".choose_credit_card").length > 0) { $(".choose_credit_card").addClass('d-none'); if($("#unionpay").length > 0) { $("#unionpay").prop("checked", false); } } } } } function updateWebsiteClearCart(response, showAlert){ /** * Rimuovo le righe HTML del carrello */ if($(".js-cart-container li").length > 0){ $(".js-cart-container li").remove(); } /** * Cambio l'icona nella pagina dei book */ if( $("#booksIndice .row .js-add-books-into-cart .icon").length > 0 ){ $("#booksIndice .row .js-add-books-into-cart .icon").html(''); } /** * Aggiorno il numero degli elementi nel carrello */ if( $(".js-count-items-cart").length > 0 ){ $(".js-count-items-cart").html(response.count_elementi); } if( $(".js-il-tuo-carello-elementi").length > 0 ){ $(".js-il-tuo-carello-elementi").html(); } if( $("#js-listing-cart tr").length > 0 ){ $("#js-listing-cart tr").remove(); } if( $(".js-books-cart").length > 0 ){ $(".js-books-cart").remove(); } if( $(".table-total").length > 0 ){ $(".table-total").remove(); } if( $("#quick-order-trs .js-quick-order-tr").length > 0 ){ $("#quick-order-trs .js-quick-order-tr").remove(); } if( $(".js-dest-container-cart tr").length > 0 ){ $(".js-dest-container-cart tr").remove(); } if( $("#checkout_2 table tr").length > 0 ){ $("#checkout_2 table tr").remove(); } if( $(".cart-calculator").length > 0 ){ $(".cart-calculator").remove(); } if( $("#book_indice .js-input-update-books").length > 0 ){ $("#book_indice .js-input-update-books").val(0); $('[data-book-cart-id]').each(function() { $(this).attr('data-book-cart-id', 0); $(this).data('bookCartId', 0); // aggiorna anche la cache interna di jQuery }); } if(showAlert) { swal({ text: response.message, type: response.status, timer: 3000, showConfirmButton: false }).catch(function (dismiss) { if (dismiss === 'timer') { if (typeof response.redirect != "undefined" && response.redirect != null && response.redirect.trim() != '') { window.location.href = response.redirect; } } }); } } function asyncClearCart(tipo_oggetto, add_item_post_aysnc_clear_cart = false, $item = null, $item_id = null, $item_type = null, quantity = '', newValue = '', $input = '', timer = true, sweetAlertMessage = false, add_product = 0, el = '', id_cart_row = ''){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/clear-cart.php", type: "POST", data: { tipo_oggetto: tipo_oggetto }, success: function (response) { updateWebsiteClearCart(response, !add_item_post_aysnc_clear_cart); if(add_item_post_aysnc_clear_cart && $item == 'book'){ addBooksIntoCart($item_id, $item_type); } else if(add_item_post_aysnc_clear_cart && $item == 'product'){ updateCart($item_id, quantity, newValue, $input, timer, sweetAlertMessage, add_product, el, id_cart_row); } } }); } function clearCart(tipo_oggetto){ swal({ title: "Do you want to empty the cart?", showDenyButton: true, showCancelButton: true, confirmButtonText: "Sì", denyButtonText: "No", cancelButtonText: "No", customClass: { confirmButton: 'template-btn outline-btn outline-dark text-uppercase mt-20 cursor-pointer', denyButton: 'template-btn outline-btn outline-dark text-uppercase mt-20 cursor-pointer', cancelButton: 'template-btn outline-btn outline-dark text-uppercase mt-20 cursor-pointer' } }).then((result) => { if (result) { asyncClearCart(tipo_oggetto); } }); if( document.querySelector('.swal2-modal h2') ){ $('.swal2-modal h2').addClass('h4 ff-garamond text-black'); } if( document.querySelector('.swal2-spacer') ){ $('.swal2-spacer').addClass('d-none'); } if( document.querySelector('.swal2-cancel') ){ $('.swal2-cancel').removeClass('styled'); $('.swal2-cancel').removeAttr('style'); $('.swal2-cancel').addClass('template-btn outline-btn outline-dark text-uppercase mt-0 mx-3 cursor-pointer bg-white'); $('.swal2-cancel').removeAttr('style'); } if( document.querySelector('.swal2-confirm') ){ $('.swal2-confirm').removeClass('styled'); $('.swal2-confirm').removeAttr('style'); $('.swal2-confirm').addClass('template-btn outline-btn outline-dark text-uppercase mt-0 cursor-pointer bg-white'); $('.swal2-confirm').removeAttr('style'); } } counterAndLimiter();