1(function( $ ) { 2 'use strict'; 3 4 /** 5 * All of the code for your public-facing JavaScript source 6 * should reside in this file. 7 * 8 * Note: It has been assumed you will write jQuery code here, so the 9 * $ function reference has been prepared for usage within the scope 10 * of this function. 11 * 12 * This enables you to define handlers, for when the DOM is ready: 13 * 14 * $(function() { 15 * 16 * }); 17 * 18 * When the window is loaded: 19 * 20 * $( window ).load(function() { 21 * 22 * }); 23 * 24 * ...and/or other possibilities. 25 * 26 * Ideally, it is not considered best practise to attach more than a 27 * single DOM-ready or window-load handler for a particular page. 28 * Although scripts in the WordPress core, Plugins and Themes may be 29 * practising this, we should strive to set a better example in our own work. 30 */ 31 // Variable subscription product page â update billing info and membership card 32 // when a specific variation is selected. 33 jQuery( document ).on( 'found_variation', function( event, variation ) { 34 var $info = jQuery( '.wps-variable-sub-info' ); 35 var html = ''; 36 37 if ( variation.wps_subscription_price_html || variation.wps_subscription_expiry_html ) { 38 html += '<p class="wps-variation-sub-billing">'; 39 if ( variation.wps_subscription_price_html ) { 40 html += variation.wps_subscription_price_html; 41 } 42 if ( variation.wps_subscription_expiry_html ) { 43 html += variation.wps_subscription_expiry_html; 44 } 45 html += '</p>'; 46 } 47 48 if ( variation.wps_membership_grants_html ) { 49 html += variation.wps_membership_grants_html; 50 } 51 52 if ( html ) { 53 $info.html( html ).show(); 54 } else { 55 $info.hide().empty(); 56 } 57 } ); 58 59 jQuery( document ).on( 'reset_data', function() { 60 jQuery( '.wps-variable-sub-info' ).hide().empty(); 61 } ); 62 63 jQuery(document).ready(function($) { 64 $('.wps_sfw_subs_box-button').on('click', function(e) { 65 e.preventDefault(); 66 67 68 $('#wps_sfw_subs_box-popup').css('display','flex'); 69 $('#wps_sfw_subs_box-popup').fadeIn(); 70 71 72 }); 73 74 $('.wps_sfw_subs_box-close, #wps_sfw_subs_box-popup').on('click', function() { 75 $('#wps_sfw_subs_box-popup').fadeOut(); 76 }); 77 78 $('.wps_sfw_subs_box-content').on('click', function(e) { 79 e.stopPropagation(); 80 }); 81 82 $('.wps_sfw_sub_box_prod_add_btn').on('click', function(e) { 83 e.preventDefault(); 84 let $btn = $(this); 85 let $input = $btn.prev('.wps_sfw_sub_box_prod_count'); // Get input field 86 let $minusBtn = $input.prev('.wps_sfw_sub_box_prod_minus_btn'); // Get minus button 87 let count = parseInt($input.val()) || 0; 88 89 var wps_sfw_sub_box_price = $btn.prev('.wps_sfw_sub_box_prod_count').data('wps_sfw_sub_box_price'); 90 91 var wps_sfw_subscription_box_price = $('.wps_sfw-sb-cta-total').data('wps_sfw_subscription_box_price'); 92 93 // Get the existing price from the span and convert it to a number 94 if( wps_sfw_subscription_box_price == 0 ){ 95 96 var existing_price = parseFloat($('.wps_sfw-sb-cta-total span').text()) || 0; 97 98 // Calculate the new total price 99 var wps_sfw_sub_box_total = existing_price + wps_sfw_sub_box_price; 100 101 // Update the span with the new total 102 $('.wps_sfw-sb-cta-total span').text(wps_sfw_sub_box_total.toFixed(2)); 103 } 104 105 106 count = count + 1; 107 $input.val(count).show(); // Update and show input 108 $minusBtn.show(); // Show minus button 109 110 }); 111 112 $('.wps_sfw_sub_box_prod_minus_btn').on('click', function(e) { 113 e.preventDefault(); 114 let $btn = $(this); 115 let $input = $btn.next('.wps_sfw_sub_box_prod_count'); // Get input field 116 let count = parseInt($input.val()) || 0; 117 let $plusbtn = $('.wps_sfw_sub_box_prod_add_btn'); 118 $plusbtn.show(); 119 120 var wps_sfw_sub_box_price = $btn.next('.wps_sfw_sub_box_prod_count').data('wps_sfw_sub_box_price'); 121 122 var wps_sfw_subscription_box_price = $('.wps_sfw-sb-cta-total').data('wps_sfw_subscription_box_price'); 123 124 // Get the existing price from the span and convert it to a number 125 if( wps_sfw_subscription_box_price == 0 ){ 126 127 var existing_price = parseFloat($('.wps_sfw-sb-cta-total span').text()) || 0; 128 129 // Calculate the new total price 130 var wps_sfw_sub_box_total = existing_price - wps_sfw_sub_box_price; 131 132 // Update the span with the new total 133 $('.wps_sfw-sb-cta-total span').text(wps_sfw_sub_box_total.toFixed(2)); 134 } 135 136 count = count - 1; 137 $input.val(count) // Hide input when 0 138 if( count == 0 ){ 139 $input.val(count).hide(); 140 $btn.hide(); // Hide minus button 141 } 142 143 }); 144 145 146 $('.wps_sfw-empty-cart').on('click', function() { 147 // Send AJAX request to empty the cart 148 $.ajax({ 149 url: sfw_public_param.ajaxurl, 150 type: 'POST', 151 data: { 152 action: 'wps_sfw_sub_box_empty_cart', 153 nonce: sfw_public_param.sfw_public_nonce, 154 }, 155 success: function(response) { 156 // Show success message and update cart totals 157 alert('Your cart has been emptied!'); 158 $('.wps_sfw-empty-cart').hide(); 159 $(document.body).trigger('updated_cart_totals'); 160 } 161 }); 162 }); 163 164 $('.wps_sfw_product_page-empty-cart').on('click', function() { 165 // Send AJAX request to empty the cart 166 $.ajax({ 167 url: sfw_public_param.ajaxurl, 168 type: 'POST', 169 data: { 170 action: 'wps_sfw_sub_box_empty_cart', 171 nonce: sfw_public_param.sfw_public_nonce, 172 }, 173 success: function(response) { 174 // Show success message and update cart totals 175 alert('Your cart has been emptied!'); 176 $(document.body).trigger('updated_cart_totals'); 177 window.location.assign(window.location.href); 178 } 179 }); 180 }); 181 182 //new code. 183 $('#wps_sfw_subs_box-form').on('submit', function (e) { 184 e.preventDefault(); 185 var $form = $('#wps_sfw_subs_box-form'); 186 var $current = $form.find('.wps_sfw-sb-step:visible'); 187 if(!validateStep($current)){ 188 e.preventDefault(); 189 return false;
190 } 191 let wps_sfw_subscription_product_id = $('.wps_sfw_subscription_product_id').data('subscription-box-id'); 192 193 let formData = { 194 products: [], 195 total: $('.wps_sfw-sb-cta-total>span').text(), 196 wps_sfw_subscription_product_id: wps_sfw_subscription_product_id, 197 }; 198 199 $('.wps_sfw_sub_box_prod_container .wps_sfw_sub_box_prod_item').each(function () { 200 let container = $(this); 201 let productId = container.find('.wps_sfw_sub_box_prod_add_btn').data('product-id'); 202 let quantity = container.find('.wps_sfw_sub_box_prod_count').val(); 203 204 if (productId && quantity > 0) { 205 formData.products.push({ 206 product_id: productId, 207 quantity: quantity 208 }); 209 } 210 }); 211 212 if (formData.products.length === 0) { 213 $('.wps_sfw_subscription_box_error_notice').text('No products selected. Please add at least one product.').show(); 214 setTimeout(function() { 215 $('.wps_sfw_subscription_box_error_notice').fadeOut(500); 216 }, 5000); // Hide after 5 seconds 217 return; 218 } 219 220 $.ajax({ 221 url: sfw_public_param.ajaxurl, 222 type: 'POST', 223 data: { 224 action: 'wps_sfw_handle_subscription_box', 225 subscription_data: JSON.stringify(formData), 226 nonce: sfw_public_param.sfw_public_nonce, 227 }, 228 success: function (response) { 229 if (response.message === "Subscription added to cart!") { 230 window.location.href = sfw_public_param.cart_url; // Redirect to cart page 231 } else { 232 // var emptyCartButton = '<button type="button" class="button wps_sfw-empty-cart" id="wps_sfw-empty-cart">Empty Cartssssdsdsds</button>'; 233 $('.wps_sfw_subscription_box_error_notice').html((response.data || 'Something went wrong.') ).show(); 234 $('.wps_sfw-empty-cart').show(); 235 setTimeout(function() { 236 $('.wps_sfw_subscription_box_error_notice').fadeOut(500); 237 }, 5000); // Hide after 5 seconds 238 239 } 240 }, 241 error: function (error) { 242 console.error('Error:', error); 243 $('.wps_sfw_subscription_box_error_notice').text('Failed to process request.').show(); 244 setTimeout(function() { 245 $('.wps_sfw_subscription_box_error_notice').fadeOut(500); 246 }, 5000); // Hide after 5 seconds 247 } 248 }); 249 }); 250 251 $(document).on('click','.wps_show_customer_subscription_box_popup', function(e) { 252 e.preventDefault(); 253 $(this).next('.wps-attached-products-popup').addClass('active_customer_popup'); 254 }); 255 $(document).on('click','.wps_sfw_customer_close_popup', function(e) { 256 $(this).parent('.wps-attached-products-popup').removeClass('active_customer_popup'); 257 }); 258 259 $(document).on('click', '.wps-sfw-aurora-filter', function(e) { 260 e.preventDefault(); 261 262 var $button = $(this); 263 var filter = $button.data('filter'); 264 var $container = $button.closest('.wps-sfw-aurora-account'); 265 var $filters = $container.find('.wps-sfw-aurora-filter'); 266 var $cards = $container.find('.wps-sfw-aurora-subscription-card'); 267 var visibleCount = 0; 268 269 $filters.removeClass('is-active'); 270 $button.addClass('is-active'); 271 272 $cards.each(function() { 273 var $card = $(this); 274 var matches = filter === 'all' || $card.data('status') === filter; 275 276 $card.toggle(matches); 277 if ( matches ) { 278 visibleCount++; 279 } 280 }); 281 282 $container.find('.wps-sfw-aurora-filterbar__meta').text('Showing ' + visibleCount + ' of ' + $cards.length); 283 }); 284 285 var $form = $('#wps_sfw_subs_box-form'); 286 if(!$form.length) return; 287 288 var totalSteps = $form.find('.wps_sfw-sb-step').length; 289 var $prevBtn = $form.find('.wps_sfw-sb-prev'); 290 var $nextBtn = $form.find('.wps_sfw-sb-next'); 291 var $addBtn = $form.find('.wps_sfw_subscription_product_id'); 292 var $error = $form.find('.wps_sfw_subscription_box_error_notice'); 293 294 function showError(msg){ 295 $error.text(msg).show(); 296 setTimeout(function() { 297 $error.fadeOut(500); 298 }, 5000); // Hide after 5 seconds 299 } 300 function clearError(){ 301 $error.hide().text(''); 302 } 303 304 function validateStep($step){ 305 var min = parseInt($step.data('min-num'), 10) || 0; 306 var max = parseInt($step.data('max-num'), 10) || 0; 307 if(min === 0 && max === 0){ return true; } 308 309 var totalQty = 0; 310 $step.find('.wps_sfw_sub_box_prod_count').each(function(){ 311 totalQty += parseInt($(this).val(), 10) || 0; 312 }); 313 console.log(totalQty, min, max); 314 if(min && totalQty < min){ 315 showError("Please select at least " + min + " products in this step."); 316 return false;
317 } 318 if(max && totalQty > max){ 319 showError("You can select maximum " + max + " products in this step."); 320 return false; 321 } 322 return true; 323 } 324 325 function showStep(index){ 326 $form.find('.wps_sfw-sb-step').hide(); 327 $form.find('.wps_sfw-sb-step[data-step-index="'+index+'"]').show(); 328 clearError(); 329 330 if(index <= 1){ 331 $prevBtn.hide(); 332 } else { 333 $prevBtn.show().data('goto', index-1); 334 } 335 336 if(index >= totalSteps){ 337 $nextBtn.hide(); 338 $addBtn.show(); 339 } else { 340 $nextBtn.show().data('goto', index+1); 341 $addBtn.hide(); 342 } 343 } 344 345 // Init first step 346 showStep(1); 347 348 $prevBtn.on('click', function(e){ 349 e.preventDefault(); 350 var gotoIndex = parseInt($(this).data('goto'), 10); 351 showStep(gotoIndex); 352 }); 353 354 $nextBtn.on('click', function(e){ 355 e.preventDefault(); 356 var $current = $form.find('.wps_sfw-sb-step:visible'); 357 if(!validateStep($current)){ 358 e.preventDefault(); 359 return false; 360 } 361 var gotoIndex = parseInt($(this).data('goto'), 10); 362 showStep(gotoIndex); 363 }); 364 365 $form.on('submit', function(e){ 366 var $current = $form.find('.wps_sfw-sb-step:visible'); 367 if(!validateStep($current)){ 368 e.preventDefault(); 369 return false; 370 } 371 }); 372 373 374 }); 375})( jQuery );
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.