admin-scripts.js
1 month ago
archive.js
1 year ago
bootstrap.min.js
2 years ago
cart.js
3 years ago
common.js
8 months ago
fontawesome.min.js
4 years ago
jquery.blockUI.js
1 year ago
jquery.magnific-popup.min.js
4 years ago
product.js
1 month ago
underscore-min.js
3 years ago
archive.js
256 lines
| 1 | // JavaScript Document |
| 2 | jQuery(document).ready(function($){ |
| 3 | |
| 4 | |
| 5 | $('body.archive.tax-location').on('change', 'div.slw_stock_location_selection input[name^="slw_add_to_cart_item_stock_location"]', function(){ |
| 6 | var obj = $(this).parents().eq(2).find('.ajax_add_to_cart'); |
| 7 | var qty_wrapper = $(this).parents().eq(2).find('.slw-item-qty-wrapper'); |
| 8 | var stock_location_qty = 0; |
| 9 | |
| 10 | |
| 11 | if(obj.length>0){ |
| 12 | var href = obj.prop('href'); |
| 13 | var url = new URL(href); |
| 14 | var search_params = url.searchParams; |
| 15 | search_params.set('stock-location', $(this).val()); |
| 16 | var qty = 0; |
| 17 | if(qty_wrapper.length>0 && qty_wrapper.find('.slw-item-qty input[name="qty"]').length>0){ |
| 18 | qty = parseInt(qty_wrapper.find('.slw-item-qty input[name="qty"]').val()); |
| 19 | |
| 20 | qty_wrapper.find('.slw-item-qty input[name="qty"]').prop('max', $(this).data('quantity')); |
| 21 | } |
| 22 | if(qty>0){ |
| 23 | |
| 24 | stock_location_qty = parseInt(qty_wrapper.find('.slw-item-qty input[name="qty"]').prop('max')); |
| 25 | |
| 26 | if(qty>stock_location_qty){ |
| 27 | alert(slw_frontend.slw_archive_items_max_msg); |
| 28 | qty = stock_location_qty; |
| 29 | qty_wrapper.find('.slw-item-qty input[name="qty"]').val(qty); |
| 30 | |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | search_params.set('quantity', qty); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | url.search = search_params.toString(); |
| 40 | obj.prop('href', url.toString()); |
| 41 | |
| 42 | } |
| 43 | }); |
| 44 | |
| 45 | $('body.archive.tax-location').on('click', '.ajax_add_to_cart', function(event){ |
| 46 | event.preventDefault(); |
| 47 | |
| 48 | //console.log('AAA'); |
| 49 | |
| 50 | var obj = $(this); |
| 51 | var product_id = obj.data('product_id') || 0; |
| 52 | var variation_id = obj.data('variation_id') || 0; |
| 53 | var location_id = 0; |
| 54 | var location_obj; |
| 55 | var location_qty = 0; |
| 56 | var cart_qty = 0; |
| 57 | var href = obj.prop('href'); |
| 58 | var urlParams = new URLSearchParams(href.split('?')[1] || ''); |
| 59 | var product_variation_id = (variation_id?variation_id:product_id); |
| 60 | |
| 61 | |
| 62 | |
| 63 | if (slw_frontend.wc_slw_pro == true && slw_frontend.show_in_product_page == 'yes_radio') { |
| 64 | location_obj = obj.parents().eq(3).find('input[name^="slw_add_to_cart_item_stock_location[' + product_id + ']"]:checked'); |
| 65 | } else { |
| 66 | location_obj = obj.parents().eq(3).find('select[name^="slw_add_to_cart_item_stock_location[' + product_id + ']"] option:selected'); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | location_id = location_obj?.val() |
| 71 | ?? (typeof slw_frontend.slw_term_id !== 'undefined' ? slw_frontend.slw_term_id : location_id) |
| 72 | ?? (urlParams.has('stock-location') ? parseInt(urlParams.get('stock-location')) : location_id); |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | location_qty = (typeof location_obj!='undefined'?location_obj.data('quantity'):0); |
| 78 | location_qty = (location_qty?location_qty:slw_frontend.stock_quantity?.[product_variation_id]?.[location_id] ?? 0); |
| 79 | |
| 80 | |
| 81 | |
| 82 | cart_qty = slw_get_location_qty(product_id, variation_id, location_id); |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | if (cart_qty && location_qty <= cart_qty) { |
| 88 | //console.log(location_qty+' <= '+cart_qty); |
| 89 | alert(slw_frontend.slw_archive_items_halt_msg); |
| 90 | } else { |
| 91 | |
| 92 | |
| 93 | var quantityInput = obj.parents().eq(1).find('input.qty[name="quantity"]'); |
| 94 | var quantity = 1; |
| 95 | |
| 96 | if (quantityInput.length) { |
| 97 | quantity = parseInt(quantityInput.val()); |
| 98 | href += (href.indexOf('?') !== -1 ? '&' : '?') + 'quantity=' + encodeURIComponent(quantity); |
| 99 | } |
| 100 | |
| 101 | //console.log(href+' - '+location_qty+' <= '+cart_qty); |
| 102 | //return; |
| 103 | |
| 104 | if (cart_qty && quantity > (location_qty-cart_qty)) { |
| 105 | alert(slw_frontend.slw_archive_items_max_msg); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | $.blockUI({ message: '' }); |
| 111 | $.post(href, {}, function () { |
| 112 | $.unblockUI(); |
| 113 | }); |
| 114 | |
| 115 | } |
| 116 | }); |
| 117 | |
| 118 | |
| 119 | $('body.archive.tax-locations').on('click', '.ajax_add_to_cart', function(event){ |
| 120 | event.preventDefault(); |
| 121 | |
| 122 | var obj = $(this); |
| 123 | var product_id = (typeof obj.data('product_id')!='undefined'?obj.data('product_id'):0); |
| 124 | var variation_id = (typeof obj.data('variation_id')!='undefined'?obj.data('variation_id'):0); |
| 125 | var location_obj; |
| 126 | var location_id = 0; |
| 127 | var location_qty = 0; |
| 128 | var cart_qty = 0; |
| 129 | |
| 130 | if(slw_frontend.wc_slw_pro==true && slw_frontend.show_in_product_page=='yes_radio'){ |
| 131 | location_obj = $('input[name^="slw_add_to_cart_item_stock_location['+product_id+']"]:checked'); |
| 132 | }else{ |
| 133 | location_obj = $('select[name^="slw_add_to_cart_item_stock_location['+product_id+']"] option:selected'); |
| 134 | } |
| 135 | |
| 136 | location_id = (typeof location_obj!='undefined'?location_obj.val():0); |
| 137 | location_qty = (typeof location_obj!='undefined'?location_obj.data('quantity'):0); |
| 138 | |
| 139 | if(typeof slw_frontend.slw_cart_items[product_id]!='undefined'){ |
| 140 | if(typeof slw_frontend.slw_cart_items[product_id][variation_id]!='undefined'){ |
| 141 | if(typeof slw_frontend.slw_cart_items[product_id][variation_id][location_id]!='undefined'){ |
| 142 | cart_qty = slw_frontend.slw_cart_items[product_id][variation_id][location_id]; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | //console.log(location_qty+' <= '+cart_qty); |
| 148 | |
| 149 | if(location_qty<=cart_qty){ |
| 150 | alert(slw_frontend.slw_archive_items_halt_msg); |
| 151 | }else{ |
| 152 | $.blockUI({message:''}); |
| 153 | $.post($(this).prop('href'), {}, function(){ |
| 154 | $.unblockUI(); |
| 155 | //document.location.href = slw_frontend.cart_url; |
| 156 | //document.location.reload(); |
| 157 | }); |
| 158 | } |
| 159 | }); |
| 160 | |
| 161 | $('body.archive.tax-location').on('click', 'div.slw-item-qty a', function(){ |
| 162 | var increase = $(this).hasClass('increase'); |
| 163 | var qty = $(this).parent().find('input'); |
| 164 | var qty_val = $.trim(qty.val()); |
| 165 | qty_val = (qty_val>=0?qty_val:0); |
| 166 | |
| 167 | var obj = $(this).parents().eq(3).find('.ajax_add_to_cart'); |
| 168 | var product_id = (typeof obj.data('product_id')!='undefined'?obj.data('product_id'):0); |
| 169 | var variation_id = (typeof obj.data('variation_id')!='undefined'?obj.data('variation_id'):0); |
| 170 | var location_obj; |
| 171 | var location_id = 0; |
| 172 | var location_qty = 0; |
| 173 | var cart_qty = 0; |
| 174 | |
| 175 | var qty_wrapper = $(this).parents().eq(3).find('.slw-item-qty-wrapper'); |
| 176 | |
| 177 | if(slw_frontend.wc_slw_pro==true && slw_frontend.show_in_product_page=='yes_radio'){ |
| 178 | location_obj = $('input[name^="slw_add_to_cart_item_stock_location['+product_id+']"]:checked'); |
| 179 | }else{ |
| 180 | location_obj = $('select[name^="slw_add_to_cart_item_stock_location['+product_id+']"] option:selected'); |
| 181 | } |
| 182 | |
| 183 | location_id = (typeof location_obj!='undefined'?location_obj.val():0); |
| 184 | location_qty = (typeof location_obj!='undefined'?location_obj.data('quantity'):0); |
| 185 | |
| 186 | if(typeof slw_frontend.slw_cart_items[product_id]!='undefined'){ |
| 187 | if(typeof slw_frontend.slw_cart_items[product_id][variation_id]!='undefined'){ |
| 188 | if(typeof slw_frontend.slw_cart_items[product_id][variation_id][location_id]!='undefined'){ |
| 189 | cart_qty = slw_frontend.slw_cart_items[product_id][variation_id][location_id]; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | |
| 195 | if(qty_wrapper.length>0 && qty_wrapper.find('.slw-item-qty input[name="qty"]').length>0){ |
| 196 | qty_wrapper.find('.slw-item-qty input[name="qty"]').prop('max', location_qty); |
| 197 | } |
| 198 | |
| 199 | //console.log(product_id+' - '+variation_id+' - '+location_id+' - '+location_qty+' - '+qty_val+'<='+cart_qty); |
| 200 | if(cart_qty>0 && qty_val<=cart_qty){ |
| 201 | alert(slw_frontend.slw_archive_items_halt_msg); |
| 202 | }else{ |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | if(increase){ |
| 208 | if(qty_val<location_qty){ |
| 209 | qty_val++; |
| 210 | } |
| 211 | }else{ |
| 212 | qty_val--; |
| 213 | } |
| 214 | qty_val = (qty_val>=0?qty_val:0); |
| 215 | qty.val(qty_val); |
| 216 | |
| 217 | |
| 218 | } |
| 219 | |
| 220 | |
| 221 | slw_archive_elements_refresh($(this).parents().eq(3)); |
| 222 | |
| 223 | }); |
| 224 | |
| 225 | function slw_archive_elements_refresh(obj){ |
| 226 | if(slw_frontend.wc_slw_pro==true && slw_frontend.show_in_product_page=='yes_radio'){ |
| 227 | obj.find('input[name^="slw_add_to_cart_item_stock_location"]:checked').change(); |
| 228 | }else{ |
| 229 | obj.find('select[name^="slw_add_to_cart_item_stock_location"] option:selected').change(); |
| 230 | } |
| 231 | } |
| 232 | function slw_get_location_qty(product_id, variation_id = 0, location_id = 0) { |
| 233 | |
| 234 | product_id = parseInt(product_id); |
| 235 | variation_id = parseInt(variation_id); |
| 236 | location_id = parseInt(location_id); |
| 237 | |
| 238 | var proceed = ( |
| 239 | typeof slw_frontend !== 'undefined' && |
| 240 | slw_frontend.slw_cart_items && |
| 241 | slw_frontend.slw_cart_items[product_id] && |
| 242 | typeof slw_frontend.slw_cart_items[product_id] !== 'undefined' && |
| 243 | Array.isArray(slw_frontend.slw_cart_items[product_id]) && |
| 244 | slw_frontend.slw_cart_items[product_id][variation_id] && |
| 245 | slw_frontend.slw_cart_items[product_id][variation_id][location_id] !== 'undefined' |
| 246 | ); |
| 247 | //console.log('proceed: '+proceed); |
| 248 | if (proceed){ |
| 249 | return slw_frontend.slw_cart_items[product_id][variation_id][location_id]; |
| 250 | } |
| 251 | return 0; // fallback if not found |
| 252 | } |
| 253 | |
| 254 | |
| 255 | }); |
| 256 |