/* function alertBook(response){ if(response.status == 'error'){ } swal({ text: response.message, type: response.status, showConfirmButton: response.status === 'error', ...( response.status === 'success' && { timer: 2000 }) }); }*/ function updateCounterCartBook(response){ $('.js-count-items-cart').html(response.counter_el_cart); } function alertBook(response, $book_id, $type_book) { if (response.status == 'error' && response.error_code == 409 && response.error_text.toLowerCase().trim() == 'this cart have a different type. you must empty it before adding') { swal({ text: response.message, type: 'error', showConfirmButton: true, showCancelButton: true, confirmButtonText: 'Svuota il carrello e aggiungi il book selezionato', cancelButtonText: 'Chiudi', buttonsStyling: false }).then(function(result) { if (result) { emptyCartAndAddItem(response, 'book', $book_id, $type_book); } }); 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; } swal({ text: response.message, type: response.status, showConfirmButton: response.status === 'error', ...(response.status === 'success' && { timer: 2000 }) }); } function addBooksIntoCart($book_id, $type_book){ $.ajax({ url: 'https://staging-fabrics.piacenza1733.com/assets/ajax/cartBookUpdate.php', type: 'POST', data: { book_id: $book_id, company_address_id: -1, force_remove: false }, beforeSend: function() { toogleButtonsBooksCart($book_id, $type_book, true); }, success: function(response) { toogleButtonsBooksCart($book_id, $type_book, false); alertBook(response, $book_id, $type_book); /** * Update counter cart */ updateCounterCartBook(response); }, error: function(response) { alertBook(response); } }); } function emptyCartAndAddItem(response, $item = null, $item_id, $item_type, quantity, newValue = '', $input = '', timer = true, sweetAlertMessage = false, add_product = 0, el = '', id_cart_row = '') { if($item == 'book'){ asyncClearCart('recupera_password', true, $item, $item_id, $item_type); } else if ($item == 'product'){ asyncClearCart('recupera_password', true, $item, $item_id, '', quantity, newValue, $input, timer, sweetAlertMessage, add_product, el, id_cart_row); } } /* function removeBookRow($book_id){ $("#js-book-"+$book_id).remove(); }*/ function removeBookRow($book_id){ $("#js-book-"+$book_id).fadeOut(300, function(){ $(this).remove(); }); } /* function deleteRow($row, $book_id){ if( $row.length > 0 ){ $row.remove(); } if( $("#js-book-"+$book_id).length > 0 ){ $("#js-book-"+$book_id).remove(); } }*/ function deleteRow($row, $book_cart_id){ if( $row.length > 0 ){ $row.fadeOut(300, function(){ $(this).remove(); }); } if( $("#js-book-"+$book_cart_id).length > 0 ){ $("#js-book-"+$book_cart_id).fadeOut(300, function(){ $(this).remove(); }); } } function toogleButtonsBooksCart($book_id, $type_book, $disable){ if( $(".js-card-book #btnAddToCart"+$type_book+$book_id).length > 0 ){ if($disable){ $(".js-card-book #btnAddToCart"+$type_book+$book_id).css("opacity", "0.2"); $(".js-card-book #btnAddToCart"+$type_book+$book_id).prop('disabled', true); } else { $(".js-card-book #btnAddToCart"+$type_book+$book_id).css("opacity", "1"); $(".js-card-book #btnAddToCart"+$type_book+$book_id).prop('disabled', false); } } } function updateButtonGoToCart(response){ $('#js-btn-vai-al-carrello').html(response.btn_vai_al_carrello); } function updateYourCartItems(response){ let il_tuo_carrello = response.il_tuo_carrello + ' (' + response.counter_el_cart + ' ' + response.elementi + ')'; $('.js-il-tuo-carello-elementi').html(il_tuo_carrello); } function removeIconClearCart(response){ if( response.count_elementi < 1 ){ $("#clearCart").fadeOut(100, function(){ $(this).remove(); }); } } function checkRedirectOut(response, $page_type){ if( ( $page_type.toLowerCase().trim() == 'checkout_1_books' || $page_type.toLowerCase().trim() == 'checkout_2_books' ) && response.count_elementi < 1 ){ window.location.href = 'https://staging-fabrics.piacenza1733.com/it'; } } function reloadPage(response, $page_type){ if( $page_type.toLowerCase().trim() == 'checkout_2_books' && response.count_elementi >= 1 ){ window.location.href = 'https://staging-fabrics.piacenza1733.com/it/riepilogo-carrello-books'; } } function updatePageBookIndice($book_id, $qty){ if( $("#book_indice").length > 0){ $("#book_indice #inputQtyBook"+$book_id).val($qty); } } function disableBtnGoToCart(response){ if(response.counter_el_cart < 1){ $("#js-btn-vai-al-carrello").css("opacity", 0.4); $("#js-btn-vai-al-carrello a").attr("href", "javascript:void(0)"); } } function updateBookCartId($page_type, $book_id){ if( $page_type.toLowerCase().trim() == 'book_indice' ){ $(".inputQtyBook"+$book_id).attr('data-book-cart-id', 0); $(".btnAddBook"+$book_id).attr('data-book-cart-id', 0); $(".btnRemoveBook"+$book_id).attr('data-book-cart-id', 0); $(".inputQtyBook"+$book_id).data('bookCartId', 0); // aggiorna anche la cache interna di jQuery $(".btnAddBook"+$book_id).data('bookCartId', 0); $(".btnRemoveBook"+$book_id).data('bookCartId', 0); } } function asyncRemoveBook($book_id, $company_id_address, $row, $alert, $delete_row, $update_icon, $update_label, $update_button_go_to_cart, $update_your_cart_items, $force_remove, $type_book, $recalculate_shipping_method, $page_type, $book_cart_id) { if ($row.hasClass('is-loading')) { return Promise.resolve(false); } return $.ajax({ url: 'https://staging-fabrics.piacenza1733.com/assets/ajax/cartBookRemove.php', type: 'POST', data: { book_id: $book_id, company_address_id: $company_id_address, force_remove: $force_remove, book_cart_id: $book_cart_id }, beforeSend: function() { $row.addClass('is-loading'); } }) .then(function(response) { if ($alert) alertBook(response); if ($delete_row) deleteRow($row, $book_cart_id); if ($update_button_go_to_cart) updateButtonGoToCart(response); if ($update_your_cart_items) updateYourCartItems(response); disableBtnGoToCart(response); updateDocumentElementsWithBookCartId($book_id, 0); updateCounterCartBook(response); updatePageBookIndice($book_id, 0); removeIconClearCart(response); checkRedirectOut(response, $page_type); reloadPage(response, $page_type); updateBookCartId($page_type, $book_id); if ($recalculate_shipping_method) { return recalculate_shipping_methods_books(); } return true; }) .catch(function(error) { console.error('Errore in asyncRemoveBook:', error); return false; }); } function removeBook(triggerEl, $force_remove, $page_type){ let $book_id = $(triggerEl).data('book-id'); let $alert = $(triggerEl).data('alert'); let $company_id_address = -1; let $delete_row = $(triggerEl).data('delete-row'); let $update_icon = $(triggerEl).data('update-icon'); let $update_label = $(triggerEl).data('update-label'); let $update_button_go_to_cart = $(triggerEl).data('update-button-go-to-cart'); let $update_your_cart_items = $(triggerEl).data('update-your-cart-items'); let $type_book = $(triggerEl).data('type-book'); let $book_cart_id = $(triggerEl).data('book-cart-id'); let $recalculate_shipping_method = $(triggerEl).data('recalculate-shipping-method'); if(!$recalculate_shipping_method && $page_type == 'checkout_1_books'){ $recalculate_shipping_method = true; } if( (typeof $book_id == "undefined" || $book_id == null || $book_id.trim == '' || $book_id < 1) || (typeof $alert == "undefined" || $alert == null || $alert.trim == '') || (typeof $delete_row == "undefined" || $delete_row == null || $delete_row.trim == '') || (typeof $update_icon == "undefined" || $update_icon == null || $update_icon.trim == '') || (typeof $update_label == "undefined" || $update_label == null || $update_label.trim == '') || (typeof $update_button_go_to_cart == "undefined" || $update_button_go_to_cart == null || $update_button_go_to_cart.trim == '') || (typeof $update_your_cart_items == "undefined" || $update_your_cart_items == null || $update_your_cart_items.trim == '') || (typeof $type_book == "undefined" || $type_book == null || $type_book.trim == '') || (typeof $company_id_address == "undefined" || $company_id_address == null || $company_id_address.trim == '' || $company_id_address < 0) || (typeof $recalculate_shipping_method == "undefined" || $recalculate_shipping_method == null || $recalculate_shipping_method.trim == '') || (typeof $book_cart_id == "undefined" || $book_cart_id == null || $book_cart_id.trim == '') ) { return; } let $row = $("#js-book-" + $book_id); let removed = asyncRemoveBook($book_id, $company_id_address, $row, $alert, $delete_row, $update_icon, $update_label, $update_button_go_to_cart, $update_your_cart_items, $force_remove, $type_book, $recalculate_shipping_method, $page_type, $book_cart_id); if(removed){ removeBookRow($book_id); return; } $row.removeClass('is-loading'); } function asyncUpdateDestinationAddress($book_id, $company_address_id, $item, $quantity, $book_cart_id){ $.ajax({ url: 'https://staging-fabrics.piacenza1733.com/assets/ajax/cartBookUpdate.php', type: 'POST', data: { book_id: $book_id, company_address_id: $company_address_id, force_remove: false, quantity: $quantity, book_cart_id: $book_cart_id }, beforeSend: function() { }, success: function(response) { recalculate_shipping_methods_books(); return true; }, error: function(response) { alertBook(response); } }); } function changeDestinationBook(triggerEl){ let $company_address_id = triggerEl.val(); let $book_id = $(triggerEl).data('book-id'); let $quantity = $(triggerEl).data('quantity'); let $book_cart_id = $(triggerEl).data('book-cart-id'); if( (typeof $book_id == "undefined" || $book_id == null || $book_id.trim == '' || $book_id < 1) || (typeof $company_address_id == "undefined" || $company_address_id == null || $company_address_id.trim == '' || $company_address_id < 0) || (typeof $quantity == "undefined" || $quantity == null || $quantity.trim == '' || $quantity < 0) || (typeof $book_cart_id == "undefined" || $book_cart_id == null || $book_cart_id.trim == '' || $book_cart_id < 0) ){ return; } let $item = $("#js-select-dest-book-" + $book_id); let updateDestinationAddress = asyncUpdateDestinationAddress($book_id, $company_address_id, $item, $quantity, $book_cart_id); } function asyncCheckoutStep1Books(triggerEl){ $.ajax({ url: 'https://staging-fabrics.piacenza1733.com/assets/ajax/cartCheckOut1Books.php', type: 'POST', beforeSend: function() { $(triggerEl).css({ 'pointer-events': 'none', 'opacity': '0.5', 'cursor': 'default' }); $(triggerEl).removeAttr('href'); $(triggerEl).attr('aria-disabled', 'true'); }, success: function(response) { if(response.status == 'success'){ if(typeof response.url_conferma_books != "undefined" && response.url_conferma_books != null && response.url_conferma_books.trim != ''){ window.location.href = response.url_conferma_books; } } return true; }, error: function(response) { alertBook(response); $(triggerEl).css({ 'pointer-events': 'auto', 'opacity': '1', 'cursor': 'pointer' }); $(triggerEl).attr('href', '#'); $(triggerEl).attr('aria-disabled', 'false'); } }); } function asyncCheckoutStep2Books(triggerEl){ let order_note = $('#js-note-order-textarea').val(); let unionpay = $('#unionpay').is(":checked"); let paymentsRadio = $('.js-payment-choosed').toArray(); let payment = ''; if(paymentsRadio) { paymentsRadio.forEach(function (el) { if (el) { if (el.checked) { payment = el.value; } } }); if (payment == '') { swal({ text: 'Seleziona un metodo di pagamento', type: "error", showConfirmButton: true }); 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 { $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/cartCheckOut2Books.php", type: "POST", data: {payment: payment, order_note:order_note, unionpay:unionpay}, beforeSend: function (data) { $(triggerEl).attr('disabled', true).addClass('disabled').addClass('btn'); }, success: function (data) { if (data.status == "success") { window.location.replace(data.url_to_redirect_ordine_confermato); } else if (data.status == "error") { $(triggerEl).attr('disabled', false).removeClass('disabled').removeClass('btn'); swal({ text: data.message, type: "error", showConfirmButton: true }); } }, error: function (data) { $(triggerEl).attr('disabled', false).removeClass('disabled').removeClass('btn'); } }); } } } function is_address_in_blacklist_books(){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/isAddressInBlackList.php", type: "POST", data: { type_cart: "books" }, dataType: "json", success: function (data) { if(data.status == "success"){ if(data.popup == "success") { hideSpinner(1); 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 { checkoutStep1Books($(this)); } } } }); } /** * RICALCOLA IL BOX DEI METODI DI SPEDIZIONE */ function recalculate_shipping_methods_books($row){ $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/recalculateShippingMethods.php", type: "POST", dataType: "html", success: function (data) { $('#shpping_method').html(data); checkout_1_books(); let keyupTimer; $('.js-shipment-code').on('keyup', function() { const element = $(this); clearTimeout(keyupTimer); keyupTimer = setTimeout(function() { const shipment_code = element.val(); checkout_1_books(); }, 700); }); } }); } /** * RICALCOLA IL BOX DEI METODI DI SPEDIZIONE */ /** * AL FOCUS OUT DEL CAMPO DEL CODICE DEL CORRIERE RICHIAMO LA FUNZIONE DEL 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_books(); }, 700); }); /** * AL FOCUS OUT DEL CAMPO DEL CODICE DEL CORRIERE RICHIAMO LA FUNZIONE DEL CHECKOUT 1 */ /** * CHECKOUT 1 */ function checkout_1_books(){ 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-checkout-step-1-books').css('opacity', '0.2'); $('#js-checkout-step-1-books').attr('disabled', 'disabled'); $('#js-checkout-step-1-books').attr('data-disabled', 'true'); $('#js-checkout-step-1-books').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/cartCheckOut1Books.php", type: "POST", beforeSend: function(xhr, settings) { $('#js-checkout-step-1-books span').html('Conferma ordine  
Loading...
'); }, data: { shipment_method_json: shipment_method_json }, success: function (data) { if (data.status == "success") { $('#js-checkout-step-1-books span').text('Conferma ordine'); $('#js-checkout-step-1-books').removeAttr('disabled'); $('#js-checkout-step-1-books').attr('data-disabled', 'false'); $('#js-checkout-step-1-books').css('opacity', '1'); } else if (data.status == "error") { $('#js-checkout-step-1-books span').text('Conferma ordine'); $('#js-checkout-step-1-books').css('opacity', '0.2'); $('#js-checkout-step-1-books').attr('disabled', 'disabled'); $('#js-checkout-step-1-books').attr('data-disabled', 'true'); $('#js-checkout-step-1-books').attr('href', 'javascript:void(0)'); } }, }); } } /** * CHECKOUT 1 */ /** * SE ESISTE IL BUTTON "PROCEDI AL PAGAMENTO" CALCOLO IL CHECKOUT 1 */ if ($('#js-checkout-step-1-books').length > 0) { checkout_1_books(); } /** * SE ESISTE IL BUTTON "PROCEDI AL PAGAMENTO" CALCOLO IL CHECKOUT 1 */ function checkoutStep1Books(triggerEl){ asyncCheckoutStep1Books(triggerEl); } function checkoutStep2Books(triggerEl){ asyncCheckoutStep2Books(triggerEl); }