1jQuery(function ($) { 2 3 let cuw_data = cuw_template.data; 4 let cuw_i18n = cuw_template.i18n; 5 let cuw_is_rtl = cuw_template.is_rtl; 6 7 /* Offer */ 8 window.cuw_offer = { 9 // init 10 init: function () { 11 this.event_listeners(); 12 if ($("#cuw-ppu-offer").length) { 13 cuw_offer.update_ppu_totals($("#cuw-ppu-offer")); 14 } 15 }, 16 17 // update price when change variant 18 update_price: function (select, section) { 19 let variant = select.find('option:selected'); 20 section.find('.cuw-product-price, .cuw-page-product-price, .cuw-ppu-product-price').html(variant.data('price_html')); 21 section.find('.cuw-ppu-order-totals').data('price', variant.data('price')).data('tax', variant.data('tax')); 22 section.find('.cuw-qty').prop('max', variant.data('stock_qty')); 23 if (variant.data('image')) { 24 section.find('.cuw-product-image, .cuw-page-product-image, .cuw-ppu-product-image').html(variant.data('image')); 25 } 26 }, 27 28 // update the closest quantity input on click press plus or minus button 29 update_quantity: function (button) { 30 let wrapper = button.parent(); 31 let input = wrapper.find('input'), 32 val = parseFloat(input.val()) || 0, 33 max = parseFloat(input.attr('max')) || '', 34 min = parseFloat(input.attr('min')) || 0, 35 step = parseFloat(input.attr('step')) || 1; 36 37 if (button.is('.cuw-plus')) { 38 if (max && (val >= max)) input.val(max); 39 else input.val(val + step); 40 } else { 41 if (min && (val <= min)) input.val(min); 42 else if (val > 0) input.val(val - step); 43 } 44 45 val = parseFloat(input.val()); // to get current value 46 wrapper.find('.cuw-minus').css('opacity', min === val ? 0.6 : 1); 47 wrapper.find('.cuw-plus').css('opacity', max === val ? 0.6 : 1); 48 input.trigger('change'); 49 }, 50 51 // to check inputting quantity 52 check_quantity: function (input) { 53 let product_quantity = parseInt(input.val()); 54 let max_quantity = parseInt(input.prop('max')); 55 if (product_quantity > max_quantity) { 56 input.val(max_quantity); 57 } 58 input.closest('.quantity-input').find('.cuw-plus').css('opacity', product_quantity >= max_quantity ? 0.6 : 1); 59 input.closest('.cuw-offer, .cuw-page, .cuw-product, .cuw-product-row').find('.variant-select').trigger('change'); 60 }, 61 62 update_ppu_totals: function (section) { 63 let qty = section.find(".cuw-ppu-product-quantity .cuw-qty").val(); 64 if (qty === undefined) { 65 qty = section.find(".cuw-ppu-order-totals").data('qty'); 66 } 67 let product_price = section.find(".cuw-ppu-order-totals").data('price'); 68 let tax = section.find(".cuw-ppu-order-totals").data('tax'); 69 let order_total = section.find(".cuw-ppu-order-totals").data('order_total'); 70 let shipping_tax = section.find(".cuw-ppu-order-totals").data('shipping_tax'); 71 72 let subtotal = parseFloat(product_price) * parseFloat(qty); 73 let subtotal_tax = parseFloat(tax) * parseFloat(qty); 74 let updated_order_total = parseFloat(order_total) + subtotal + subtotal_tax + shipping_tax; 75 section.find(".cuw-ppu-subtotal").html(cuw_helper.format_price(subtotal)); 76 section.find(".cuw-ppu-tax").html(cuw_helper.format_price(subtotal_tax + shipping_tax)); 77 section.find(".cuw-ppu-total").html(cuw_helper.format_price(updated_order_total)); 78 }, 79 80 // to load event listeners 81 event_listeners: function () { 82 $(document).on("click", ".cuw-offer .quantity-input .cuw-plus, .cuw-offer .quantity-input .cuw-minus", function () { 83 cuw_offer.update_quantity($(this)); 84 }); 85 86 $(document).on("click", ".cuw-page .quantity-input .cuw-plus, .cuw-page .quantity-input .cuw-minus", function () { 87 cuw_offer.update_quantity($(this)); 88 }); 89 90 $(document).on("change", ".cuw-offer .variant-select", function () { 91 cuw_offer.update_price($(this), $(this).closest('.cuw-offer')); 92 }); 93 94 $(document).on("input", '.cuw-offer .cuw-qty, #cuw-ppu-offer .cuw-qty', function () { 95 cuw_offer.check_quantity($(this)); 96 }); 97 98 $(document).on("change", '#cuw-ppu-offer .cuw-qty', function () { 99 cuw_offer.update_ppu_totals($("#cuw-ppu-offer")); 100 }); 101 102 $(document).on("change", ".cuw-page .variant-select", function () { 103 cuw_offer.update_price($(this), $(this).closest('.cuw-page')); 104 cuw_offer.update_ppu_totals($("#cuw-ppu-offer")); 105 }); 106 }, 107 } 108 109 /* Products */ 110 window.cuw_products = { 111 // init 112 init: function () { 113 this.event_listeners(); 114 this.load_sections(); 115 }, 116 117 // load all available sections 118 load_sections: function () { 119 $(".cuw-products").each(function (index, section) { 120 cuw_products.load_section($(section)); 121 }); 122 }, 123 124 // load specific section 125 load_section: function (section) { 126 section.find('.variant-select').trigger('change'); 127 cuw_products.update_prices(section); 128 }, 129 130 // get all products 131 get_all_products: function (section) { 132 let products = []; 133 section.find('.cuw-product').each(function (index, product) { 134 if ($(product).find(".cuw-product-checkbox").length || $(product).data('key')) { 135 products.push($(product)); 136 } 137 }); 138 return products; 139 }, 140 141 // get chosen products 142 get_chosen_products: function (section) { 143 let products = []; 144 section.find('.cuw-product').each(function (index, product) { 145 if ($(product).find(".cuw-product-checkbox").prop("checked") || $(product).data('key')) { 146 products.push($(product)); 147 } 148 }); 149 return products; 150 }, 151 152 // update modal product prices 153 update_product_row: function (section, product_row) { 154 let price_column = product_row.find('.cuw-product-price'); 155 let product_id = product_row.data('product_id'), variant_id; 156 if (!product_id) { 157 product_id = product_row.data('id'); 158 } 159 160 let product = section.find('.cuw-product[data-id="' + product_id + '"]').first(); 161 let variant_select = product_row.find('.variant-select').first(); 162 if (variant_select && variant_select.length) { 163 variant_id = variant_select.val(); 164 let variant = variant_select.find('option[value="' + variant_id + '"]').first(); 165 product.data('regular_price', variant.data('regular_price')); 166 product.data('price', variant.data('price')); 167 if (product_id) { 168 product.closest('.cuw-products').find('.cuw-product[data-id="' + product_id + '"]').data('regular_price', variant.data('regular_price')); 169 product.closest('.cuw-products').find('.cuw-product[data-id="' + product_id + '"]').data('price', variant.data('price')); 170 } 171 product.find('.cuw-product-variation-id').val(variant_id); 172 product.find('.cuw-product-variation-attributes-json').val(JSON.stringify(variant_select.data('chosen_attributes'))); 173 product_row.find('.cuw-product-variation-id').val(variant_id); 174 product_row.find('.cuw-qty').prop('max', variant.data('stock_qty')); 175 if (variant.data('image')) { 176 if (section.data('change_image') != 'only_row') { 177 product.find('.cuw-product-image').html(variant.data('image')); 178 } 179 product_row.find('.cuw-product-image').html(variant.data('image')); 180 } 181 price_column.html(variant.data('price_html')); 182 //price_column.html(cuw_helper.get_price_html(variant.data('price'), variant.data('regular_price'))); 183 } else if (price_column.html() === '') { 184 price_column.html(product.find('.cuw-product-price').html()); 185 //price_column.html(cuw_helper.get_price_html(product.data('price'), product.data('regular_price'))); 186 } 187 }, 188 189 // update pricing section 190 update_prices: function (section, data = {}) { 191 if (section.closest('.cuw-modal').length > 0) { 192 section = section.closest('.cuw-modal'); 193 } 194 let regular_price_total = 0, price_total = 0, items = 0, main_price = 0, main_item = 0, addons_price = 0, 195 addon_items = 0; 196 let total_items = this.get_all_products(section).length; 197 let main_product_id = parseInt(section.find('[name="main_product_id"]').val());
198 this.get_chosen_products(section).forEach(function (product) { 199 let quantity = 1; 200 if (product.find('.quantity-input .cuw-qty').length > 0) { 201 quantity = parseFloat(product.find('.quantity-input .cuw-qty').first().val()); 202 } else if (product.data('quantity')) { 203 quantity = parseFloat(product.data('quantity')); 204 } 205 if (main_product_id === product.data('id')) { 206 main_price = parseFloat(product.data('price')) * quantity; 207 main_item = 1; 208 } else { 209 addons_price += parseFloat(product.data('price')) * quantity; 210 addon_items++; 211 } 212 regular_price_total += parseFloat(product.data('regular_price')) * quantity; 213 price_total += parseFloat(product.data('price')) * quantity; 214 items++; 215 }); 216 217 let total_savings = regular_price_total - price_total; 218 let at_least_items = parseInt(section.find(".cuw-buy-section").find(".cuw-add-to-cart").data('at_least_items')) || 1; 219 if (items >= at_least_items) { 220 section.find(".cuw-buy-section .cuw-message").hide(); 221 if (section.find(".cuw-buy-section .cuw-actions").first().data('inactive') === 'disable') { 222 section.find(".cuw-buy-section .cuw-actions").show(); 223 section.find(".cuw-buy-section .cuw-actions").find(":input").prop('disabled', false); 224 } else { 225 section.find(".cuw-buy-section .cuw-actions").show(); 226 } 227 section.find(".cuw-buy-section .cuw-prices").show(); 228 229 section.find(".cuw-main-item").html(main_item); 230 section.find(".cuw-addon-items").html(addon_items); 231 section.find(".cuw-total-items").html(items); 232 section.find(".cuw-main-price").html(cuw_helper.get_price_html(main_price)); 233 section.find(".cuw-addons-price").html(cuw_helper.get_price_html(addons_price)); 234 235 if (section.data('main_product_price')) { 236 regular_price_total += parseFloat(section.data('main_product_regular_price')); 237 price_total += parseFloat(section.data('main_product_price')); 238 } 239 240 section.find(".cuw-total-price").html(cuw_helper.get_price_html(price_total, regular_price_total)); 241 if (price_total == 0) { 242 section.find('.cuw-total-price-section').hide(); 243 } else { 244 section.find('.cuw-total-price-section').show(); 245 } 246 } else { 247 if (section.find(".cuw-buy-section .cuw-actions").first().data('inactive') === 'disable') { 248 section.find(".cuw-buy-section .cuw-actions").find(":input").prop('disabled', true); 249 } else { 250 section.find(".cuw-buy-section .cuw-actions").hide(); 251 } 252 section.find(".cuw-buy-section .cuw-prices").hide(); 253 section.find(".cuw-buy-section .cuw-message").show(); 254 } 255 if (total_savings > 0) { 256 section.find('.cuw-total-savings').not('[data-hidden="1"]').show(); 257 section.find('.cuw-total-savings .cuw-saved-amount').html(cuw_helper.get_price_html(total_savings)); 258 } else { 259 section.find('.cuw-total-savings').not('[data-hidden="1"]').hide(); 260 section.find('.cuw-total-savings .cuw-saved-amount').html(''); 261 } 262 if (data.cart_subtotal) { 263 section.find('.cuw-cart-subtotal').html(data.cart_subtotal); 264 } 265 this.update_add_to_cart_text(section, items, total_items); 266 this.update_cta_button(section); 267 268 jQuery(document.body).trigger('cuw_product_prices_updated', [{ 269 regular_price_total: regular_price_total, 270 price_total: price_total, 271 items: items, 272 main_item: main_item, 273 main_price: main_price, 274 addon_items: addon_items, 275 addons_price: addons_price, 276 total_savings: total_savings, 277 available_items: total_items, 278 other_data: data, 279 }, section]); 280 }, 281 282 update_cta_button: function (section) { 283 let disable = false;
284 this.get_chosen_products(section).forEach(function (product) { 285 if (product.hasClass('is_variable')) { 286 if (product.find('.variant-select').val() === '' || product.find('.cuw-product-variation-id').val() === '') { 287 disable = true; 288 return false; // break 289 } 290 } 291 }); 292 section.find('.cuw-add-to-cart').not('[data-choose_variants="1"]').prop('disabled', disable); 293 }, 294 295 // update inputs 296 update_inputs: function (product, show = true) { 297 let inputs = product.find(':input').not('input[type="checkbox"]'); 298 let product_id = product.data('id'); 299 if (show) { 300 inputs.prop("disabled", false); 301 product.css('opacity', '1'); 302 if (product_id) { 303 product.closest('.cuw-products').find('.cuw-product[data-id="' + product_id + '"]').css('opacity', '1'); 304 } 305 product.find('.cuw-plus, .cuw-minus, .cuw-attributes-section').css('pointer-events', 'all'); 306 307 product.find('.cuw-remove-item-from-cart, .cuw-added-text, .cuw-added-icon').hide(); 308 product.find('.cuw-add-product-to-cart').show(); 309 } else { 310 inputs.prop("disabled", true); 311 product.css('opacity', '0.8'); 312 if (product.hasClass('cuw-recommendation-product')) { 313 product.find('.cuw-redirect-to-cart').show(); 314 } 315 if (product_id) { 316 product.closest('.cuw-products').find('.cuw-product[data-id="' + product_id + '"]').css('opacity', '0.8'); 317 } 318 product.find('.cuw-plus, .cuw-minus, .cuw-attributes-section').css('pointer-events', 'none'); 319 320 product.find('.cuw-add-product-to-cart').hide(); 321 product.find('.cuw-remove-item-from-cart, .cuw-added-text, .cuw-added-icon').show(); 322 } 323 }, 324 325 // update add to cart text 326 update_add_to_cart_text: function (section, chosen_items_count, total_items_count) { 327 if (section.find(".cuw-buy-section").find(".cuw-add-to-cart").data('buy_now')) { 328 return; 329 } 330 let text = section.find(".cuw-buy-section").find(".cuw-add-to-cart").data('text'); 331 if (text && !text.includes('{items_text}') && !text.includes('{items_count}')) { 332 text = text; 333 } else if (text && chosen_items_count > 1) { 334 let items_text = ''; 335 let items_count = chosen_items_count + ' ' + cuw_i18n.add_to_cart.items; 336 if (cuw_i18n.add_to_cart.number_to_text[chosen_items_count]) { 337 items_text = cuw_i18n.add_to_cart.number_to_text[chosen_items_count]; 338 } else if (chosen_items_count === total_items_count) { 339 items_text = cuw_i18n.add_to_cart.all_items; 340 } else { 341 items_text = cuw_i18n.add_to_cart.selected_items; 342 } 343 text = text.replace('{items_text}', items_text).replace('{items_count}', items_count); 344 } else { 345 text = cuw_i18n.add_to_cart.text; 346 } 347 section.find(".cuw-add-to-cart").html(text); 348 }, 349 350 // update badges 351 update_badges: function (product) { 352 if (product.find('.cuw-badge').data('hidden') == '1') return; 353 354 let save_text = product.find('.cuw-badge').data('save_text'); 355 let regular_price = product.data('regular_price'); 356 let price = product.data('price'); 357 358 if (regular_price === '' || price === '' || !save_text) return; 359 360 if (regular_price === price) { 361 product.find('.cuw-badge').hide(); 362 } else { 363 let amount = cuw_helper.format_price(regular_price - price); 364 let percentage = cuw_helper.format_percentage((regular_price - price) / regular_price * 100); 365 let text = save_text.replace('{price}', amount).replace('{percentage}', percentage); 366 product.find('.cuw-badge').html(text).show(); 367 } 368 }, 369 370 // update carousel actions 371 carousel_update_actions: function (section) { 372 let slider = section.find(".cuw-carousel-slider"); 373 if (slider[0].scrollLeft === 0) { 374 section.find(".cuw-previous").css('opacity', 0.5); 375 } else if (Math.abs(slider[0].scrollLeft) + slider[0].clientWidth == slider[0].scrollWidth) { 376 section.find(".cuw-next").css('opacity', 0.5); 377 } else { 378 section.find(".cuw-previous, .cuw-next").css('opacity', 1); 379 } 380 }, 381 382 // carousel scroll items 383 carousel_scroll_items: function (section, action) { 384 let slider = section.find(".cuw-carousel-slider"); 385 let item_gap = parseInt(slider.data('gap')) || 18; 386 let item_width = parseInt(slider.find(':first-child').width()); 387 if (action.hasClass('cuw-next')) { 388 slider.animate({ 389 scrollLeft: (cuw_is_rtl ? '-=' : '+=') + (item_width + item_gap), 390 behaviour: 'smooth' 391 }, 10); 392 } else if (action.hasClass('cuw-previous')) { 393 slider.animate({ 394 scrollLeft: (cuw_is_rtl ? '+=' : '-=') + (item_width + item_gap), 395 behaviour: 'smooth' 396 }, 10); 397 } 398 }, 399 400 // to load event listeners 401 event_listeners: function () { 402 $(document).on("click", ".cuw-products .cuw-product-image", function () { 403 let checkbox = $(this).closest(".cuw-product").find(".cuw-product-checkbox").first(); 404 checkbox.prop("checked", !checkbox.prop("checked")).trigger("change"); 405 }); 406 407 $(document).on('click', '.cuw-carousel .cuw-scroll-action', function () { 408 cuw_products.carousel_scroll_items($(this).closest(".cuw-carousel"), $(this)); 409 }); 410 411 $('.cuw-carousel .cuw-carousel-slider').on('scroll', function () { 412 cuw_products.carousel_update_actions($(this).closest(".cuw-carousel")); 413 }); 414 415 $(".single_variation_wrap").on("show_variation", function (event, variation) { 416 $('.cuw-product-addons').each(function (index, section) { 417 $(section).data('main_product_regular_price', variation.display_regular_price); 418 $(section).data('main_product_price', variation.display_price); 419 cuw_products.update_prices($(section)); 420 }); 421 }); 422 423 $(document).on("change", ".cuw-products .cuw-product-checkbox", function () { 424 cuw_products.update_inputs($(this).closest(".cuw-product"), $(this).is(":checked")); 425 cuw_products.update_prices($(this).closest(".cuw-products")); 426 }); 427 428 $(document).on("click", ".cuw-products .quantity-input .cuw-plus, .cuw-products .quantity-input .cuw-minus", function () { 429 cuw_offer.update_quantity($(this)); 430 }); 431 432 $(document).on("change", '.cuw-products .quantity-input .cuw-qty', function () { 433 cuw_products.update_prices($(this).closest(".cuw-products")); 434 }); 435 436 $(document).on("input", '.cuw-products .quantity-input .cuw-qty', function () { 437 cuw_offer.check_quantity($(this)); 438 }); 439 440 $(document).on("change", ".cuw-products .variant-select", function () { 441 cuw_products.update_product_row($(this).closest(".cuw-products"), $(this).closest(".cuw-product-row")); 442 cuw_products.update_prices($(this).closest(".cuw-products")); 443 cuw_products.update_badges($(this).closest(".cuw-product-row")); 444 }); 445 446 $(document).on("cuw_products_load_section", function (event, section) { 447 cuw_products.load_section(section); 448 }); 449 450 $(document).on("click", '.cuw-cart-addon-products .cuw-toggle-ad
450dons', function () { 451 $(this).closest(".cuw-cart-addon-products").find(".cuw-products").slideToggle(); 452 $(this).closest(".cuw-cart-addon-products").find(".cuw-addon-arrow-up").toggle(); 453 $(this).closest(".cuw-cart-addon-products").find(".cuw-addon-arrow-down").toggle(); 454 }); 455 456 $(document).on("click", '.cuw-attributes-select .cuw-reset-attributes', function (event) { 457 event.preventDefault(); 458 $(this).closest('.cuw-attributes-select').find('select').each(function (index, element) { 459 $(element).html($(element).data('attribute_html')); 460 }); 461 462 $(this).closest('.cuw-attributes-select').find('select').trigger('change'); 463 }); 464 465 $(document).on("change", '.cuw-attributes-select select', function () { 466 cuw_variations.update_attributes_fields($(this).closest('.cuw-attributes-select')); 467 468 var chosen_attributes = cuw_variations.get_chosen_attributes($(this).closest('.cuw-attributes-select')); 469 var chosen_attribute_data = cuw_variations.find_matching_variants( 470 $(this).closest('.cuw-attributes-select').data('product_variations'), 471 chosen_attributes.data 472 ); 473 474 var section = $(this).closest('.cuw-attributes-section'); 475 var variant_select = section.find('.variant-select'); 476 if (chosen_attribute_data.length === 1 && chosen_attributes.count === chosen_attributes.chosenCount) { 477 chosen_attribute_data = chosen_attribute_data.shift(); 478 variant_select.val(chosen_attribute_data.id).prop('selected', true); 479 variant_select.data('chosen_attributes', chosen_attributes.data); 480 variant_select.trigger('change'); 481 $(this).closest('.cuw-offer, .cuw-page').find('.cuw-button, .cuw-checkbox, .cuw-page-button, .cuw-ppu-accept-button') 482 .prop('disabled', false) 483 .css({'pointer-events': 'all', 'opacity': '1'}); 484 $(this).closest('#cuw-ppu-offer').find('.cuw-ppu-order-totals-table').css('display', 'table'); 485 $(this).closest('.cuw-product').find('.cuw-add-product-to-cart') 486 .prop('disabled', false) 487 .css({'pointer-events': 'all', 'opacity': '1'}); 488 } else if (chosen_attribute_data.length === 0 && chosen_attributes.count === chosen_attributes.chosenCount) { 489 // fail case, so we can ignore it 490 } else { 491 variant_select.data('chosen_attributes', '[]'); 492 variant_select.val('').prop('selected', true); 493 variant_select.trigger('change'); 494 $(this).closest('.cuw-offer, .cuw-page').find('.cuw-button, .cuw-checkbox, .cuw-page-button, .cuw-ppu-accept-button') 495 .prop('disabled', true) 496 .css({'pointer-events': 'none', 'opacity': '0.8'}); 497 $(this).closest('#cuw-ppu-offer').find('.cuw-ppu-order-totals-table').css('display', 'none'); 498 $(this).closest('.cuw-product').find('.cuw-add-product-to-cart') 499 .prop('disabled', true) 500 .css({'pointer-events': 'none', 'opacity': '0.8'}); 501 } 502 503 section.find('.cuw-reset-attributes').css('display', chosen_attributes.chosenCount > 0 ? 'block' : 'none'); 504 }); 505 }, 506 } 507 508 /* Variation */ 509 window.cuw_variations = { 510
511 update_attributes_fields: function (variation_section) { 512 var product_variations = variation_section.data('product_variations'); 513 var currently_chosen_attributes = cuw_variations.get_chosen_attributes(variation_section).data; 514 515 variation_section.find('select').each(function (index, element) { 516 var current_attribute_select = $(element), 517 current_attribute_name = current_attribute_select.data('attribute_name'), 518 attached_options_count = 0, 519 new_attribute_select = $('<select/>'), 520 option_gt_filter = ':gt(0)', 521 selected_attribute_value = current_attribute_select.val() || '', 522 selected_attribute_value_valid = true; 523 524 if (!current_attribute_select.data('attribute_html')) { 525 var reference_select = current_attribute_select.clone(); 526 reference_select.find('option').removeAttr('attached').prop('disabled', false).prop('selected', false); 527 current_attribute_select.data('attribute_options', reference_select.find('option' + option_gt_filter).get()); 528 current_attribute_select.data('attribute_html', reference_select.html()); 529 } 530 531 new_attribute_select.html(current_attribute_select.data('attribute_html')); 532 533 var check_attributes = $.extend(true, {}, currently_chosen_attributes); 534 check_attributes[current_attribute_name] = ''; 535 var variations_data = cuw_variations.find_matching_variants(product_variations, check_attributes); 536 537 for (var num in variations_data) { 538 if (typeof (variations_data[num]) !== 'undefined') { 539 var variation_attributes = variations_data[num].attributes; 540 for (var attribute_name in variation_attributes) { 541 if (variation_attributes.hasOwnProperty(attribute_name)) { 542 var attribute_value = variation_attributes[attribute_name], variation_active = ''; 543 if (attribute_name === current_attribute_name) { 544 if (variations_data[num].is_active) { 545 variation_active = 'enabled'; 546 } 547 if (attribute_value) { 548 attribute_value = $('<div/>').html(attribute_value).text(); 549 var option_elements = new_attribute_select.find('option'); 550 if (option_elements.length) { 551 for (var i = 0, length = option_elements.length; i < length; i++) { 552 var option_element = $(option_elements[i]), 553 option_value = option_element.val(); 554 if (attribute_value === option_value) { 555 option_element.addClass('attached ' + variation_active); 556 break; 557 } 558 } 559 } 560 } else { 561 new_attribute_select.find('option:gt(0)').addClass('attached ' + variation_active); 562 } 563 } 564 } 565 } 566 } 567 } 568 569 attached_options_count = new_attribute_select.find('option.attached').length; 570 571 if (selected_attribute_value) { 572 selected_attribute_value_valid = false; 573 if (0 !== attached_options_count) { 574 new_attribute_select.find('option.attached.enabled').each(function () { 575 var option_value = $(this).val(); 576 if (selected_attribute_value === option_value) { 577 selected_attribute_value_valid = true; 578 return false;
579 } 580 }); 581 } 582 } 583 584 if (attached_options_count > 0 && selected_attribute_value && selected_attribute_value_valid) { 585 new_attribute_select.find('option:first').remove(); 586 option_gt_filter = ''; 587 } 588 589 new_attribute_select.find('option' + option_gt_filter + ':not(.attached)').remove(); 590 591 current_attribute_select.html(new_attribute_select.html()); 592 current_attribute_select.find('option' + option_gt_filter + ':not(.enabled)').prop('disabled', true); 593 594 if (selected_attribute_value) { 595 if (selected_attribute_value_valid) { 596 current_attribute_select.val(selected_attribute_value); 597 } else { 598 current_attribute_select.val('').trigger('change'); 599 } 600 } else { 601 current_attribute_select.val(''); 602 } 603 }); 604 }, 605 606 find_matching_variants: function (variations_data, attributes) { 607 var matching = []; 608 for (var i = 0; i < variations_data.length; i++) { 609 var variation = variations_data[i]; 610 611 if (cuw_variations.is_attributes_match(variation.attributes, attributes)) { 612 matching.push(variation); 613 } 614 } 615 return matching; 616 }, 617 618 is_attributes_match: function (variation_attributes, attributes) { 619 var match = true; 620 for (var attr_name in variation_attributes) { 621 if (variation_attributes.hasOwnProperty(attr_name)) { 622 var value_1 = variation_attributes[attr_name]; 623 var value_2 = attributes[attr_name]; 624 if (value_1 !== undefined && value_2 !== undefined && value_1.length !== 0 && value_2.length !== 0 && value_1 !== value_2) { 625 match = false; 626 } 627 } 628 } 629 return match; 630 }, 631 632 get_chosen_attributes: function (variation_section) { 633 var data = {}, count = 0, chosen = 0; 634 variation_section.find('select').each(function (index, element) { 635 var attribute_name = $(element).data('attribute_name'); 636 var value = $(element).val() || ''; 637 if (value.length > 0) { 638 chosen++; 639 } 640 count++; 641 data[attribute_name] = value; 642 }); 643 return { 644 'count': count, 645 'chosenCount': chosen, 646 'data': data, 647 }; 648 }, 649 } 650 651 /* Helper */ 652 window.cuw_helper = { 653 // format price 654 format_price: function (price, tag = '') { 655 price = parseFloat(price).toFixed(cuw_data.woocommerce.price.decimals); 656 price = price.replace(".", cuw_data.woocommerce.price.decimal_separator); 657 let split = price.split(cuw_data.woocommerce.price.decimal_separator); 658 split[0] = split[0].replace(/\B(?=(\d{3})+(?!\d))/g, cuw_data.woocommerce.price.thousand_separator); 659 price = split.join(cuw_data.woocommerce.price.decimal_separator); 660 price = cuw_data.woocommerce.price.format.replace('%1$s', cuw_data.woocommerce.price.symbol).replace('%2$s', price); 661 return tag ? '<' + tag + '>' + price + '</' + tag + '>' : price; 662 }, 663 664 // get price html 665 get_price_html: function (price, regular_price = null) { 666 if (regular_price && regular_price > price) { 667 return this.format_price(regular_price, 'del') + ' ' + this.format_price(price, 'ins'); 668 } 669 return this.format_price(price, 'ins'); 670 }, 671 672 // get discount text 673 get_discount_text: function (discount_type, discount_value) { 674 let discount_text = ''; 675 if (discount_type === 'percentage' && discount_value !== '') { 676 discount_text = cuw_helper.format_percentage(discount_value); 677 } else if (discount_type === 'fixed_price' && discount_value !== '') { 678 discount_text = cuw_helper.format_price(discount_value); 679 } else if (discount_type === 'free') { 680 discount_text = cuw_i18n.free; 681 } 682 return discount_text; 683 }, 684 685 // to format discount percentage 686 format_percentage: function (price, round = true) { 687 if (price === '') return price; 688 689 if (round) { 690 price = Math.round(price * 100) / 100; 691 price = price.toFixed(2); 692 price = price.replace(/\.?0+$/, ''); 693 } 694 return price += '%'; 695 }, 696 } 697 698 /* Spinner */ 699 window.cuw_spinner = { 700 // show spinner 701 show: function (section) { 702 if (typeof section === 'string') { 703 section = $(section).first(); 704 } 705 if (section.block) { 706 $(section).block({message: null, overlayCSS: {background: '#fff', opacity: 0.6}}); 707 } 708 }, 709 710 // hide spinner 711 hide: function (section) { 712 if (typeof section === 'string') { 713 section = $(section).first(); 714 } 715 if (section.unblock) { 716 $(section).unblock(); 717 } 718 } 719 } 720 721 /* Model */ 722 window.cuw_modal = { 723 // show modal 724 show: function (modal, temp = false) { 725 if (typeof modal === 'string') { 726 modal = $(modal).first(); 727 } 728 modal.show(); 729 modal.find('.cuw-modal-close').click(function () { 730 temp ? modal.remove() : modal.hide() 731 }); 732 $(window).click(function (event) { 733 if ($(event.target).hasClass('cuw-modal')) { 734 temp ? modal.remove() : modal.hide() 735 } 736 }); 737 $(document).keydown(function (event) { 738 if (event.keyCode === 27) { 739 temp ? modal.remove() : modal.hide() 740 } 741 }); 742 }, 743 744 // hide modal 745 hide: function (modal) { 746 if (typeof modal === 'string') { 747 modal = $(modal).first(); 748 } 749 modal.hide(); 750 } 751 } 752 753 /* Init */ 754 $(document).ready(function () { 755 cuw_offer.init(); 756 cuw_products.init(); 757 }); 758 759});
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.