frontend-functions.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. ;(function($, window, document, undefined) {
  2. var $win = $(window);
  3. var $doc = $(document);
  4. var $field_container;
  5. $doc.ready(function() {
  6. $(document).on("booked-on-new-app", function(event) {
  7. $field_container = $('.field.field-paid-service');
  8. booked_wc_products_field($field_container);
  9. });
  10. booked_wc_btn_edit_appointment_shortcode();
  11. booked_wc_btn_edit_appointment_popup_app();
  12. booked_wc_btn_pay_appointment_shortcode();
  13. $(document).on("booked-before-loading-calendar-booking-options", function(event) {
  14. booked_wc_change_calendar_loading_paramenters();
  15. });
  16. $(document).on("booked-before-loading-booking-form", function(event) {
  17. booked_wc_change_booking_form_paramenters();
  18. });
  19. $(document).on("booked-on-requested-appointment", function(event,redirectObj) {
  20. redirectObj.redirect = booked_wc_redirect_to_checkout_if_product_option();
  21. });
  22. });
  23. function booked_wc_products_field(field_container) {
  24. var $dropdown = $('select', field_container);
  25. $dropdown.on('change', function() {
  26. var $this = $(this),
  27. calendar_id = parseInt( $this.data('calendar-id') ),
  28. product_id = $this.val(),
  29. field_name = $this.attr('name'),
  30. $variations_container = $this.parent().find('.paid-variations');
  31. booked_wc_load_variations(product_id, field_name, calendar_id, $variations_container);
  32. });
  33. }
  34. function booked_wc_load_variations( product_id, field_name, calendar_id, variations_container ) {
  35. if ( !product_id ) {
  36. variations_container.html('');
  37. return;
  38. };
  39. var data = {
  40. 'action': booked_wc_variables.prefix + 'load_variations',
  41. 'product_id': parseInt(product_id),
  42. 'calendar_id': calendar_id,
  43. 'field_name': field_name
  44. };
  45. $.post(
  46. booked_wc_variables.ajaxurl,
  47. data,
  48. function(response) {
  49. variations_container.html(response);
  50. resize_booked_modal();
  51. }
  52. );
  53. }
  54. function booked_wc_btn_edit_appointment_shortcode() {
  55. $('.booked-profile-appt-list .appt-block .edit').on('click', function(event) {
  56. event.preventDefault();
  57. var $button = $(this),
  58. appt_id = $button.attr('data-appt-id'),
  59. calendar_link = $button.attr('data-app-calendar');
  60. if (booked_wc_variables.i18n_confirm_appt_edit){
  61. confirm_edit = confirm(booked_wc_variables.i18n_confirm_appt_edit);
  62. } else {
  63. confirm_edit = true;
  64. }
  65. if ( confirm_edit === true ) {
  66. window.location.href = calendar_link;
  67. }
  68. return false;
  69. });
  70. }
  71. function booked_wc_btn_pay_appointment_shortcode() {
  72. $('.booked-profile-appt-list .appt-block .pay').on('click', function(event) {
  73. event.preventDefault();
  74. var $button = $(this),
  75. appt_id = $button.attr('data-appt-id');
  76. confirm_edit = confirm(booked_wc_variables.i18n_pay);
  77. if ( confirm_edit===true ){
  78. var data = {
  79. 'action': booked_wc_variables.prefix + 'add_to_cart',
  80. 'app_id': appt_id
  81. };
  82. jQuery.post(booked_wc_variables.ajaxurl, data, function(response) {
  83. if ( response.status === 'success' ) {
  84. window.location.href = booked_wc_variables.checkout_page;
  85. } else {
  86. alert( response.messages[0] );
  87. };
  88. }, 'json');
  89. }
  90. return false;
  91. });
  92. }
  93. function booked_wc_change_calendar_loading_paramenters() {
  94. if ( !booked_load_calendar_date_booking_options ) {
  95. return;
  96. };
  97. var current_url = window.location.href,
  98. url_parameters = current_url.replace(/^[^?]+\??/gi, ''),
  99. url_parameters_parts = url_parameters ? url_parameters.split('&') : false;
  100. if (url_parameters_parts && current_url.match('booked_wc_extension')) {
  101. // populate additional loading parameters
  102. for (var i = 0; i < url_parameters_parts.length; i++) {
  103. var data = url_parameters_parts[i].split('='),
  104. name = data[0].replace(/_\d+$/, ''),
  105. value = data[1];
  106. booked_load_calendar_date_booking_options[name] = value;
  107. }
  108. };
  109. }
  110. function booked_wc_change_booking_form_paramenters() {
  111. if ( !booked_appt_form_options ) {
  112. return;
  113. };
  114. var current_url = window.location.href,
  115. url_parameters = current_url.replace(/^[^?]+\??/gi, ''),
  116. url_parameters_parts = url_parameters ? url_parameters.split('&') : false;
  117. if (url_parameters_parts && current_url.match('booked_wc_extension')) {
  118. // populate additional loading parameters
  119. for (var i = 0; i < url_parameters_parts.length; i++) {
  120. var data = url_parameters_parts[i].split('='),
  121. name = data[0].replace(/_\d+$/, ''),
  122. value = data[1];
  123. booked_appt_form_options[name] = value;
  124. }
  125. };
  126. }
  127. function booked_wc_redirect_to_checkout_if_product_option() {
  128. var redirect = false,
  129. $form = $('form#newAppointmentForm');
  130. $('.field-paid-service', $form).each(function() {
  131. var $this = $(this);
  132. $('select', $this).each(function() {
  133. var $this_select = $(this);
  134. if ( $this_select.val()!=='' ) {
  135. redirect = true;
  136. };
  137. });
  138. });
  139. if ( redirect ) {
  140. window.location = booked_wc_variables.checkout_page;
  141. return true;
  142. }
  143. return false;
  144. }
  145. function booked_wc_btn_edit_appointment_popup_app() {
  146. $doc.on('click', '.booked-form input#submit-edit-request-appointment', function(e){
  147. var $thisButton = $(this);
  148. $('form#newAppointmentForm p.status').show().html('<i class="booked-icon booked-icon-spinner-clock booked-icon-spin"></i>&nbsp;&nbsp;&nbsp;' + booked_js_vars.i18n_please_wait);
  149. resize_booked_modal();
  150. e.preventDefault();
  151. $.ajax({
  152. type : 'post',
  153. url : booked_js_vars.ajax_url,
  154. data : $('#newAppointmentForm').serialize(),
  155. success : function(data) {
  156. data = data.split('###');
  157. var data_result = data[0].substr(data[0].length - 5);
  158. if (data_result == 'error'){
  159. $thisButton.attr('disabled',false);
  160. $thisButton.parents('form').find('button.cancel').show();
  161. $('.booked-form input').each(function(){
  162. thisDefault = $(this).attr('title');
  163. thisVal = $(this).val();
  164. if (!thisVal){ $(this).val(thisDefault); }
  165. });
  166. $('form#newAppointmentForm p.status').show().html('<i class="booked-icon booked-icon-alert" style="color:#E35656"></i>&nbsp;&nbsp;&nbsp;' + data[1]);
  167. resize_booked_modal();
  168. } else {
  169. window.location = booked_js_vars.profilePage
  170. }
  171. }
  172. });
  173. });
  174. }
  175. })(jQuery, window, document);