1jQuery(document).ready(function($){ 2 3 // Hook into the added_to_cart event 4 $(document.body).on('added_to_cart', function () { 5 // Trigger a mini cart update 6 $.ajax({ 7 url: wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'get_refreshed_fragments'), 8 type: 'POST', 9 success: function (data) { 10 if (data && data.fragments) { 11 $.each(data.fragments, function (key, value) { 12 $(key).replaceWith(value); 13 }); 14 } 15 } 16 }); 17 }); 18 19 // Listen for variation selection change 20 $('form.variations_form').on('show_variation', function(event, variation){ 21 // Get your location-based price & stock from the variation data 22 var locRegular = variation.wlf_location_regular_price; 23 var locSale = variation.wlf_location_sale_price; 24 var locSaleStart = variation.wlf_location_sale_start; 25 var locSaleEnd = variation.wlf_location_sale_end; 26 var locStockStatus = variation.wlf_location_stock_status; 27 var locStockQty = variation.wlf_location_stock_qty; 28 29 // Determine if sale is valid 30 var now = new Date(); 31 var showSale = false; 32 if(locSale && locSale !== '') { 33 if((!locSaleStart || locSaleStart <= now.toISOString().slice(0,10)) && 34 (!locSaleEnd || locSaleEnd >= now.toISOString().slice(0,10))) { 35 showSale = true; 36 } 37 } 38 39 // Format price (reuse your PHP format logic in JS if possible) 40 function wlf_format_price(price) { 41 var symbol = wlf_front_vars.currency_symbol || 'â¹'; 42 var pos = wlf_front_vars.currency_position || 'left'; 43 var thousand = wlf_front_vars.thousand_separator || ','; 44 var decimal = wlf_front_vars.decimal_separator || '.'; 45 var decimals = wlf_front_vars.decimals || 2; 46 if(price === undefined || price === null || price === '') return ''; 47 var num = parseFloat(price).toFixed(decimals).toString(); 48 var parts = num.split('.'); 49 parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousand); 50 var formatted = parts.join(decimal); 51 switch(pos){ 52 case 'left': return symbol + formatted; 53 case 'right': return formatted + symbol; 54 case 'left_space': return symbol + ' ' + formatted; 55 case 'right_space': return formatted + ' ' + symbol; 56 default: return symbol + formatted; 57 } 58 } 59 60 // Set price HTML 61 var $priceHtml = $('.single_variation .price'); 62 if(showSale) { 63 $priceHtml.html('<del>' + wlf_format_price(locRegular) + '</del> <ins>' + wlf_format_price(locSale) + '</ins>'); 64 } else if(locRegular && locRegular !== '') { 65 $priceHtml.html(wlf_format_price(locRegular)); 66 } 67 68 // Set stock HTML (create or update a custom container) 69 var $stockHtml = $('.wlf-location-variation-stock-info'); 70 if($stockHtml.length === 0) { 71 $stockHtml = $('<div class="wlf-location-variation-stock-info"></div>'); 72 $('.single_variation').prepend($stockHtml); 73 } 74 if(locStockStatus === 'instock') { 75 $stockHtml.html('<span class="stock in-stock" style="color:green;font-weight:600;">In stock'+ 76 (locStockQty !== undefined && locStockQty !== '' ? ' ('+locStockQty+' available)' : '')+ 77 '</span>'); 78 $('.single_add_to_cart_button').prop('disabled', false).show(); 79 $('.variations_form .quantity').show(); 80 } else if(locStockStatus === 'onbackorder') { 81 $stockHtml.html('<span class="stock on-backorder" style="color:orange;font-weight:600;">
81Available on backorder</span>'); 82 $('.single_add_to_cart_button').prop('disabled', false).show(); 83 $('.variations_form .quantity').show(); 84 } else { 85 $stockHtml.html('<span class="stock out-of-stock" style="color:red;font-weight:600;">Out of stock</span>'); 86 $('.single_add_to_cart_button').prop('disabled', true).hide(); 87 $('.variations_form .quantity').hide(); 88 } 89 }); 90 91 // Also handle reset variation (show default) 92 $('form.variations_form').on('reset_data', function(){ 93 $('.wlf-location-variation-stock-info').remove(); 94 $('.single_add_to_cart_button').prop('disabled', false).show(); 95 $('.variations_form .quantity').show(); 96 }); 97 98 // === SIMPLE PRODUCT QUANTITY LIMIT === 99 function setSimpleProductQuantityLimit() { 100 var $qtyInput = $('.cart .quantity input.qty'); 101 var locStockQty = $('input[name="wlf_location_simple_stock_qty"]').val(); 102 103 if (typeof locStockQty !== 'undefined' && locStockQty !== '') { 104 var max = parseInt(locStockQty, 10); 105 if (!isNaN(max)) { 106 $qtyInput.attr('max', max); 107 108 // If stock is 1, set min/max/value and disable input 109 if (max === 1) { 110 $qtyInput.val(1).attr('min', 1).attr('step', 1).prop('readonly', true); 111 } else if (max === 0) { 112 $qtyInput.val(0).attr('min', 0).attr('max', 0).prop('readonly', true); 113 $('.single_add_to_cart_button').prop('disabled', true).hide(); 114 } else { 115 $qtyInput.attr('min', 1).prop('readonly', false); 116 $('.single_add_to_cart_button').prop('disabled', false).show(); 117 } 118 } 119 } 120 } 121 122 // Run for simple products 123 setSimpleProductQuantityLimit(); 124 125 // If quantity input changes, enforce limit 126 $(document).on('input', '.cart .quantity input.qty', function() { 127 var max = parseInt($(this).attr('max'), 10); 128 var val = parseInt($(this).val(), 10); 129 if (!isNaN(max) && val > max) { 130 $(this).val(max); 131 } 132 }); 133 134 // === VARIATION PRODUCT QUANTITY LIMIT === 135 $('form.variations_form').on('show_variation', function(event, variation){ 136 var locStockQty = variation.wlf_location_stock_qty; 137 var $qtyInput = $('.variations_form .quantity input.qty'); 138 139 if (typeof locStockQty !== 'undefined' && locStockQty !== '') { 140 var max = parseInt(locStockQty, 10); 141 if (!isNaN(max)) { 142 $qtyInput.attr('max', max); 143 144 if (max === 1) { 145 $qtyInput.val(1).attr('min', 1).attr('step', 1).prop('readonly', true); 146 } else if (max === 0) { 147 $qtyInput.val(0).attr('min', 0).attr('max', 0).prop('readonly', true); 148 $('.single_add_to_cart_button').prop('disabled', true).hide(); 149 } else { 150 $qtyInput.attr('min', 1).prop('readonly', false); 151 $('.single_add_to_cart_button').prop('disabled', false).show(); 152 } 153 } 154 } 155 }); 156 157 // Reset on variation reset 158 $('form.variations_form').on('reset_data', function(){ 159 var $qtyInput = $('.variations_form .quantity input.qty'); 160 $qtyInput.removeAttr('max min step readonly'); 161 $qtyInput.val(1); 162 $('.single_add_to_cart_button').prop('disabled', false).show(); 163 }); 164 165 // Enforce on input change for variations too 166 $(document).on('input', '.variations_form .quantity input.qty', function() { 167 var max = parseInt($(this).attr('max'), 10); 168 var val = parseInt($(this).val(), 10); 169 if (!isNaN(max) && val > max) { 170 $(this).val(max); 171 } 172 }); 173});
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.