Created
May 5, 2020 07:04
-
-
Save acanza/4b90f7d7bf51d63c9553c80276e6ad7c to your computer and use it in GitHub Desktop.
JS to modify calendar performance in Booking and Appointment for WooCommerce plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function ($) { | |
if ((0 == $("#booking_calender").length)) { | |
return; | |
} | |
// Initialize var | |
var old_checkout_date = ""; | |
if ($('#wapbk_hidden_date_checkout').length > 0) { | |
old_checkout_date = $('#wapbk_hidden_date_checkout').val(); | |
} | |
// Append max rent term notice HTML | |
$(".bkap_end_date").before('\ | |
<div id="max_rent_term_notice" style="font-weight: bold; display: none;">\ | |
<p>MÁXIMO UNA SEMANA DE ALQUILER</p>\ | |
</div>'); | |
// Append negative rent term notice HTML in "My account > Review order" page | |
if ($(".woocommerce-view-order").length > 0) { | |
$(".bkap_end_date").before('\ | |
<div id="negative_rent_term_notice" style="font-weight: bold; display: none;">\ | |
<p>NO PUEDES INDICAR UNA FECHA ANTERIOR A LA FECHA DE DEVOLUCIÓN ACTUAL</p>\ | |
</div>'); | |
} | |
$("table.variations tbody select").on('change', function () { | |
set_variation_id_before_calculate_price(); | |
force_one_week_to_booking($(this), old_checkout_date); | |
}); | |
$("#booking_calender").datepicker().on('input change', function () { | |
force_one_week_to_booking($(this), old_checkout_date); | |
}); | |
$("#booking_calender_checkout").datepicker().on('input change', function () { | |
force_one_week_to_booking($(this), old_checkout_date); | |
}); | |
function force_one_week_to_booking(calendar, old_checkout_date) { | |
var checkin_class = "#booking_calender"; | |
var checkout_class = "#booking_calender_checkout"; | |
var checkin_date = ""; | |
var checkout_date = ""; | |
var checkout_date_obj = ""; | |
var rent_term = ""; | |
var is_my_account = false; | |
// Check if current page is "My account" | |
if ($(".woocommerce-view-order").length > 0) { | |
var product_id = calendar.closest('.modal-body').attr('id').split('-'); | |
product_id = product_id[product_id.length-1]; | |
rent_term = products_data[product_id].tipo_alquiler; | |
is_my_account = true; | |
} else { | |
rent_term = $("select#pa_tipo-alquiler").children("option:selected").val(); | |
} | |
// Check if checkin date exist | |
if ($(checkin_class).val() === "") { | |
return; | |
}else { | |
checkin_date = $(checkin_class).val(); | |
// Debug | |
console.log( 'checkin_date = ' + checkin_date ); | |
} | |
// Check if check out date exist | |
if ($(checkout_class).val() === "") { | |
return; | |
}else { | |
checkout_date = $(checkout_class).val(); | |
//Debug | |
console.log( 'checkout_date = ' + checkout_date ); | |
} | |
checkin_date = checkin_date.split('/').join('-'); | |
checkout_date = checkout_date.split('/').join('-'); | |
var checkin_date_obj = bkap_functions.bkap_create_date_obj(checkin_date); | |
$(checkin_class).datepicker("refresh"); | |
$(checkout_class).datepicker("refresh"); | |
$("#wapbk_hidden_date").val(checkin_date); | |
if (checkout_date.length > 0) { | |
// Clean the notices messages | |
$("#max_rent_term_notice").hide(); | |
$("#negative_rent_term_notice").hide(); | |
if ('semanas' == rent_term) { | |
var days = 0; | |
// Calculate the difference days between calendar dates | |
if (is_my_account) { | |
days = daysBetween(old_checkout_date, checkout_date); | |
} else { | |
days = daysBetween(checkin_date, checkout_date); | |
} | |
if (days > 0 && days != 7) { | |
days = 7; | |
// Show max rent term notice box | |
$("#max_rent_term_notice").show(); | |
} else if (days < 0) { | |
days = 0; | |
// Show negative rent term notice box | |
$("#negative_rent_term_notice").show(); | |
} | |
/** | |
* If user has rescheduled the rent term in My account, then add one week to the previous check out date | |
* instead of to the checkin date | |
*/ | |
if (is_my_account) { | |
checkout_date_obj = bkap_functions.bkap_create_date_obj(old_checkout_date); | |
checkout_date_obj = bkap_functions.bkap_add_days_to_date(checkout_date_obj, days); | |
} else { | |
checkout_date_obj = bkap_functions.bkap_add_days_to_date(checkin_date_obj, days); | |
} | |
} else { | |
checkout_date_obj = bkap_functions.bkap_create_date_obj(checkout_date); | |
} | |
} | |
date = bkap_functions.bkap_create_date(checkout_date_obj); | |
$("#wapbk_hidden_date_checkout").val(date); | |
$(checkout_class).datepicker("setDate", checkout_date_obj); | |
if ($("#wapbk_hidden_date").val() !== "" && $("#wapbk_hidden_date_checkout").val() !== "") { | |
bkap_calculate_price(); | |
// Show variation price box | |
$("form.variations_form .woocommerce-variation-price").show(); | |
} | |
} | |
function set_variation_id_before_calculate_price() { | |
var attributes_data = {}; | |
var rent_type = ""; | |
var selected_rent_term = $("select#pa_tipo-alquiler").children("option:selected").val(); | |
var product_variations_data = $("form.variations_form").data('product_variations'); | |
for (let variation of product_variations_data) { | |
if (variation.attributes['attribute_pa_tipo-alquiler'] !== "") { | |
rent_type = variation.attributes['attribute_pa_tipo-alquiler']; | |
attributes_data[rent_type] = variation.variation_id; | |
} else { | |
console.log('No existe el atributo: attribute_pa_tipo-alquiler'); | |
} | |
} | |
if (selected_rent_term !== "" && attributes_data[selected_rent_term] !== "") { | |
$("form.variations_form .variation_id").val(attributes_data[selected_rent_term]); | |
} | |
} | |
// Processing the dates in order to subtract | |
function parseDate(str) { | |
var mdy = str.split('-'); | |
return new Date(mdy[2], mdy[1], mdy[0]-1); | |
} | |
// Return the difference days between two dates | |
function daysBetween(startDate, endDate) { | |
// Take the difference between the dates and divide by milliseconds per day. | |
// Round to nearest whole number to deal with DST. | |
if (startDate.length == 0 || endDate.length == 0) { | |
console.log( 'Error: [function - daysBetween] Al menos una de las fechas está vacía' ); | |
return false; | |
} | |
return Math.round((parseDate(endDate) - parseDate(startDate)) / (1000*60*60*24)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment