1/* 2 * JavaScript for front-end discount code display 3 * 4 */ 5jQuery(document).ready(function($) { 6 7 // Cache the value of the last checked discount code 8 var last_checked_discount_code; 9 10 /* 11 * Show / Hide discount code field if a free plan is selected 12 * 13 */ 14 toggle_discount_box( $('input[name=subscription_plans][type=radio]').length > 0 ? $('input[name=subscription_plans][type=radio]:checked') : $('input[name=subscription_plans][type=hidden]') ) 15 16 /** 17 * Trigger automatically "Apply" discount button when the user already entered a discount code and selects another subscription plan, or checks the "Automatically renew subscription" checkbox. 18 * This will update the discount message shown below the field. 19 * 20 */ 21 $(document).on('click', '.pms-subscription-plan input[type="radio"][name="subscription_plans"]', function(){ 22 23 // If subscription is not free and discount code field is not empty 24 if (($(this).attr("data-price") > 0) && ($('#pms_subscription_plans_discount_code').length > 0)) { 25 26 $('#pms-apply-discount').trigger('click') 27 28 } else { 29 30 $('#pms-subscription-plans-discount-messages-wrapper').hide() 31 $('#pms-subscription-plans-discount-messages').hide() 32 33 } 34 35 toggle_discount_box( $(this) ) 36 37 }) 38 39 $(document).on('click', '.pms-subscription-plan-auto-renew input[type="checkbox"][name="pms_recurring"]', function () { 40 41 // If discount code field is not empty 42 if ( $('#pms_subscription_plans_discount_code').length > 0 ){ 43 44 $('#pms-apply-discount').trigger('click') 45 46 } else { 47 48 $('#pms-subscription-plans-discount-messages-wrapper').hide() 49 $('#pms-subscription-plans-discount-messages').hide() 50 51 } 52 53 toggle_discount_box( $($pms_checked_subscription) ) 54 55 }) 56 57 /** 58 * Toggle discount code box when the subscription plans field is shown from the 59 * Profile Builder Conditional Logic functionality 60 */ 61 $(document).on("wppbAddRequiredAttributeEvent", function (e) { 62 if ($(e.target).is('#pms_subscription_plans_discount_code')) 63 toggle_discount_box($('input[name=subscription_plans][type=radio]').length > 0 ? $('input[name=subscription_plans][type=radio]:checked') : $('input[name=subscription_plans][type=hidden]')) 64 }) 65 66 /** 67 * Handles discount code validation when the user clicks the "Apply" discount button 68 * 69 */ 70 $(document).on('click', '#pms-apply-discount', function(e){ 71 72 e.preventDefault(); 73 74 // If undefined, cache the parent form 75 if( typeof $pms_form == 'undefined' ) 76 $pms_form = $(this).closest('form'); 77 78 var $subscription_plan = ''; 79 80 $('.pms-subscription-plan input[type="radio"]').each(function(){ 81 if($(this).is(':checked')){ 82 $subscription_plan = $(this); 83 } 84 }); 85 86 if( $subscription_plan == '' ) { 87 $subscription_plan = $('input[type=hidden][name=subscription_plans]'); 88 } 89 90 if( $('#pms_subscription_plans_discount_code').val() == '' ) { 91 $('#pms-subscription-plans-discount-messages-wrapper').fadeOut( 350 ); 92 $('#pms-subscription-plans-discount-messages').fadeOut( 350 ) 93 94 $subscription_plan.data( 'discounted-price', false ) 95 jQuery(document).trigger( 'pms_discount_error' ) 96 97 return false; 98 } 99 100 // Cache the discount code 101 last_checked_discount_code = $('#pms_subscription_plans_discount_code').val(); 102 103 pwyw_price = ''; 104 105 if ( $('input[name="subscription_price_'+$subscription_plan.val()+'"]').length != 0 ) 106 pwyw_price = $('input[name="subscription_price_'+$subscription_plan.val()+'"]').val(); 107 108 var data = { 109 'action' : 'pms_discount_code', 110 'code' : $.trim( $('#pms_subscription_plans_discount_code').val()), 111 'subscription' : $subscription_plan.val(), 112 'recurring' : $('input[name="pms_recurring"]:checked').val(), 113 'pwyw_price' : pwyw_price, 114 'pmstkn_original' : $pms_form.find('input[name="pmstkn"]').val(), 115 'pms_current_subscription': $pms_form.find('input[name="pms_current_subscription"]').val(), 116 'form_action' : $pms_form.find('input[name="form_action"]').val(), 117 }; 118 119 var currency = new URLSearchParams(window.location.search).get('pms_mc_currency'); 120 121 if ( currency ) 122 data['pms_mc_currency'] = currency; 123 124 if( data.pmstkn_original === undefined && jQuery( '.wppb-register-user' ).length > 0 ) 125 data.pmstkn_original = 'pb_form' 126 127 // Make sure it's not an empty discount 128 if ( data['code'] !== '' ) { 129 130 $('#pms-subscription-plans-discount-messages').hide() 131 $('#pms-subscription-plans-discount-messages-wrapper').show() 132 $('#pms-subscription-plans-discount-messages-loading').fadeIn(350) 133 134 // We can also pass the url value separately from aja
134xurl for front end AJAX implementations 135 jQuery.post(pms_discount_object.ajax_url, data, function (response) { 136 137 if (response.success != undefined) { 138 139 // Add success message 140 $('#pms-subscription-plans-discount-messages').removeClass('pms-discount-error') 141 $('#pms-subscription-plans-discount-messages').addClass('pms-discount-success') 142 143 $('#pms-subscription-plans-discount-messages-loading').fadeOut(350, function () { 144 $('#pms-subscription-plans-discount-messages').html(response.success.message).fadeIn(350) 145 }) 146 147 // Hide payment fields 148 if (response.is_full_discount) 149 $.pms_hide_payment_fields( $pms_form ) 150 else 151 $.pms_show_payment_fields( $pms_form ) 152 153 $subscription_plan.data( 'price-original', $subscription_plan.data('price') ) 154 $subscription_plan.data( 'price', response.discounted_price ) 155 $subscription_plan.data( 'discounted-price', true ) 156 $subscription_plan.data( 'discounted-price-value', response.original_discounted_price ) 157 158 if( response.is_full_discount == true ){ 159 160 if( response.recurring_payments == 1 ){ 161 $( 'input[name="pms_recurring"]', $pms_auto_renew_field ).prop( 'checked', true ) 162 $pms_auto_renew_field.hide() 163 } 164 165 $subscription_plan.data( 'is-full-discount', true ) 166 167 } else 168 $subscription_plan.data( 'is-full-discount', false ) 169 170 $subscription_plan.data( 'discount-recurring-payments', response.recurring_payments ) 171 172 jQuery(document).trigger( 'pms_discount_success' ) 173 174 } 175 176 if (response.error != undefined) { 177 178 // Add error message 179 $('#pms-subscription-plans-discount-messages').removeClass('pms-discount-success') 180 $('#pms-subscription-plans-discount-messages').addClass('pms-discount-error') 181 182 $('#pms-subscription-plans-discount-messages-loading').fadeOut(350, function () { 183 $('#pms-subscription-plans-discount-messages').html(response.error.message).fadeIn(350) 184 }) 185 186 // Show payment fields 187 $.pms_show_payment_fields( $pms_form ) 188 189 $subscription_plan.data( 'price', $subscription_plan.data('price-original') ) 190 $subscription_plan.data( 'discounted-price', false ) 191 $subscription_plan.data( 'discounted-price-value', 0 ) 192 193 jQuery(document).trigger( 'pms_discount_error' ) 194 195 } 196 197 }); 198 } else { 199 200 $subscription_plan.data( 'price', $subscription_plan.data('price-original') ) 201 $subscription_plan.data( 'discounted-price', false ) 202 203 jQuery(document).trigger( 'pms_discount_error' ) 204 205 } 206 207 }) 208 209 /** 210 * If there is a discount code value already set on document ready 211 * apply it 212 * 213 */ 214 if( $('input[name=discount_code]').val() != '' ) 215 $('#pms-apply-discount').trigger('click') 216 217 /** 218 * When losing focus of the discount code field, directly apply the discount 219 * 220 */ 221 $('input[name=discount_code]').on( 'blur', function() { 222 223 if( last_checked_discount_code != $('input[name=discount_code]').val() ) 224 $('#pms-apply-discount').trigger('click'); 225 226 if ( $('input[name=discount_code]').val() == '' ) 227 $.pms_show_payment_fields( $pms_form ); 228 }) 229 230 /* 231 * Show / Hide discount code field if a free plan is selected 232 * 233 */ 234 function toggle_discount_box( $element ) { 235 236 if( !$element ) 237 return 238 239 var selector = '#pms-subscription-plans-discount'; 240 241 if( !subscription_has_discount( $element.val() ) ) 242 $(selector).hide() 243 else { 244 if ( $element.attr('data-price') == '0' ) { 245 246 if ( $.isFunction( $.pms_plan_is_prorated ) && $.pms_plan_is_prorated( $element ) ) { 247 248 if ( $('input[name="pms_recurring"]', $('.pms-subscription-plan-auto-renew') ).prop('checked') || $element.data('recurring') == 2 ){ 249 $(selector).show() 250 return 251 } 252 253 } 254 255 if ( $.isFunction( $.pms_plan_has_signup_fee ) && $.pms_plan_has_signup_fee( $element ) ) 256 $(selector).show() 257 258 $(selector).hide() 259 260 } else { 261 $(selector).show() 262 } 263 } 264 265 } 266 267 function subscription_has_discount( subscription_id ){ 268 269 // show the box if we don't have data available (old default) 270 if( typeof pms_discount_object == 'undefined' || typeof pms_discount_object.discounted_subscriptions == 'undefined' ) 271 return true 272 273 let return_value = false 274 let subscriptions = JSON.parse( pms_discount_object.discounted_subscriptions ) 275 276 for ( var subscription in subscriptions ){ 277 if( subscription_id == subscriptions[subscription] ) 278 return_value = true 279 } 280 281 return return_value 282 283 } 284 285});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.