oont-contents/themes/old-oont-themev2/assets/js/oont.js
2025-02-08 15:10:23 +01:00

658 lines
No EOL
34 KiB
JavaScript

;(function($){
var ajaxURL = softoba_objects.ajaxurl;
$(document).ready(function() {
softoba.submitReturnFirstStep();
softoba.submitExchangeFirstStep();
softoba.getSelectedProduct();
softoba.getSelectedExchangeProduct();
softoba.uploadPreview();
softoba.showBoxIfChecked();
softoba.submitReturnFinalStep();
// softoba.submitExchangeFinalStep();
softoba.checkedExchangeBtn();
softoba.submitExchangeRequest();
softoba.payExchangeFee();
softoba.plusMinusBtn();
softoba.addedToCartAction();
softoba.updateCartQuantity();
});
var softoba = {
submitReturnFirstStep: function() {
$( '#softoba_register_return' ).on('click', function(e) {
e.preventDefault();
var $message = $('#softoba_message');
var $form = $('#softoba_return_form');
var requiredFields = $form.find( 'input[required]' );
var formdata = $form.serialize();
var hasError = false;
// Remove existing error messages
$('.error-message').remove();
$message.html("");
requiredFields.each(function() {
var inputField = $(this);
var value = inputField.val();
if (value === '') {
inputField.addClass('error-field');
inputField.after('<span class="error-message">' + inputField.attr('validation') + ' is required!</span>');
hasError = true;
} else {
inputField.removeClass('error-field');
inputField.next('.error-message').remove();
}
inputField.on('keyup', function(e) {
if (value === '') {
inputField.removeClass('error-field');
inputField.next('.error-message').remove();
hasError = true;
}
});
});
if (!hasError) {
$.ajax({
url: ajaxURL,
type: 'POST',
data: {
action: 'softoba_first_return_request',
form_data: formdata
},
dataType: 'json',
success: function(response) {
if( response.type === 'error' ) {
$message.fadeIn('slow', function() {
$message.html(`<div class="softoba_error_msg">${response.message}</div>`);
$message.delay(3000).fadeOut('slow');
});
}else if( response.type === 'success' ) {
$('.softoba_return_wrapper').addClass('loader-active');
$('.softoba-loader').show();
setTimeout( ()=> {
$('.softoba_return_wrapper').removeClass('loader-active');
$('.softoba-loader').hide();
$('#softoba-page-wrapper').html(response.output);
}, 3000);
}
}
});
}
});
},
submitExchangeFirstStep: function() {
$( '#softoba_register_exchange' ).on('click', function(e) {
e.preventDefault();
var $message = $('#softoba_message');
var $form = $('#softoba_exchange_form');
var requiredFields = $form.find( 'input[required]' );
var formdata = $form.serialize();
var hasError = false;
// Remove existing error messages
$('.error-message').remove();
$message.html("");
requiredFields.each(function() {
var inputField = $(this);
var value = inputField.val();
if (value === '') {
inputField.addClass('error-field');
inputField.after('<span class="error-message">' + inputField.attr('validation') + ' is required!</span>');
hasError = true;
} else {
inputField.removeClass('error-field');
inputField.next('.error-message').remove();
}
inputField.on('keyup', function(e) {
if (value === '') {
inputField.removeClass('error-field');
inputField.next('.error-message').remove();
hasError = true;
}
});
});
if (!hasError) {
$.ajax({
url: ajaxURL,
type: 'POST',
data: {
action: 'softoba_first_exchange_request',
form_data: formdata
},
dataType: 'json',
success: function(response) {
$('#softoba_register_exchange').next('#sofotba_exchange_pay_btn').remove();
if( response.type === 'error' ) {
$message.fadeIn('slow', function() {
$message.html(`<div class="softoba_error_msg">${response.message}</div>`);
$message.delay(3000).fadeOut('slow');
});
if( response.exchange_post_id ) {
$('#softoba_register_exchange').after( '<button id="sofotba_exchange_pay_btn" data-exchange-id="'+ response.exchange_post_id +'" >Pay Shipping fee</button>' );
}
}else if( response.type === 'success' ) {
$('.softoba_return_wrapper').addClass('loader-active');
$('.softoba-loader').show();
setTimeout( ()=> {
$('.softoba_return_wrapper').removeClass('loader-active');
$('.softoba-loader').hide();
$('#softoba-page-wrapper').html(response.output);
}, 3000);
}
}
});
}
});
},
getSelectedProduct: function() {
$(document).on('change', '#oont-select-product', function() {
var selectedProductId = $(this).val();
var orderId = $(this).data('order-id');
var itemId = $(this).find('option:selected').data('item-id');
if (selectedProductId !== '') {
$.ajax({
url: ajaxURL,
type: 'POST',
data: {
action: 'softoba_get_product_details',
order_id: orderId,
product_id: selectedProductId
},
dataType: 'html',
success: function(response) {
$('#selected_product').html(response);
$('#submit_return_request').removeAttr('disabled');
$('#selected-item-id').val(itemId);
}
});
} else {
$('#submit_return_request').attr("disabled", true);
$('#selected_product').empty();
}
});
},
getSelectedExchangeProduct: function() {
$(document).on('change', '#oont-exchange-product', function() {
var selectedProductId = $(this).val();
var orderId = $(this).data('order-id');
var itemId = $(this).find('option:selected').data('item-id');
if (selectedProductId !== '') {
$.ajax({
url: ajaxURL,
type: 'POST',
data: {
action: 'softoba_get_exchange_product_details',
order_id: orderId,
product_id: selectedProductId
},
dataType: 'html',
success: function(response) {
$('#selected_exchange_product').html(response);
$('#submit_exchange_request').attr("disabled", true);
$('#selected-item-id').val(itemId);
}
});
} else {
$('#submit_exchange_request').attr("disabled", true);
$('#selected_product').empty();
}
});
},
showBoxIfChecked: function() {
if( $('#exChangeBtn').is(':checked') ) {
$('.od_change-size').show();
$('#submit_exchange_request').removeAttr('disabled');
} else {
$('.od_change-size').hide();
$('#submit_exchange_request').attr("disabled", true);
}
$(document).on('change', '#exChangeBtn', function() {
if ($(this).is(':checked')) {
$('.od_change-size').show();
$('#submit_exchange_request').removeAttr('disabled');
} else {
$('.od_change-size').hide();
$('#submit_exchange_request').attr("disabled", true);
}
});
},
uploadPreview: function() {
$(document).on('change', '.return_reason-file', function(e) {
var fileInput = $(this);
var imagePreview = fileInput.closest('.order_details-item').find('.oont-upload-preview');
softoba.handleReturnFileUpload(fileInput, imagePreview);
});
},
handleReturnFileUpload: function(fileInput, imagePreview) {
var input = fileInput[0]; // Get the DOM element
var return_file = fileInput.prop('files')[0];
if (input.files && input.files[0]) {
var fileType = input.files[0].type;
var fileExtension = input.files[0].name.split('.').pop().toLowerCase();
if (fileType.startsWith('image/') && (fileExtension === 'jpg' || fileExtension === 'png')) {
var formData = new FormData();
formData.append('file', return_file);
formData.append('action', 'oont_return_attachments');
$.ajax({
url: ajaxURL,
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(response) {
if (response.type === 'success') {
imagePreview.append('<img src="' + response.imgurl + '" alt="Image Preview" id="inputImgPrev">');
var hiddenInput = '<input type="hidden" name="return_attachments[]" class="return_attachments" value="' + response.imgurl + '">';
fileInput.closest('.od_return_upload').append(hiddenInput);
console.log('File uploaded successfully.');
} else {
// Handle error
alert('Error uploading file.');
}
},
error: function() {
// Handle error
console.log('AJAX request failed.');
}
});
} else {
alert('Please select a valid image file (JPG or PNG)');
fileInput.val('');
imagePreview.empty();
}
}
},
submitReturnFinalStep: function(){
$(document).on('click', '#submit_return_request', function(e){
e.preventDefault();
var $message = $('#softoba_message');
$('.error-message').remove();
var check = false;
var selected_product = {};
var count = 0;
var orderid = $("#return_order_id").val();
$('.order_details-item').each(function() {
if($(this).find('.return_checkbox').is(':checked')){
check = true;
var product_info = {};
var item_id = $(this).find('.oont_item_id').val();
var reason = $(this).find('.oont-reaturn-reason').val();
var attachments = $(this).find('.return_attachments').val();
var comments = $(this).find('.return__comments').val();
var qty = $(this).find('.oont_return_qty').val();
var total_qty = $(this).find('.oont_return_total_qty').val();
var price = $(this).find('.oont_return_pprice').val();
var product_id = $(this).find('.oont_return_pid').val();
product_info['product_id'] = product_id;
product_info['reason'] = reason;
product_info['comments'] = comments;
product_info['item_id'] = item_id;
product_info['price'] = price;
product_info['tqty'] = total_qty;
product_info['rqty'] = qty;
product_info['attachments'] = attachments;
selected_product[count] = product_info;
count++;
}
});
if (check == true) {
console.log(selected_product);
var data = {
action: 'oont_return_products',
products: selected_product,
orderid: orderid,
}
$.ajax({
url: ajaxURL,
type: "POST",
data: data,
dataType :'json',
success: function(response) {
if( response.type === 'error' ) {
$message.fadeIn('slow', function() {
$message.html(`<div class="softoba_error_msg">${response.message}</div>`);
$message.delay(3000).fadeOut('slow');
});
}else if( response.type === 'success' ) {
$('.softoba_return_wrapper').addClass('loader-active');
$('.softoba-loader').show();
setTimeout( ()=> {
$('.softoba_return_wrapper').removeClass('loader-active');
$('.softoba-loader').hide();
$('#softoba-page-wrapper').html(response.html);
}, 3000);
}
}
});
}
// if (!hasError) {
// formdata.append( 'action', 'softoba_final_return_request' );
// var ajaxSettings = {
// type: 'POST',
// url: ajaxURL,
// data: formdata,
// success: function ( response ) {
// if( response.type === 'error' ) {
// $message.fadeIn('slow', function() {
// $message.html(`<div class="softoba_error_msg">${response.message}</div>`);
// $message.delay(3000).fadeOut('slow');
// });
// }else if( response.type === 'success' ) {
// $('.softoba_return_wrapper').addClass('loader-active');
// $('.softoba-loader').show();
// setTimeout( ()=> {
// $('.softoba_return_wrapper').removeClass('loader-active');
// $('.softoba-loader').hide();
// $('#softoba-page-wrapper').html(response.html);
// }, 3000);
// }
// },
// error: function ( jqXHR, text_status, error_thrown ) {
// $('#submit_return_request').removeAttr('disabled');
// },
// complete: function () {
// },
// cache: false,
// contentType: false,
// processData: false
// };
// $.ajax( ajaxSettings );
// }
})
},
checkedExchangeBtn: function() {
$(document).on('click', '.exchange-btn-inner', function(e) {
if($(this).find('.exchange_card_check').is(':checked')){
$(this).next('.od_change-size').show();
}else{
$(this).next('.od_change-size').hide();
}
// Return
if($(this).find('.return_checkbox').is(':checked')){
$(this).next('.return_reason_card').show();
}else{
$(this).next('.return_reason_card').hide();
}
});
},
submitExchangeRequest: function() {
$(document).on('click', '#submit_exchange_request', function(e){
e.preventDefault();
var $message = $('#softoba_message');
$('.error-message').remove();
var check = false;
var selected_product = {};
var count = 0;
var orderid = $("#ex_order_id").val();
$('.order_details-item').each(function() {
if($(this).find('.exchange_card_check').is(':checked')){
check = true;
var product_info = {};
var ordered_variation = $(this).find('.ex_ordered_variation').val();
var exchange_variation = $(this).find('.exchange_variation').val();
var item_id = $(this).find('.ex_item_id').val();
var exchange_qty = $(this).find('.ex_exchange_qty').val();
var product_qty = $(this).find('.ex_total_qty').val();
var product_price = $(this).find('.ex_product_price').val();
var product_id = $(this).find('.ex_product_id').val();
product_info['product_id'] = product_id;
product_info['variation_id'] = ordered_variation;
product_info['ex_variation_id'] = exchange_variation;
product_info['item_id'] = item_id;
product_info['price'] = product_price;
product_info['eqty'] = exchange_qty;
product_info['qty'] = product_qty;
selected_product[count] = product_info;
count++;
}
});
if (check == true) {
var data = {
action: 'oont_exchange_products',
products: selected_product,
orderid: orderid,
}
$.ajax({
url: ajaxURL,
type: "POST",
data: data,
dataType :'json',
success: function(response) {
if( response.type === 'error' ) {
$message.fadeIn('slow', function() {
$message.html(`<div class="softoba_error_msg">${response.message}</div>`);
$message.delay(3000).fadeOut('slow');
});
}else if( response.type === 'success' ) {
$('.softoba_return_wrapper').addClass('loader-active');
$('.softoba-loader').show();
setTimeout( ()=> {
$('.softoba_return_wrapper').removeClass('loader-active');
$('.softoba-loader').hide();
$('#softoba-page-wrapper').html(response.html);
}, 3000);
}
}
});
// console.log( selected_product );
}
});
},
payExchangeFee: function(){
$(document).on('click', '#sofotba_exchange_pay_btn', function(e) {
e.preventDefault();
var exchangeId = $(this).data('exchange-id');
$('.softoba_return_wrapper').addClass('loader-active');
$('.softoba-loader').show();
var ex_data = {
action: 'softoba_pay_exchange_fee',
exchangeId: exchangeId,
}
// console.log(ex_data);
$.ajax({
type: 'POST',
url: ajaxURL,
data: ex_data,
success: function(res) {
if( res.type == 'success' ) {
window.location.replace(res.redirect_to);
}
console.log(res);
},
error: function(error) {
console.log(error)
}
});
});
},
plusMinusBtn: function() {
$('body').on('click', '.plus, .minus', function() {
// Get values
var $qty = $(this).parents('.quantity').find('.qty'),
form = $(this).parents('.cart'),
button_add = $(form).length ? $(form).find('.single_add_to_cart_button') : false,
currentVal = parseFloat($qty.val()),
max = parseFloat($qty.attr('max')),
min = parseFloat($qty.attr('min')),
step = $qty.attr('step');
var _old_val = $qty.val();
$qty.attr('data-old', _old_val);
// Format values
currentVal = !currentVal ? 0 : currentVal;
max = !max ? '' : max;
min = !min ? 0 : min;
if (
step === 'any' ||
step === '' ||
typeof step === 'undefined' ||
parseFloat(step) === 'NaN'
) {
step = 1;
}
// Change the value Plus
if ($(this).hasClass('plus')) {
if (max && (max == currentVal || currentVal > max)) {
$qty.val(max);
if (button_add && button_add.length) {
button_add.attr('data-quantity', max);
}
} else {
$qty.val(currentVal + parseFloat(step));
if (button_add && button_add.length) {
button_add.attr('data-quantity', currentVal + parseFloat(step));
}
}
}
// Change the value Minus
else {
if (min && (min == currentVal || currentVal < min)) {
$qty.val(min);
if (button_add && button_add.length) {
button_add.attr('data-quantity', min);
}
} else if (currentVal > 0) {
$qty.val(currentVal - parseFloat(step));
if (button_add && button_add.length) {
button_add.attr('data-quantity', currentVal - parseFloat(step));
}
}
}
console.log( $qty );
// Trigger change event
$qty.trigger('change');
});
},
addedToCartAction: function() {
$('body').on('added_to_cart', function(ev, fragments, cart_hash, popup, event_add) {
var _popup = typeof popup !== 'undefined' && popup === false ? false : true;
// var _event_add = typeof event_add === 'undefined' ? $('input[name="softoba-event-after-add-to-cart"]').length && $('input[name="softoba-event-after-add-to-cart"]').val() : event_add;
if ($('#cart-sidebar').length > 0) {
_event_add = 'sidebar';
}
if ($('.widget_shopping_cart_content').find('.mini_cart_item').length > 0) {
/**
* * Show Mini Cart Sidebar
*/
if (_event_add === 'sidebar') {
setTimeout(function() {
$('.softoba-static-sidebar').fadeIn(200).addClass('oont-active');
$('.softoba-overlay').addClass('active');
if ($('#cart-sidebar').length && !$('#cart-sidebar').hasClass('oont-active')) {
$('#cart-sidebar').addClass('oont-active');
}
/**
* notification free shipping
*/
// init_shipping_free_notification($, true);
}, 200);
}
}
});
},
updateCartQuantity: function() {
$('body').on('change', '.softoba_single_cart_item .qty', function(e) {
// alert('Chaanged');
if( typeof oont_wc_handle !== 'undefined' ) {
// alert('Working with this, Please wait...');
// var _urlAjax = oont_wc_handle.wc_ajax_url.toString().replace('%%endpoint%%', 'softoba_quantity_mini_carttttttt');
var _input = $(this);
var _wrap = $(_input).parents('.softoba_single_cart_item');
var _hash = $(_input).attr('name').replace(/cart\[([\w]+)\]\[qty\]/g, "$1");
var _max = parseFloat($(_input).attr('max'));
if (!_max) {
_max = false;
}
var _quantity = parseFloat($(_input).val());
var _old_val = parseFloat($(_input).attr('data-old'));
if (!_old_val) {
_old_val = _quantity;
}
if (_max > 0 && _quantity > _max) {
$(_input).val(_max);
_quantity = _max;
}
if (_old_val !== _quantity) {
var _confirm = true;
if (_confirm) {
$.ajax({
url: ajaxURL,
type: 'POST',
dataType: 'json',
cache: false,
data: {
hash: _hash,
quantity: _quantity,
action: 'softoba_quantity_mini_cart'
},
beforeSend: function () {
if (!$(_wrap).hasClass('soba-loading')) {
$(_wrap).addClass('soba-loading');
}
if ($(_wrap).find('soba-loader').length >= 0) {
$(_wrap).append('<div class="soba-loader"></div>');
}
},
success: function (data) {
if (data && data.fragments) {
$.each(data.fragments, function(key, value) {
if ($(key).length) {
$(key).replaceWith(value);
}
});
$('body').trigger('added_to_cart', [data.fragments, data.cart_hash, false, false]);
$('body').trigger('wc_fragments_refreshed');
}
$('.woocommerce-mini-cart-item').find('.soba-loader').remove();
},
error: function (xhr, textStatus, errorThrown) {
$(document.body).trigger('wc_fragments_ajax_error');
$('#soba-sidebar').find('.soba-loader').remove();
$('#soba-sidebar').find('.soba-loading').removeClass('soba-loading');
}
});
}
}
}
});
}
}
})(jQuery);