1(function ($) { 2 3 window.cart = { 4 5 init: function () { 6 if ($('body').hasClass('cart-dynamic-item-status')) { 7 window.cart.check_quantities($('.store_item.store_item_dynamic_status, .store_item_list_price.store_item_dynamic_status')); 8 } 9 10 window.cart.edit_price($('.store_item .edit_price')); 11 window.cart.add_to_cart($('.store_item .store_item_add_to_cart')); 12 window.cart.add_to_cart($('.store_item_buy_now')); 13 window.cart.remove_from_cart($('.store_item .store_item_remove_from_cart')); 14 window.cart.remove_from_cart_summary_page(); 15 window.cart.change_qty(); 16 window.cart.set_coupon(); 17 window.cart.set_shipping_zone_in_session(); 18 window.cart.set_shipping_option_in_session(); 19 window.cart.proceed_to_payment(); 20 window.cart.cart_summary.init(); 21 window.cart.cart_page_init(); 22 window.cart.revert_quantities(); 23 window.cart.dev_mode_checkbox(); 24 window.cart.cart_validate_qty(); 25 window.cart.cart_terms_checkbox(); 26 window.cart.cart_fields.init(); 27 window.cart.cart_shipping_form_options(); 28 window.cart.errors(); 29 window.cart.set_currency(); 30 31 window.cart.stripe_checkout_payment_intent(); 32 window.cart.checkout_first_step_form(); 33 window.cart.checkout_second_step_form(); 34 window.cart.stripe_payment_request(); 35
36 // Wishlist specififc inits 37 window.cart.wishlist_forms.init(); 38 window.cart.update_wishlist_rows(); 39 40 window.cart.add_to_wishlist($('.add_to_wishlist')); 41 42 window.cart.wishlist_summary.init(); 43 window.cart.remove_from_wishlist($('.wishlist_button .store_item_remove_from_wishlist')); 44 window.cart.remove_from_wishlist_summary_page(); 45 window.cart.after_page_load(); 46 window.cart.enable_framing_option(); 47 }, 48 49 after_page_load: function () { 50 51 var local_storage = window.cart.get_cart_session_from_localstorage() 52 var user_selected_currency = local_storage.currency_code; 53 54 if (user_selected_currency) { 55 var currency_store_select = $('.currency_store_select'); 56 var currency_store_default_currency = currency_store_select.attr('data-cart_currency') && typeof currency_store_select.attr('data-cart_currency') != 'undefined' ? currency_store_select.attr('data-cart_currency') : 'GBP'; 57 58 if (currency_store_select.length) { 59 60 currency_store_select.each(function () { 61 $(this).val(user_selected_currency); 62 }); 63 64 var selected_currency_option = currency_store_select.find(":selected"); 65 var selected_conversion_rate = parseFloat(selected_currency_option.attr('data-conversion_rate')); 66 var selected_currency_name = selected_currency_option.attr('data-currency_name'); 67 var selected_currency_code = selected_currency_option.attr('value'); 68 69 if (selected_currency_code != currency_store_default_currency) { 70 71 $(".store_item").each(function (index) { 72 var include_vat_in_price = $(this).find('input[name=include_vat_in_price]').val(); 73 if (include_vat_in_price == 'True') { 74 var price_in_base_currency = $(this).find('input[name=price]').val(); 75 } else { 76 var price_in_base_currency = $(this).find('input[name=price_without_vat]').val(); 77 } 78 price_in_base_currency = parseFloat(price_in_base_currency); 79 if (typeof selected_conversion_rate == 'number' && !isNaN(selected_conversion_rate) && typeof price_in_base_currency == 'number' && !isNaN(price_in_base_currency)) { 80 var price_in_session_currency = (price_in_base_currency * selected_conversion_rate).toFixed(2); 81 if (selected_currency_name && price_in_session_currency) { 82 $(this).find('.price').text(selected_currency_name + ' ' + price_in_session_currency); 83 $(this).closest('.purchase_options').find('.price:not(.hidden)').eq(index).text(selected_currency_name + ' ' + price_in_session_currency); 84 } 85 } 86 }); 87 88 $(".widget_total_price").each(function (index) { 89 var include_vat_in_price = $(this).attr('data-cart_include_vat_in_price'); 90 if ($(this).attr('data-conversion_rate')) { 91 try { 92 var conversion_rate_object = JSON.parse(JSON.stringify($(this).attr('data-conversion_rate'))); 93 } catch (e) { 94 console.error(e) 95 } 96 } 97 if (conversion_rate_object) { 98 // var conversion_rate_object = JSON.parse(JSON.stringify($(this).attr('data-conversion_rate'))); 99 if (typeof conversion_rate_object[user_selected_currency] != 'undefined') { 100 var conversion_rate = conversion_rate_object[user_selected_currency].conversion_rate;
100 // BUG IT SHOULD BE A DICTIONARY AND CHOSE THE RIGHT AMOUNT 101 102 var count = $(this).attr('data-count'); 103 104 if (include_vat_in_price == 'True') { 105 var total_in_base_currency = $(this).attr('data-total_price_with_vat_in_base_currency') 106 } else { 107 var total_in_base_currency = $(this).attr('data-total_price_without_vat_in_base_currency') 108 } 109 total_in_base_currency = parseFloat(total_in_base_currency); 110 111 var total_in_session_currency = (total_in_base_currency * conversion_rate).toFixed(2); 112 if (total_in_session_currency && total_in_session_currency != 'NaN') { 113 $(this).text(total_in_session_currency); 114 } 115 if (selected_currency_name) { 116 $('.widget_currency').text(selected_currency_name); 117 } 118 119 $('.scw_total_count').text(count); 120 } 121 ; 122 } 123 124 }); 125 } 126 127 } 128 129 } 130 }, 131 132 cart_active: function () { 133 var cart_active = false; 134 if (typeof (Storage) !== "undefined") { 135 if (localStorage.cart_active && typeof localStorage.cart_active != 'undefined') { 136 var cart_active = true; 137 } 138 } 139 return cart_active; 140 }, 141 142 cart_page_init: function () { 143 window.onbeforeunload = function () { 144 } 145 if ($('#sc_checkout_container').length) { 146 $('#sc_checkout_button').unbind('click.alert').bind('click.alert', function () { 147 if ($(this).hasClass('sc_quantities_failed')) { 148 h.alert($('#sc_total_quantity_warning_message').html()); 149 } else if ($(this).hasClass('sc_fields_unconfirmed')) { 150 window.cart.cart_fields.field_change(null, null, true); 151 h.alert('<h2>Sorry</h2>Please fill out all the required fields and ensure you have entered a valid phone number and email address.<br><br>The phone number must only include numbers and dashes.'); 152 } else if ($(this).hasClass('sc_shipping_unconfirmed')) { 153 if ($('#shipping_state:visible').length || $('#shipping_zip:visible').length) { 154 h.alert('<h2>Sorry</h2>Please enter your country, state, a valid ZIP code and shipping method.'); 155 } else { 156 h.alert('<h2>Sorry</h2>Please choose a shipping country and method.'); 157 } 158 } else if ($(this).hasClass('sc_terms_unconfirmed')) { 159 h.alert('<h2>Sorry</h2>Please agree to the terms and conditions.'); 160 } 161 }); 162 if ($('#sc_checkout_content .proceed_to_payment').hasClass('active') || $('#sc_checkout_button').hasClass('active')) { 163 window.cart.enable_checkout_button(); 164 } 165 if (!$('#shipping_container #shipping_container_loader').length) { 166 $('#shipping_container').append('<div id="shipping_container_loader" class="loader_simple hidden"></div>'); 167 } 168 if ($('iframe.stripe_checkout_app').is(':visible')) { 169 // Block user from clicking back in the checkout process... 170 window.history.forward(); 171 } 172 window.onbeforeunload = function () { 173 $('body').addClass('sc-payment-processing'); 174 } 175 } 176 177 }, 178 179 check_quantities: function (elements, success_callback, failure_callback) { 180 added_item_details = {}; 181 // This option only works if .promise() is available in the current version of jQuery 182 if ($.isFunction($.fn.promise)) { 183 $(elements) 184 .each(function () { 185 if ($(this).hasClass('store_item_list_price')) { 186 // List format object 187 var item_uid = $(this).attr('data-cart-uid'); 188 if (item_uid) { 189 added_item_details[item_uid] = {}; 190 added_item_details[item_uid]['key'] = $(this).attr('data-cart-uid'); 191 added_item_details[item_uid]['recid'] = $(this).attr('data-cart-recid'); 192 added_item_details[item_uid]['table_name'] = $(this).attr('data-cart-table_name'); 193 } 194 } else { 195 // Detail page format 196 var item_uid = $(this).attr('data-uid'); 197 if (item_uid) { 198 added_item_details[item_uid] = {}; 199 $('.store_item[data-uid="' + item_uid + '"]').find('input').each(function (data) { 200 added_item_details[item_uid][$(this).attr('name')] = $(this).val(); 201 }); 202 } 203 } 204 }) 205 .promise() 206 .done(function () { 207 var availablity_params = { 208 'items': JSON.stringify(added_item_details) 209 }; 210 if (window.location.search && typeof window.location.search != 'undefined') { 211 if (window.location.search.indexOf('_cmspreview=1') > -1) { 212 availablity_params['_cmspreview'] = '1' 213 } 214 } 215 $.post('/cart/check_availability/', availablity_params, function (data) { 216 if (!$.isEmptyObject(data.items)) { 217 for (item in data.items) { 218 var item_key = item; 219 var item = data.items[item]; 220 if (item.item_unavailable) { 221 // Detail page type 222 $('.store_item[data-uid="' + item_key + '"]').find('> div').not('.sold_out_dynamic').remove(); 223 $('.store_item[data-uid="' + item_key + '"]').find('.sold_out_dynamic').show(); 224 $('.store_item[data-uid="' + item_key + '"]').addClass('store_item_sold_out'); 225 // List page type 226 $('.store_item_list_price[data-cart-uid="' + item_key + '"]').find('> div, > span').not('.sold_out_dynamic').remove(); 227 $('.store_item_list_price[data-cart-uid="' + item_key + '"]').find('.sold_out_dynamic').show(); 228 $('.store_item_list_price[data-cart-uid="' + item_key + '"]').closest('li').removeClass('available'); 229 $('.store_item_list_price[data-cart-uid="' + item_key + '"]').closest('li').addClass('unavailable'); 230 $('.store_item_list_price[data-cart-uid="' + item_key + '"]').addClass('store_item_sold_out'); 231 if (failure_callback && typeof failure_callback != 'undefined') { 232 failure_callback(); 233 } 234 } else { 235 if (success_callback && typeof success_callback != 'undefined') { 236 success_callback(); 237 } 238 } 239 } 240 } else { 241 if (failure_callback && typeof failure_callback != 'undefined') { 242 failure_callback(); 243 } 244 245 } 246 window.cart.check_quantities_after(); 247 }, 'json'); 248 }); 249 ; 250 } 251 252 }, 253 254 check_quantities_after: function () { 255 // Function which is run after the dyanmic quantities are checked 256 }, 257 258 revert_quantities: function () { 259 if ($('#sc_revert_changes').length) { 260 $('#sc_revert_changes a') 261 .click(function () { 262 // For accessibility - tracks which element to refocus on 263 try { 264 h.accessibility.global_variables.element_to_refocus_to = $(this); 265 } catch (error) { 266 console.error(error); 267 } 268 $.post('/cart/revert_quantities/', { 269 'transaction_id': $(this).attr('data-transaction-id') 270 }, function (data) { 271 if (data.success) { 272 h.alert('<h2>Success</h2><div>' + data.reverted_count + ' artwork(s) were reverted succesfully.</div>'); 273 $('#sc_revert_changes_container').hide(); 274 } else { 275 if (data.error) { 276 h.alert('<h2>Sorry</h2><div>An error occurred. Please try again or manually check the artwork status in your inventory.</div>'); 277 } else { 278 h.alert('<h2>Sorry</h2><div>No artworks seem to have reverted. They may have already been reverted OR certain fields may have reverted only - please manually check the artwork status in your inventory ASAP.</div>'); 279 } 280 } 281 }, 'json'); 282 return false;
283 }) 284 ; 285 } 286 287 }, 288 289 dev_mode_checkbox: function () { 290 if ($('#sc_dev_mode_box').length) { 291 if ($('#checkout_first_step').length) { 292 // Checkout 2.0 293 294 if (window.location.href.indexOf('_cmspreview=1') > -1) { 295 if ($('#sc_dev_mode_box #sc_dev_mode').length && !$('#sc_dev_mode_box #sc_dev_mode').is(':checked')) { 296 $('#sc_dev_mode_box #sc_dev_mode').attr('checked', true); 297 } 298 } 299 } else { 300 // Checkout 1.0 301 $('#sc_dev_mode_box #sc_dev_mode') 302 .change(function () { 303 if (window.location.href.indexOf('_cmspreview=1')) { 304 history.replaceState(null, null, window.location.pathname); 305 } 306 var this_value = 0; 307 if ($(this).is(':checked')) { 308 var this_value = 1; 309 } 310 $.post('/cart/set_dev_mode/', { 311 'dev_mode': this_value 312 }, function () { 313 console.log('reload page after change on #sc_dev_mode_box #sc_dev_mode'); 314 window.location.reload(); 315 }); 316 }) 317 ; 318 $('#sc_dev_mode_box #sc_dev_mode_return') 319 .change(function () { 320 var this_value = 0; 321 if ($(this).is(':checked')) { 322 var this_value = 1; 323 } 324 $.post('/cart/set_dev_mode/', { 325 'dev_mode': 1, 326 'checkout_dev_return': this_value 327 }, function () { 328 329 }); 330 }) 331 ; 332 333 if (window.location.href.indexOf('_cmspreview=1') > -1) { 334 console.log('In cmspreview mode!'); 335 if ($('#sc_dev_mode_box #sc_dev_mode').length && !$('#sc_dev_mode_box #sc_dev_mode').is(':checked')) { 336 337 $.post('/cart/set_dev_mode/', { 338 'dev_mode': 1 339 }, function () { 340 console.log('reload page after change when #sc_dev_mode_box #sc_dev_mode exists and #sc_dev_mode_box #sc_dev_mode is not checked'); 341 window.location.reload(); 342 }); 343 } 344 } 345 } 346 } 347 348 }, 349 350 cart_validate_qty: function () { 351 if ($('#sc_total_quantity_limit_box').length) { 352 var min_qty = $('#sc_minimum_quantity').val() && typeof $('#sc_minimum_quantity').val() != 'undefined' ? $('#sc_minimum_quantity').val() : ''; 353 var min_spend = $('#sc_minimum_spend').val() && typeof $('#sc_minimum_spend').val() != 'undefined' ? $('#sc_minimum_spend').val() : ''; 354 $.post('/cart/validate_qty/', {'min_qty': min_qty, 'min_spend': min_spend}, function (obj) { 355 obj = JSON.parse(obj); 356 if (obj.validated) { 357 $('#sc_checkout_button').removeClass('sc_quantities_failed'); 358 $('#sc_total_quantity_limit_box').removeClass('active'); 359 window.cart.stripe_init_button(); 360 } else { 361 $('#sc_checkout_button').addClass('sc_quantities_failed'); 362 $('#sc_total_quantity_limit_box').addClass('active'); 363 if (!$('#sc_total_quantity_limit_box').hasClass('initialised')) { 364 $('#sc_total_quantity_limit_box').addClass('initialised'); 365 h.alert($('#sc_total_quantity_warning_message').html()); 366 } 367 } 368 }); 369 } 370 }, 371 372 cart_shipping_form_options: function () { 373 if ($('#sc_shipping_address_matches_billing').length) { 374 $('#sc_shipping_address_matches_billing').change(function () { 375 if ($(this).is(':checked')) { 376 $('#sc_shipping_address_field_wrapper input[type="text"]').not(':disabled').each(function () { 377 $(this).val(''); 378 }); 379 if (false) { 380 $('#sc_billing_address_field_wrapper input[type="text"], #sc_contact_info_field_wrapper input[type="text"]').not(':disabled').each(function () { 381 var autocomplete_value = typeof $(this).attr('autocomplete') != 'undefined' ? $(this).attr('autocomplete') : false;
382 if (autocomplete_value) { 383 $('#sc_shipping_address_field_wrapper input[autocomplete="' + autocomplete_value + '"]').val($(this).val()); 384 } 385 }); 386 } 387 $('#sc_shipping_address_field_wrapper').hide(); 388 389 $('#_customer_country_billing_fixed').show(); 390 $('#sc_field_country_shipping').hide(); 391 $('#sc_field_country_shipping').val($('#_customer_country_billing_fixed').val()); 392 393 if ($('#_customer_postcode_billing_fixed').length) { 394 $('#_customer_postcode_billing_fixed').show(); 395 $('#sc_field_postcode_shipping').hide(); 396 $('#sc_field_postcode_shipping').val($('#_customer_postcode_billing_fixed').val()); 397 } 398 399 $('#_customer_state_billing_fixed').show(); 400 $('#sc_field_state_shipping').hide(); 401 $('#sc_field_state_shipping').val($('#_customer_state_billing_fixed').val()); 402 403 } else { 404 if (false) { 405 $('#sc_shipping_address_field_wrapper input[type="text"]').not(':disabled').each(function () { 406 $(this).val(''); 407 }); 408 } 409 $('#sc_shipping_address_field_wrapper').show(); 410 411 $('#_customer_country_billing_fixed').hide(); 412 $('#sc_field_country_shipping').show(); 413 414 if ($('#_customer_postcode_billing_fixed').length) { 415 $('#_customer_postcode_billing_fixed').hide(); 416 $('#sc_field_postcode_shipping').show(); 417 } 418 419 $('#_customer_state_billing_fixed').hide(); 420 $('#sc_field_state_shipping').show(); 421 422 } 423 window.cart.cart_fields.init(); 424 }).trigger('change'); 425 } 426 }, 427 428 cart_terms_checkbox: function () { 429 if ($('#sc_terms_box').length) { 430 $('#sc_terms_box #sc_terms_agree') 431 .each(function () { 432 if ($(this).is(':checked')) { 433 $('#sc_checkout_button').removeClass('sc_terms_unconfirmed'); 434 } else { 435 $('#sc_checkout_button').addClass('sc_terms_unconfirmed'); 436 if ($('button.stripe-button-el:not(.stripe_placeholder_button)').length) { 437 $('button.stripe-button-el:not(.stripe_placeholder_button)').attr("disabled", true); 438 } 439 } 440 }) 441 .change(function () { 442 if ($(this).is(':checked')) { 443 $('#sc_checkout_button').removeClass('sc_terms_unconfirmed'); 444 window.cart.enable_checkout_button(); 445 } else { 446 $('#sc_checkout_button').addClass('sc_terms_unconfirmed'); 447 if ($('button.stripe-button-el:not(.stripe_placeholder_button)').length) { 448 $('button.stripe-button-el:not(.stripe_placeholder_button)').attr("disabled", true); 449 } 450 } 451 }) 452 ; 453 $('#sc_terms_box label a[href="#sc_terms_content"]').unbind().click(function () { 454 // For accessibility - tracks which element to refocus on 455 try { 456 h.accessibility.global_variables.element_to_refocus_to = $(this); 457 } catch (error) { 458 console.error(error); 459 } 460 var terms_and_conditions_content = $('#sc_terms_box #sc_terms_content').html(); 461 h.alert(terms_and_conditions_content); 462 return false;
463 }); 464 } 465 466 }, 467 468 cart_fields: { 469 470 init: function () { 471 if ($('#sc_additional_fields').length) { 472 $('#sc_additional_fields .sc_field_row input').unbind() 473 .each(function () { 474 if ($(this).val() == '' && $(this).val() == $(this).attr('default-value')) { 475 $(this).addClass('sc_field_edited') 476 } 477 }) 478 .focus(function () { 479 $(this).closest('.sc_field_row').removeClass('sc_field_error error'); 480 }) 481 .bind('change', function (e) { 482 $(this).addClass('sc_field_edited'); 483 window.cart.cart_fields.field_change(false, $(this)); 484 }) 485 ; 486 window.cart.cart_fields.field_change(true); 487 } 488 }, 489 490 field_change: function (first_load, changed_field, all_fields) { 491 var all_fields = (all_fields && typeof all_fields != 'undefined' ? true : false); 492 var first_load = (first_load && typeof first_load != 'undefined' ? true : false); 493 var changed_field = (changed_field && typeof changed_field != 'undefined' ? changed_field : false); 494 additional_fields_verified = true; 495 additional_fields_errors = []; 496 $('#sc_additional_fields').each(function () { 497 var field_selector = '#sc_additional_fields .sc_field_row input:required'; 498 $(field_selector).each(function () { 499 if ($(this).val() == '' || $(this).val() == $(this).attr('default-value')) { 500 additional_fields_verified = false; 501 if ($(this).hasClass('sc_field_edited') || all_fields) { 502 additional_fields_errors.push($(this).attr('name')); 503 } 504 } else { 505 if ($(this).attr('name') == 'cart_user_email' || $(this).attr('name') == 'customer_email') { 506 if (!window.cart.validate_fields.email($(this).val())) { 507 additional_fields_verified = false; 508 if ($(this).hasClass('sc_field_edited') || all_fields) { 509 additional_fields_errors.push($(this).attr('name')); 510 } 511 } 512 } 513 if ($(this).attr('name') == 'cart_user_phone' || $(this).attr('name') == 'customer_phone') { 514 if (!window.cart.validate_fields.phone($(this).val())) { 515 additional_fields_verified = false; 516 if ($(this).hasClass('sc_field_edited') || all_fields) { 517 additional_fields_errors.push($(this).attr('name')); 518 } 519 } 520 } 521 } 522 }); 523 }) 524 .promise() 525 .done(function () { 526 if (additional_fields_verified && additional_fields_errors.length == 0) { 527 cart_user_email_has_changed = false; 528 $('#sc_checkout_button').removeClass('sc_fields_unconfirmed'); 529 $('#sc_additional_fields .sc_field_row').removeClass('sc_field_error error'); 530 if ($('button.stripe-button-el:not(.stripe_placeholder_button)').length) { 531 $('button.stripe-button-el:not(.stripe_placeholder_button)').attr("disabled", false); 532 } 533 534 $('#stripe_form .sc_additional_field_mirror').remove(); 535 $('#sc_additional_fields .sc_field_row input').each(function () { 536 $('#stripe_form').prepend('<input name="' + $(this).attr('name') + '" type="hidden" class="sc_additional_field_mirror" value="' + $(this).val() + '" />'); 537 if ($(this).attr('name') == 'cart_user_email') { 538 if ($('#stripe_form').attr('data-email') != $(this).val()) { 539 cart_user_email_has_changed = true; 540 $('#stripe_form').attr('data-email', $(this).val()); 541 } 542 } 543 }).promise().done(function () { 544 if (cart_user_email_has_changed) { 545 // Re-initialise the checkout button if the email address has changed, this is so the correct email is fed into the stripe form 546 window.cart.enable_checkout_button(); 547 } 548 }); 549 550 } else { 551 $('#sc_checkout_button').addClass('sc_fields_unconfirmed'); 552 if ($('button.stripe-button-el:not(.str
552ipe_placeholder_button)').length) { 553 $('button.stripe-button-el:not(.stripe_placeholder_button)').attr("disabled", true); 554 } 555 556 $('#sc_additional_fields .sc_field_row').removeClass('sc_field_error error'); 557 if (!first_load) { 558 $.each(additional_fields_errors, function (index, field_name) { 559 var associated_field = $('#sc_additional_fields input[name="' + field_name + '"]'); 560 associated_field.closest('.sc_field_row').addClass('sc_field_error error'); 561 }); 562 } 563 564 } 565 }); 566 } 567 568 }, 569 570 validate_fields: { 571 572 phone: function (number) { 573 if (number == '' || typeof number == 'undefined') { 574 return false; 575 } 576 var filter = /^[- +()]*[0-9][- +()0-9]*$/; 577 var numbers_only = number.replace(/\D/g, ''); 578 var number = number.replace(/\s/g, ''); 579 if (numbers_only.length >= 5) { 580 if (filter.test(number.replace(/ /g, ''))) { 581 return true; 582 } else { 583 return false; 584 } 585 return true; 586 } 587 return false; 588 }, 589 590 email: function (which) { 591 if (which != "" && typeof which != "undefined") { 592 var str = which; 593 var at = "@"; 594 var dot = "."; 595 var lat = str.indexOf(at); 596 var lstr = str.length; 597 var ldot = str.indexOf(dot); 598 599 if (str.indexOf(at) == -1) { 600 return false; 601 } 602 603 if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) { 604 return false; 605 } 606 607 if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr || str.indexOf(dot) == lstr - 1) { 608 return false; 609 } 610 611 if (str.indexOf(at, (lat + 1)) != -1) { 612 return false; 613 } 614 615 if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) { 616 return false; 617 } 618 619 if (str.indexOf(dot, (lat + 2)) == -1) { 620 return false; 621 } 622 623 if (str.indexOf(" ") != -1) { 624 return false; 625 } 626 627 return true; 628 } else { 629 return false; 630 } 631 } 632 633 }, 634 635 errors: function () { 636 $('#sc_checkout_error').each(function () { 637 h.alert($('#sc_checkout_error').html()); 638 }); 639 }, 640 641 test_localstorage: function () { 642 643 if (has_localstorage) { 644 return has_localstorage; 645 } 646 try { 647 localStorage.setItem('_', '_'); 648 localStorage.removeItem('_'); 649 has_localstorage = true; 650 } catch (e) { 651 has_localstorage = false; 652 } 653 return has_localstorage; 654 }, 655 656 edit_price: function (price_field) { 657 price_field.each(function () { 658 var price_field_item = $(this); 659 $(this) 660 .each(function () { 661 $(this).attr('data-last-value', $(this).val()); 662 }) 663 .bind('input paste', function () { 664 // allow only numbers 665 var validChars = /^[0-9]*$|^[0-9][0-9]*\.[0-9]?[0-9]?$/; 666 var strIn = $(this).val(); 667 var i = strIn.length; 668 var strOut = (validChars.test(strIn)) ? strIn : strIn.substring(0, i - 1); 669 $(this).val(strOut); 670 }) 671 .bind('focus', function () { 672 $(this).val(''); 673 }) 674 .bind('blur change', function () { 675 if (!$(this).val() || typeof $(this).val() == 'undefined') { 676 var strOut = $(this).attr('data-last-value'); 677 } else { 678 var strOut = $(this).val(); 679 var strOut = parseFloat(strOut).toFixed(2); 680 if (strOut) { 681 $(this).attr('data-last-value', strOut); 682 } 683 } 684 685 editable_price = strOut; 686 $(this).val(editable_price); 687 688 var added_item_details = {}; 689 item_uid = $(this).closest('.store_item').attr('data-uid'); 690 691 $('.store_item[data-uid="' + item_uid + '"]').find('input').each(function (data) { 692 added_item_details[$(this).attr('name')] = $(this).val(); 693 // console.log( added_item_details[$(this).attr('name')] ) ; 694 }); 695 // console.log(added_item_details); 696 697 $.post('/cart/set_price/', {'item': JSON.stringify(added_item_details)}, function (obj) { 698 if (obj.error) { 699 h.alert(obj.error, {'persistent': false}); 700 } else { 701 if (obj.new_key) { 702 $('.store_item[data-uid="' + item_uid + '"]').find("input[name='key']").val(obj.new_key); 703 $('.store_item[data-uid="' + item_uid + '"]').find("input[name='price']").val(editable_price); 704 $('.store_item[data-uid="' + item_uid + '"]').find("input[name='price_without_vat']").val(editable_price); 705 $('.store_item[data-uid="' + item_uid + '"]').attr('data-uid', obj.new_key); 706 } 707 } 708 }, 'json'); 709 }) 710 ; 711 }); 712 713 }, 714 715 get_wishlist_uids: function () { 716 return JSON.parse(localStorage.getItem('wishlist_uids') || '[]'); 717 }, 718 719 update_wishlist_rows: function () { 720 var uids = this.get_wishlist_uids(); 721 $.each(uids, function (index, uid) { 722 $('.wishlist_button[data-uid="' + uid + '"]').addClass('active'); 723 }); 724 }, 725 726 add_to_wishlist: function (add_button) { 727 728 add_button.unbind().click(function () { 729 // $('body').delegate(add_button.selector, 'click', function() { 730 // $('body').off('click.add_to_wl_'+add_button.selector).on('click.add_to_wl_'+add_button.selector, add_button.selector, function(event) { 731 var element = $(this);//event.target; 732 733 // For accessibility - tracks which element to refocus on 734 try { 735 h.accessibility.global_variables.element_to_refocus_to = element; 736 } catch (error) { 737 console.error(error); 738 } 739 740 add_to_cart_clicked_element = element; 741 742 window.cart.add_to_wishlist_function(); 743 744 return false;
745 }); 746 747 // Check if this item is already active 748 749 }, 750 751 add_to_wishlist_allowed: function (uid) { 752 clicked_element = add_to_cart_clicked_element; 753 console.log('Add to pool - Success '); 754 window.cart.add_to_wishlist_function(); 755 }, 756 757 add_to_wishlist_disallowed: function () { 758 console.log('Add to pool - Failed'); 759 clicked_element = add_to_cart_clicked_element; 760 h.alert('<h2>Sorry</h2><div>This option is currently unavailable. Please try again later.</div>'); 761 }, 762 763 add_to_wishlist_function: function () { 764 clicked_element = add_to_cart_clicked_element; 765 766 if (typeof (Storage) !== "undefined") { 767 localStorage.setItem("wishlist_active", true); 768 localStorage.setItem("wishlist_active_timestamp", new Date().getTime()); 769 } 770 771 772 var added_item_details = {}, 773 $this = $(clicked_element), 774 item_uid = $this.closest('.wishlist_button').attr('data-uid'); 775 776 $('.wishlist_button[data-uid="' + item_uid + '"]').find('input').each(function (data) { 777 added_item_details[$(this).attr('name')] = $(this).val(); 778 }); 779 780 $.post('/cart/add_to_wishlist/', {'item': JSON.stringify(added_item_details)}, function (obj) { 781 if (obj.error) { 782 h.alert(obj.error, {'persistent': false}); 783 } else { 784 785 window.cart.wishlist_summary.get_summary(); 786 window.cart.wishlist_summary.update(obj.total_count, 'add', obj.uid); 787 window.cart.update_wishlist_row(obj.uid, obj.total_count); 788 789 if (window.gtag) { 790 gtag('event', 'add_to_wishlist', {}); 791 } 792 793 if (window.ga) { 794 795 var event_label = 'Unknown'; 796 797 var item_name = $('.wishlist_button[data-uid="' + obj.uid + '"]:eq(0) [name="title"]').val(); 798 var item_url = $('.wishlist_button[data-uid="' + obj.uid + '"]:eq(0) [name="item_url"]').val(); 799 800 var event_label = (item_name && typeof item_name != 'undefined' ? item_name + ' ' : '') + (item_url && typeof item_url != 'undefined' ? '(' + item_url + ')' : ''); 801 802 analytics_params = { 803 'hitType': 'event', 804 'eventCategory': 'Add to wishlist', 805 'eventAction': window.location.pathname, 806 'eventLabel': event_label 807 }; 808 ga('send', analytics_params); 809 ga('tracker2.send', analytics_params); 810 ga('artlogic_tracker.send', analytics_params); 811 } 812 if (typeof window.fbq != 'undefined') { 813 if (window.fbq) { 814 fbq('track', 'AddToWishlist'); 815 } 816 } 817 if (obj.total_count == 1 && $('#wcw_popup').length && !$('#wishlist_quick_cart_widget').length) { 818 // debugger; 819 h.alert($('#wcw_popup').html(), {buttons: fal
819se}); 820 $('.arpromptmessage #wcw_popup_close a').click(function () { 821 $.prompt.close(); 822 return false; 823 }); 824 } 825 } 826 }, 'json'); 827 828 829 }, 830 831 add_to_cart: function (add_button) { 832 833 add_button.unbind('click.add_to_cart').bind('click.add_to_cart', function () { 834 835 add_to_cart_clicked_element = $(this); 836 // For accessibility - tracks which element to refocus on 837 try { 838 h.accessibility.global_variables.element_to_refocus_to = add_to_cart_clicked_element; 839 } catch (error) { 840 console.error(error); 841 } 842 843 window.cart.add_to_cart_function(); 844 845 return false; 846 }); 847 }, 848 849 add_to_cart_allowed: function (uid) { 850 clicked_element = add_to_cart_clicked_element; 851 var clicked_element_context = clicked_element.closest('.store_item.store_item_dynamic_status'); 852 quantities_are_available_callback = function () { 853 window.cart.add_to_cart_function(); 854 } 855 quantities_are_unavailable_callback = function () { 856 h.alert('<h2>Sorry</h2><div>This item is no longer available.</div>'); 857 } 858 window.cart.check_quantities(clicked_element_context, quantities_are_available_callback, quantities_are_unavailable_callback); 859 setTimeout(function () { 860 $(clicked_element).closest('.store_item').removeClass('store_item_adding'); 861 $(clicked_element).removeClass('loading'); 862 }, 700); 863 }, 864 865 add_to_cart_disallowed: function () { 866 clicked_element = add_to_cart_clicked_element; 867 h.alert('<h2>You are in a queue</h2><div><strong>We are experiencing high levels of demand for this item. Please keep trying to add the item to your cart, you may need to wait a minute between attempts.</strong><br><br>Please note items are not reserved until you have completed the full checkout process.</div>'); 868 setTimeout(function () { 869 $(clicked_element).closest('.store_item').removeClass('store_item_adding'); 870 $(clicked_element).removeClass('loading'); 871 }, 700); 872 }, 873 874 add_to_cart_function: function () { 875 clicked_element = add_to_cart_clicked_element; 876 if (typeof (Storage) !== "undefined") { 877 localStorage.setItem("cart_active", true); 878 localStorage.setItem("cart_active_timestamp", new Date().getTime()); 879 } 880 881 var added_item_details = {}, 882 $this = $(clicked_element), 883 item_uid = $this.closest('.store_item').attr('data-uid'); 884 885 if ($(clicked_element).closest('.store_item').hasClass('edit_price_row')) { 886 887 var edited_price = $(clicked_element).closest('.store_item').find('.price input').val(); 888 889 if (edited_price == '' || parseInt(edited_price) == 0) { 890 h.alert('Please enter the correct amount'); 891 } else { 892 893 $('.store_item[data-uid="' + item_uid + '"]').find('input').each(function (data) { 894 added_item_details[$(this).attr('name')] = $(this).val(); 895 }); 896 897 $.post('/cart/add/', {'item': JSON.stringify(added_item_details)}, function (obj) { 898 if (obj.error) { 899 if ($('#scw_popup').length) { 900 window.cart.add_to_cart_popup(obj, obj.error); 901 window.cart.add_to_cart_after(obj.item_count, obj.total_count, obj.sub_total, obj.uid); 902 } else { 903 h.alert(obj.error, {'persistent': false}); 904 } 905 } else { 906 //window.cart.cart_summary.update(obj.total_count, obj.sub_total); 907 window.cart.cart_summary.get_summary(); 908 window.cart.update_store_row(obj.uid, obj.item_count); 909 910 if (window.gtag) { 911 var event_name = $(clicked_element[0]).hasClass('store_item_buy_now') ? 'store_buy_now' : 'store_add_to_cart'; 912 gtag('event', event_name, {}); 913 } 914 915 if (window.ga) { 916 var item_name = $('.store_item[data-uid="' + obj.uid + '"]:eq(0) .title').text(); 917 918 if($(clicked_element[0]).hasClass('store_item_buy_now')){ 919 var categoryText = 'buy now'; 920 var categoryLabel = 'BuyNow';//convert to pascal casing 921 }else{ 922 var categoryText = 'add to cart'; 923 var categoryLabel = 'AddToCart';//convert to pascal casing 924 } 925 926 analytics_params = { 927 'hitType': 'event', 928 'eventCategory': 'Store ' + categoryText, 929 'eventAction': window.location.pathname, 930 'eventLabel': (item_name && typeof item_name != 'undefined' ? item_name : 'unknown') 931 }; 932 ga('send', analytics_params); 933 ga('tracker2.send', analytics_params); 934 ga('artlogic_tracker.send', analytics_params); 935 } 936 if (typeof window.fbq != 'undefined') { 937 if (window.fbq) { 938 fbq('track', categoryLabel); 939 } 940 } 941 window.cart.add_to_cart_popup(obj); 942 window.cart.add_to_cart_after(obj.item_count, obj.total_count, obj.sub_total); 943 } 944 }, 'json'); 945 } 946 } else { 947 948 $('.store_item[data-uid="' + item_uid + '"]').find('input').each(function (data) { 949 added_item_details[$(this).attr('name')] = $(this).val(); 950 }); 951 $.post('/cart/add/', {'item': JSON.stringify(added_item_details)}, function (obj) { 952 953 if (obj.error) { 954 if (obj.errortype == 'max_qty' && $('#store_quick_cart_widget').length && !($('#sc_contents_container').length || $('#sc_checkout_container').length)) { 955 window.cart.cart_summary.quick_cart_widget_open(obj.uid, obj.errortype, obj.error_short); 956 } else { 957 //h.alert(obj.error); 958 window.cart.add_to_cart_popup(obj); 959 window.cart.add_to_cart_after(obj.item_count, obj.total_count, obj.sub_total, obj.uid); 960 } 961 } else { 962 //$.post('/cart/get_widget_total/', function(sub_total){ 963 // window.cart.cart_summary.update(obj.total_count, sub_total.toFixed(2)); 964 //}, 'json'); 965 966 window.cart.cart_summary.get_summary(); 967 // window.cart.cart_summary.update(obj.total_count, obj.sub_total); 968 window.cart.update_store_row(obj.uid, obj.item_count); 969 970 if (window.gtag) { 971 var event_name = $(clicked_element[0]).hasClass('store_item_buy_now') ? 'store_buy_now' : 'store_add_to_cart'; 972 gtag('event', event_name, {}); 973 } 974 975 if (window.ga) { 976 var item_name = $('.store_item[data-uid="' + obj.uid + '"]:eq(0) .title').text(); 977 978 if($(clicked_element[0]).hasClass('store_item_buy_now')){ 979 var categoryText = 'buy now'; 980 var categoryLabel = 'BuyNow';//convert to pascal casing 981 }else{ 982 var categoryText = 'add to cart'; 983 var categoryLabel = 'AddToCart';//convert to pascal casing 984 } 985 986 analytics_params = { 987 'hitType': 'event', 988 'eventCategory': 'Store ' + categoryText, 989 'eventAction': window.location.pathname, 990 'eventLabel': (item_name && typeof item_name != 'undefined' ? item_name : 'unknown') 991 }; 992 ga('send', analytics_params); 993 ga('tracker2.send', analytics_params); 994 ga('artlogic_tracker.send', analytics_params); 995 } 996 if (typeof window.fbq != 'undefined') { 997 if (window.fbq) { 998 fbq('track', categoryLabel); 999 } 1000 } 1001 if($(clicked_element).closest('.store_item').parent().hasClass("store_item_buy_now")) { 1002 window.location.href = "/store/basket/"; 1003 }else{ 1004 window.cart.add_to_cart_popup(obj); 1005 } 1006 window.cart.add_to_cart_after(obj.item_count, obj.total_count, obj.sub_total, obj.uid); 1007 } 1008 }, 'json'); 1009 } 1010 }, 1011 1012 add_to_cart_popup: function (obj, error_message) { 1013 if ($('#scw_popup').length) { 1014 var checkout_link = $('#scw_checkout a.scw_checkout_link').attr('href'); 1015 1016 if ($('#scw_popup').hasClass('scw_popup_skip_to_checkout') && typeof checkout_link != 'undefined') { 1017 // Redirect straight to the cart when an item is added 1018 $('.store_item[data-uid="' + obj.uid + '"]').addClass('store_item_redirecting_to_cart'); 1019 setTimeout(function () { 1020 window.location.href = checkout_link; 1021 }, 200, checkout_link); 1022 } else { 1023 if (obj.total_count == 1 || $('#scw_popup').hasClass('scw_popup_always_show')) { 1024 h.alert($('#scw_popup').html()); 1025 $('.arpromptmessage #scw_popup_close a').click(function () { 1026 $.prompt.close(); 1027 return false;
1028 }); 1029 1030 $('.arpromptmessage #scw_popup_inner h3').show(); 1031 $('.arpromptmessage #scw_popup_inner #scw_popup_error_message').hide(); 1032 if (error_message && typeof error_message != 'undefined') { 1033 $('.arpromptmessage #scw_popup_inner h3').hide(); 1034 if ($('.arpromptmessage #scw_popup_inner #scw_popup_error_message').length == 0) { 1035 $('.arpromptmessage #scw_popup_inner h3').after('<div id="scw_popup_error_message"></div>'); 1036 } 1037 $('.arpromptmessage #scw_popup_inner #scw_popup_error_message').html(error_message.replace('h2>', 'h3>')); 1038 } 1039 $('.arpromptbuttons').remove(); 1040 } 1041 } 1042 } 1043 }, 1044 1045 add_to_cart_after: function (item_count, total_item_count, sub_total, uid) { 1046 1047 }, 1048 1049 remove_from_cart: function (remove_button, reload) { 1050 var item_uid; 1051 remove_button.unbind().click(function () { 1052 if ($(this).closest('.store_item').length > 0) { 1053 item_uid = $(this).closest('.store_item').attr('data-uid'); 1054 parent_row = $(this).closest('.store_item'); 1055 } else if ($(this).closest('.product_detail').length > 0) { 1056 item_uid = $(this).closest('.product_detail').attr('data-uid'); 1057 parent_row = $(this).closest('.product_detail'); 1058 } else { 1059 item_uid = $(this).attr('data-uid'); 1060 parent_row = false; 1061 } 1062 parent_row_delay = $(this).closest('#store_quick_cart_widget').attr('data-remove-delay') ? $(this).closest('#store_quick_cart_widget').attr('data-remove-delay') : false; 1063 1064 $.post('/cart/remove_item/', {'unique_id': item_uid}, function (obj) { 1065 if (reload == 'reload') { 1066 window.location.pathname = window.location.pathname; 1067 window.cart.cart_summary.get_summary(); 1068 } else { 1069 remove_from_cart_after = function () { 1070 window.cart.cart_summary.get_summary(); 1071 window.cart.update_store_row(obj.uid, obj.item_count); 1072 } 1073 if (parent_row && parent_row_delay) { 1074 $(parent_row) 1075 .addClass('hide') 1076 .delay(parent_row_delay) 1077 .queue(function () { 1078 remove_from_cart_after(); 1079 $(this).dequeue(); 1080 }) 1081 ; 1082 } else { 1083 remove_from_cart_after(); 1084 } 1085 } 1086 }, 'json'); 1087 1088 return false; 1089 }); 1090 }, 1091 1092 remove_from_cart_summary_page: function () { 1093 window.cart.remove_from_cart($('#shopping_cart_information .remove'), 'reload'); 1094 }, 1095 1096 update_store_row: function (uid, total_count) { 1097 $('.store_item[data-uid="' + uid + '"]').each(function () { 1098 if (total_count > 0) { 1099 $(this).addClass('active'); 1100 } else { 1101 $(this).removeClass('active'); 1102 } 1103 $('.store_item_remove_container', this).each(function () { 1104 if (total_count > 0) { 1105 $(this).fadeIn(); 1106 } else { 1107 $(this).fadeOut(); 1108 } 1109 }); 1110 $('.store_item_total', this).each(function () { 1111 $('.store_item_total_number', this).html(total_count); 1112 }); 1113 }); 1114 }, 1115 1116 set_cart_session_in_localstorage: function (cart_session) { 1117 var cart_session = cart_session && typeof cart_session != 'undefined' ? cart_session : false;
1118 if (window.cart.test_localstorage() && cart_session) { 1119 localStorage.setItem('cart_session', JSON.stringify(cart_session)); 1120 } 1121 }, 1122 1123 get_cart_session_from_localstorage: function () { 1124 if (window.cart.test_localstorage()) { 1125 var cart_session = localStorage.getItem('cart_session'); 1126 if (cart_session && typeof cart_session != 'undefined') { 1127 return JSON.parse(cart_session); 1128 } else { 1129 return {}; 1130 } 1131 } else { 1132 return {}; 1133 } 1134 }, 1135 1136 cart_summary: { 1137 1138 init: function () { 1139 1140 // If the checkout process has finished, run cleanup 1141 1142 if ($('#sc_confirmation_body').length || $('.sc_confirmation_body').length || $('input[name="sc_confirmation"]').length) { 1143 // Confirmation page functions 1144 if (window.cart.test_localstorage()) { 1145 window.cart.cart_summary.clear_cart_from_localstorage(); 1146 } 1147 1148 if (window.gtag) { 1149 var cart_items = JSON.parse(localStorage.getItem('cart_session')).items; 1150 1151 $.each(cart_items, function (_index, value) { 1152 gtag('event', 'store_purchase', { 1153 'item': value.title, 1154 }); 1155 }); 1156 } 1157 1158 if (window.ga) { 1159 var cart_items = JSON.parse(localStorage.getItem('cart_session')).items; 1160 var label_list = []; 1161 $.each(cart_items, function (index, value) { 1162 label_list.push(value.title); 1163 }); 1164 analytics_params = { 1165 'hitType': 'event', 1166 'eventCategory': 'Store Purchase', 1167 'eventAction': window.location.pathname, 1168 'eventLabel': label_list.join(', ') 1169 }; 1170 ga('send', analytics_params); 1171 ga('tracker2.send', analytics_params); 1172 ga('artlogic_tracker.send', analytics_params); 1173 } 1174 } 1175 1176 // If the widget exists, run the setup 1177 1178 if ($('#store_cart_widget').length > 0 || $('#store_quick_cart_widget').length > 0) { 1179 window.cart.cart_summary.widget_setup(); 1180 window.cart.cart_summary.quick_cart_widget_setup(); 1181 1182 1183 //Standard cart summary check, using ajax calls 1184 window.cart.cart_summary.get_summary($('#store_quick_cart_widget').hasClass('sqcw_initialised') ? false : true); 1185 1186 } 1187 1188 // If new session data has been supplied, add it to localstorage 1189 1190 if ($('input[name="sc_session_data_for_localstorage"]').length) { 1191 var parsed_value = JSON.parse($('input[name="sc_session_data_for_localstorage"]').val()); 1192 if (parsed_value.cart && typeof parsed_value.cart != 'undefined') { 1193 window.cart.set_cart_session_in_localstorage(parsed_value.cart); 1194 } 1195 } 1196 1197 }, 1198 1199 clear_cart_from_localstorage: function () { 1200 if (window.cart.test_localstorage()) { 1201 localStorage.removeItem("cart_active", true); 1202 localStorage.removeItem('cart_summary'); 1203 } 1204 }, 1205 1206 widget_setup: function () { 1207 if ($('#store_cart_widget').hasClass('hide_when_empty')) { 1208 $('#store_cart_widget').hide(); 1209 } 1210 if ($('#store_quick_cart_widget').length > 0) { 1211 $('#store_cart_widget .scw_checkout_link').unbind('click.scw_basket_link').bind('click.scw_basket_link', function () { 1212 // For accessibility - tracks which element to refocus on 1213 try { 1214 h.accessibility.global_variables.element_to_refocus_to = $(this); 1215 } catch (error) { 1216 console.error(error); 1217 } 1218 window.cart.cart_summary.quick_cart_widget_open(); 1219 return false;
1220 }); 1221 } 1222 }, 1223 1224 quick_cart_widget_setup: function () { 1225 if ($('#store_quick_cart_widget').length) { 1226 $('#store_quick_cart_widget #sqcw_close').unbind().click(function () { 1227 window.cart.cart_summary.quick_cart_widget_close(); 1228 }); 1229 } 1230 }, 1231 1232 quick_cart_widget_open: function (item_uid, errortype, errormsg) { 1233 if (!($('#sc_contents_container').length || $('#sc_checkout_container').length)) { 1234 var item_uid = item_uid && typeof item_uid != 'undefined' ? item_uid : false; 1235 var errortype = errortype && typeof errortype != 'undefined' ? errortype : false; 1236 var errormsg = errormsg && typeof errormsg != 'undefined' ? errormsg : ''; 1237 1238 $('#store_quick_cart_widget').addClass('active'); 1239 $('body').addClass('sc_quick_cart_widget_active'); 1240 $('#store_quick_cart_widget').clearQueue().delay(10).queue(function () { 1241 $(this).addClass('animate').dequeue(); 1242 $('body').addClass('sc_quick_cart_widget_animate'); 1243 }); 1244 if (errortype) { 1245 if (errortype == 'max_qty' && item_uid) { 1246 $('#sqcw_items_list li[data-uid="' + item_uid + '"]').addClass('sqcw_max_qty_error'); 1247 $('#sqcw_items_list li[data-uid="' + item_uid + '"] .sqcw_item_error').text(errormsg); 1248 } 1249 } 1250 if ($('#sqcw_empty').length > 0) { 1251 $('#store_quick_cart_widget').addClass('sqcw_no_items'); 1252 } else { 1253 $('#store_quick_cart_widget').removeClass('sqcw_no_items'); 1254 } 1255 window.cart.cart_summary.quick_cart_widget_open_callback(); 1256 } 1257 }, 1258 1259 quick_cart_widget_close: function () { 1260 $('#store_quick_cart_widget').removeClass('animate'); 1261 $('body').removeClass('sc_quick_cart_widget_animate'); 1262 $('#store_quick_cart_widget').clearQueue().delay(600).queue(function () { 1263 $(this).removeClass('active').dequeue(); 1264 $('body').removeClass('sc_quick_cart_widget_active'); 1265 }); 1266 window.cart.cart_summary.quick_cart_widget_close_callback(); 1267 }, 1268 1269 quick_cart_widget_open_callback: function () { 1270 1271 }, 1272 1273 quick_cart_widget_close_callback: function () { 1274 1275 }, 1276 1277 get_summary: function (quick_cart_auto_open, error_info) { 1278 quick_cart_auto_open = (typeof quick_cart_auto_open != 'undefined' ? quick_cart_auto_open : true); 1279 error_info = (typeof error_info != 'undefined' ? error_info : {}); 1280 1281 if ($('#store_cart_widget').length > 0 || $('#store_quick_cart_widget').length > 0) { 1282 $.ajax({ 1283 type: "POST", 1284 url: '/cart/get_cart_info/', 1285 success: function (data) { 1286 var sub_total = data.sub_total.toFixed(2); 1287 var order_total = data.order_total.toFixed(2); 1288 if ($('#store_cart_widget').length) { 1289 window.cart.cart_summary.update(data.count, sub_total, order_total); 1290 } 1291 if ($('#store_quick_cart_widget').length) { 1292 $('#store_quick_cart_widget #sqcw_items').html(data.quick_cart_widget_html); 1293 $('#store_quick_cart_widget .sqcw_total_price .sqcw_total_price_amount').html(sub_total); 1294 $('#store_quick_cart_widget .sqcw_total_price .sqcw_total_price_amount_with_vat').html(order_total); 1295 // Initialise cart functions 1296 window.cart.remove_from_cart($('#sqcw_items .remove'), false); 1297 window.cart.change_qty(); 1298 window.archimedes.archimedes_core.cms_preview.update_links('#store_quick_cart_widget'); 1299 // Open the panel if the auto open setting is enabled 1300 if (quick_cart_auto_open && $('#store_quick_cart_widget').hasClass('sqcw_auto_open') && $('#store_quick_cart_widget').hasClass('sqcw_initialised')) { 1301 if (error_info.errortype) { 1302 window.cart.cart_summary.quick_cart_widget_open(error_info.item_uid, error_info.errortype, error_info.errormsg); 1303 } else { 1304 window.cart.cart_summary.quick_cart_widget_open(); 1305 } 1306 } 1307 // Add the initialised class to show this has finished loading for the first time 1308 $('#store_quick_cart_widget').addClass('sqcw_initialised'); 1309 } 1310 if ($('.store_item .store_item_total').length) { 1311 $('.store_item .store_item_total').closest('.store_item').each(function () { 1312 var store_item_row_uid = $(this).attr('data-uid'); 1313 var item_in_cart_contents = data.cart_contents[store_item_row_uid]; 1314 if (item_in_cart_contents && typeof item_in_cart_contents != 'undefined') { 1315 var item_qty = item_in_cart_contents.qty; 1316 if (item_qty && parseInt(item_qty) > 0) { 1317 window.cart.update_store_row(store_item_row_uid, item_qty); 1318 } 1319 } 1320 }); 1321 } 1322 1323 window.cart.set_cart_session_in_localstorage(data.cart_session); 1324 1325 }, 1326 dataType: 'json' 1327 }); 1328 } 1329 1330 }, 1331 1332 update: function (total_count, sub_total, order_total) { 1333 if ($('#store_cart_widget').length > 0) { 1334 1335 if (total_count < 1) { 1336 $('#store_cart_widget').addClass('empty'); 1337 } else { 1338 $('#store_cart_widget').removeClass('empty'); 1339 var currencyData = $('#store_cart_widget #scw_items a').attr("data-currency"); 1340 if (currencyData && typeof currencyData != 'undefined') { 1341 var currencyForAriaLabel = currencyData 1342 } else { 1343 var currencyForAriaLabel = '' 1344 } 1345 var itemString = ((total_count == 1) ? " item " : " items ") 1346 var updatedAriaLabel = total_count + itemString + "in your basket with a total value of " + currencyForAriaLabel + order_total 1347 $('#store_cart_widget #scw_items a').attr("aria-label", updatedAriaLabel); 1348 } 1349 1350 if ($('#store_cart_widget').hasClass('hide_when_empty') && total_count == 0) { 1351 $('#store_cart_widget').hide(); 1352 $('#store_cart_widget').removeClass('active'); 1353 $('#store_cart_widget').attr('data-item-count', total_count); 1354 } else { 1355 1356 $('#store_cart_widget').removeClass('active'); 1357 $('#store_cart_widget').fadeOut('fast', function () { 1358 $('.scw_total_count').html(total_count); 1359 $('.scw_total_price_amount').html(Number(sub_total).toLocaleStr
1359ing('en', {minimumFractionDigits: 2})); 1360 $('.scw_total_price_amount_with_vat').html(Number(order_total).toLocaleString('en', {minimumFractionDigits: 2})); 1361 $('#store_cart_widget').fadeIn('fast'); 1362 $('#store_cart_widget').addClass('active'); 1363 $('#store_cart_widget').attr('data-item-count', total_count); 1364 window.cart.cart_summary.update_callback(); 1365 }); 1366 } 1367 1368 if ($('#store_cart_widget').hasClass('hide_when_empty')) { 1369 if (total_count < 1) { 1370 $('#scw_checkout').hide(); 1371 $('#store_cart_widget').fadeOut('fast'); 1372 $('#store_cart_widget').removeClass('active'); 1373 } else { 1374 $('#scw_checkout').show(); 1375 $('#store_cart_widget').fadeIn('fast'); 1376 $('#store_cart_widget').addClass('active'); 1377 } 1378 } 1379 1380 window.cart.cart_summary.update_callback(); 1381 } 1382 }, 1383 1384 update_callback: function () { 1385 1386 } 1387 1388 }, 1389 1390 set_coupon: function () { 1391 $('#cart_set_coupon_button a').unbind().click(function () { 1392 1393 // For accessibility - tracks which element to refocus on 1394 try { 1395 h.accessibility.global_variables.element_to_refocus_to = $(this); 1396 } catch (error) { 1397 console.error(error); 1398 } 1399 1400 var coupon_field = $('#cart_coupon_id').val(); 1401 $.post('/cart/set_coupon/', { 1402 'coupon_id': coupon_field 1403 }, function (response) { 1404 var response = $.parseJSON(response); 1405 window.cart.update_order_total($('.order_total')); 1406 if (response.discount > 0) { 1407 $('#sc_discount').show(); 1408 $('#sc_discount_inc_tax').show(); 1409 $('#sc_coupon_container').addClass('active'); 1410 } else { 1411 $('#sc_discount').hide(); 1412 $('#sc_discount_inc_tax').hide(); 1413 $('#sc_coupon_container').removeClass('active'); 1414 } 1415 $('.discount_amount').html(response.discount.toFixed(2)); 1416 $('.discount_amount_inc_tax').html(response.discount_inc_vat.toFixed(2)); 1417 if (response.success) { 1418 h.alert('<h2>Thanks</h2> Your coupon has been added.'); 1419 } else { 1420 h.alert('<h2>Sorry</h2> That coupon is not valid.'); 1421 } 1422 $('#arpromptbox').addClass('couponprompt'); 1423 window.cart.update_total_vat_amount(); 1424 }); 1425 return false; 1426 }); 1427 }, 1428 1429 update_discount: function () { 1430 1431 var coupon_field = $('#cart_coupon_id').val(); 1432 $.post('/cart/get_total_discount/', { 1433 'coupon_id': coupon_field 1434 }, function (response) { 1435 var response = $.parseJSON(response); 1436 if (response.discount > 0) { 1437 $('#sc_discount').show(); 1438 $('#sc_discount_inc_tax').show(); 1439 $('#sc_coupon_container').addClass('active'); 1440 } else { 1441 $('#sc_discount').hide(); 1442 $('#sc_discount_inc_tax').hide(); 1443 $('#sc_coupon_container').removeClass('active'); 1444 } 1445 $('.discount_amount').html(response.discount.toFixed(2)); 1446 $('.discount_amount_inc_tax').html(response.discount_inc_vat.toFixed(2)); 1447 window.cart.update_total_vat_amount(); 1448 }); 1449 1450 }, 1451 1452 set_shipping_zone_in_session: function () { 1453 $('select#shipping_zones').change(function () { 1454 1455 if ($(this).val() == '') { 1456 $(this).removeClass('active'); 1457 } else { 1458 $(this).addClass('active'); 1459 } 1460 zone_prefilled = $(this).hasClass('prefilled'); 1461 1462 $('#sc_total_price_container').removeClass('active'); 1463 $('.proceed_to_payment').removeClass('active'); 1464 $('#sc_checkout_button').addClass('sc_shipping_unconfirmed'); 1465 $('#shipping_container').addClass('shipping_options_loading');
1466 if ($('button.stripe-button-el:not(.stripe_placeholder_button)').length) { 1467 $('button.stripe-button-el:not(.stripe_placeholder_button)').attr("disabled", true); 1468 } 1469 1470 var shipping_country_id = $(this).val(); 1471 1472 var shipping_state_id = 0; 1473 if(shipping_country_id == 227 || shipping_country_id == 36){ 1474 $("#shipping_state_container").show(); 1475 }else{ 1476 $("#shipping_state_container").hide(); 1477 } 1478 if($("#shipping_state").val() != ''){ 1479 shipping_state_id = $("#shipping_state").val(); 1480 } 1481 1482 if ($('#sc_checkout_container').data('sales-tax-api-enabled')) { 1483 window.cart.update_shipping(shipping_country_id); 1484 } else { 1485 $.post('/cart/set_shipping_zone/', { 1486 'shipping_zone_id': $(this).val(), 1487 'shipping_country_id': $(this).val(), 1488 'use_countries': $('#use_countries').val(), 1489 'no_reset': (zone_prefilled ? '1' : ''), 1490 'shipping_state_id': shipping_state_id 1491 }, function (data) { 1492 $('#shipping_container').removeClass('shipping_options_loading'); 1493 window.cart.update_shipping_cost($('.shipping_cost')); 1494 window.cart.update_shipping_cost_inc_vat($('.shipping_cost_inc_vat')); 1495 window.cart.get_shipping_methods(); 1496 window.cart.update_total_vat_amount(); 1497 window.cart.update_order_total($('.order_total')); 1498 1499 data = jQuery.parseJSON(data); 1500 1501 if (!data.error) { 1502 window.cart.show_shipping_states(data); 1503 } 1504 }); 1505 } 1506 setTimeout(function () { 1507 $(this).removeClass('prefilled'); 1508 }, 400); 1509 }); 1510 $('select#shipping_state').change(function () { 1511 $('select#shipping_zones').trigger('change'); 1512 }); 1513 $('select#shipping_zones.prefilled').trigger('change'); 1514 1515 $('input#shipping_zip').change(function () { 1516 $('select#shipping_zones').trigger('change'); 1517 }); 1518 $('input#shipping_zip.prefilled').trigger('change'); 1519 1520 //$('select#shipping_zones option[value!=""]:eq(0)').each(function() { 1521 // $(this).attr('selected', 'selected'); 1522 //}); 1523 //$('select#shipping_zones').triggerHandler('change') 1524 }, 1525 1526 show_shipping_states: function (data) { 1527 /* update_shipping_states */ 1528 var shipping_country_id = $("#shipping_zones").val(); 1529 var shipping_states_container_el = $('#shipping_state_container'); 1530 var shipping_zip_container_el = $('#shipping_zip_container'); 1531 var shipping_states_el = $("select#shipping_state"); 1532 1533 if ((!isFinite(parseInt(shipping_country_id)) && shipping_country_id != '') 1534 || parseInt(shipping_country_id) == 0) { 1535 // Hide the shipping state field when no country is selected; 1536 // and remove the state options to allow validate_shipping_info 1537 // to work properly. 1538 $(shipping_states_el).empty(); 1539 $(shipping_states_el).append($("<option></option>") 1540 .attr("value", "0") 1541 .prop("selected", true)); 1542 $(shipping_states_container_el).add(shipping_zip_container_el).css('display', 'none'); 1543 } else if (shipping_country_id != '') { 1544 const current_shipping_state = $(shipping_states_el).val(); 1545 const states = data.shipping_states; 1546 const current_state = parseInt(current_shipping_state || data.shipping_state); 1547 $(shipping_states_el).empty(); 1548 $(shipping_states_el).append($("<option></option>") 1549 .attr("value", "0") 1550 .prop("selected", true)); 1551 $.each(states, function (i, state) { 1552 $(shipping_states_el).append($("<option></option>") 1553 .attr("value", state.id) 1554 .prop("selected", parseInt(state.id) == current_state) 1555 .text(state.name)); 1556 }); 1557 $(shipping_states_container_el).add(shipping_zip_container_el).css('display', states.length === 0 ? 'none' : 'inherit'); 1558 } 1559 1560 if (data.shipping_state_label) { 1561 $(shipping_states_container_el).find('#shipping_state_label') 1562 .text(data.shipping_state_label); 1563 } 1564 }, 1565 1566 update_shipping: function () { 1567 $('#shipping_container').addClass('shipping_options_loading'); 1568 $('#shipping_options_container').addClass('loading'); 1569 1570 var shipping_country_id = $("#shipping_zones").val(); 1571 // var shipping_option_id = shipping_country_id != '0' ? $("#shipping_options").val() : '0'; 1572 const requestData = { 1573 'shipping_zone_id': shipping_country_id, 1574 'shipping_country_id': shipping_country_id, 1575 'shipping_state_id': $("#shipping_state").val(), 1576 'shipping_option_id': $("#shipping_options").val(), 1577 'shipping_zip': $("#shipping_zip").val(), 1578 'no_reset': (zone_prefilled ? '1' : ''), 1579 'use_countries': $('#use_countries').val() 1580 }; 1581 $.post('/cart/update_shipping/', requestData, function (data) { 1582 // console.log(data); 1583 if (data.error) { 1584 h.alert('<h2>Sorry</h2><p>An unknown server error occurred while updating shipping options.
1584</p>'); 1585 } 1586 1587 if (data.shipping_error === 'invalid_zip') { 1588 $('input#shipping_zip').attr("data-error", "true"); 1589 } else { 1590 $('input#shipping_zip').attr("data-error", "false"); 1591 } 1592 1593 /* update_shipping_cost */ 1594 var shipping_cost_el = $('#sc_shipping_total .shipping_cost'); 1595 shipping_cost_el.html(Number(data.shipping_cost) 1596 .toLocaleString('en', {minimumFractionDigits: 2})); 1597 1598 /* update_shipping_cost_inc_vat */ 1599 var shipping_cost_inc_vat_el = $('.shipping_cost_inc_vat'); 1600 shipping_cost_inc_vat_el.html(Number(data.shipping_cost_inc_vat) 1601 .toLocaleString('en', {minimumFractionDigits: 2})); 1602 1603 /* get_shipping_methods */ 1604 // TODO: on the server side, it makes more sense to return data in 1605 // a list like the shipping states rather than as HTML, but 1606 // it will need to be a new function so as not to break 1607 // backwards compatibility. 1608 var shipping_zone_id = $('select#shipping_zones').val(); 1609 var shipping_option_id = parseInt($('select#shipping_options').val()); 1610 $('#shipping_options_container #shipping_options') 1611 .attr('data-default-label', $('#shipping_options_container #shipping_options option[value=""]').text()); 1612 $('#shipping_options_container #shipping_options') 1613 .html(data.shipping_methods); 1614 $('#shipping_options_container #shipping_options option[value=""]') 1615 .text($('#shipping_options_container #shipping_options').attr('data-default-label')); 1616 1617 if (isFinite(shipping_option_id)) { 1618 $('#shipping_options_container #shipping_options').val(shipping_option_id); 1619 } else { 1620 $('#shipping_options_container #shipping_options').val(data.shipping_option); 1621 } 1622 1623 if (shipping_zone_id && shipping_zone_id != '0') { 1624 $('#shipping_options_container').addClass('active'); 1625 } else { 1626 $('#shipping_options_container').removeClass('active'); 1627 } 1628 1629 /* update_total_vat_amount */ 1630 $('.total_vat_amount').html(Number(data.total_vat_amount).toLocaleString('en', {minimumFractionDigits: 2})); 1631 /* update_vate_rate */ 1632 if (($('.vate_rate').length && typeof data.vat_rate_base != 'undefined' && data.vat_rate_base != 0)|| 1633 (data.taxjar_enabled && data.shipping_country == 227)) { 1634 $('#sc_vat_rate').show(); 1635 $('.vate_rate').html(Number(data.vat_rate_base).toLocaleString('en', {minimumFractionDigits: 2})); 1636 }else{ 1637 $('#sc_vat_rate').hide(); 1638 1639 } 1640 1641 /* update_order_total */ 1642 var order_total_el = $('.order_total'); 1643 order_total_el.each(function () { 1644 $(this).html(Number(data.order_total).toLocaleString('en', {minimumFractionDigits: 2})); 1645 }); 1646 1647 /* update_shipping_states */ 1648 window.cart.show_shipping_states(data); 1649 1650 window.cart.validate_shipping_info(); 1651 $('#shipping_options_container').removeClass('loading'); 1652 $('#shipping_container').removeClass('shipping_options_loading'); 1653 }); 1654 }, 1655 1656 validate_shipping_info: function () { 1657 var shipping_zone = parseInt($('select#shipping_zones').val()); 1658 var shipping_state = parseInt($('select#shipping_state').val()); 1659 var shipping_option = parseInt($('select#shipping_options').val()); 1660 var shipping_option_not_required = false; 1661 1662 var zone_valid = isFinite(shipping_zone) && shipping_zone != 0; 1663 var state_valid = true; 1664 1665 if ($('select#shipping_state>option').length > 1) { 1666 state_valid = isFinite(shipping_state) && shipping_state != 0; 1667 } 1668 1669 if (shipping_option === 0) { 1670 var shipping_options = $('select#shipping_options'); 1671 var all_options = $('select#shipping_options').length ? shipping_options[0].options : []; 1672 1673 if (all_options.length) { 1674 var selected_option = all_options[all_options.selectedIndex]; 1675 1676 if (selected_option && parseInt(selected_option.dataset.notRequired) === 1) { 1677 shipping_option_not_required = true; 1678 } 1679 } 1680 } 1681 1682 var method_valid = shipping_option_not_required || (isFinite(shipping_option) && shipping_option != 0); 1683 1684 if (shipping_zone == 227) { 1685 var zip_regex = /^\d{5}
1685(-\d{4})?$/; 1686 var zip_validated = zip_regex.test($('input#shipping_zip').val()); 1687 } else { 1688 var zip_validated = true; 1689 } 1690 var zip_valid = ( 1691 ( 1692 $('input#shipping_zip').is(':visible') 1693 && 1694 $('input#shipping_zip').val() != '' 1695 && 1696 zip_validated 1697 && 1698 $('input#shipping_zip').attr("data-error") !== 'true' 1699 ) 1700 || 1701 $('input#shipping_zip').length == 0 1702 || 1703 $('input#shipping_zip').is(':hidden') 1704 ); 1705 1706 if (zip_valid && zone_valid && state_valid && method_valid && ($('#sc_terms_agree').length === 0 || $('#sc_terms_agree').is(':checked'))) { 1707 // console.log("SHIPPING VALID"); 1708 $('#sc_checkout_content .proceed_to_payment').addClass('active'); 1709 $('#sc_checkout_content #sc_checkout_button').removeClass('sc_shipping_unconfirmed'); 1710 } else { 1711 // console.log("SHIPPING INVALID"); 1712 $('#sc_checkout_content .proceed_to_payment').removeClass('active'); 1713 $('#sc_checkout_content #sc_checkout_button').addClass('sc_shipping_unconfirmed'); 1714 } 1715 }, 1716 1717 get_shipping_methods: function () { 1718 var shipping_zone_id = $('select#shipping_zones').val(); 1719 1720 var shipping_state_id = 0; 1721 $('#shipping_container').addClass('shipping_options_loading'); 1722 $('#shipping_options_container').addClass('loading'); 1723 if($("#shipping_state").val() != ''){ 1724 shipping_state_id = $("#shipping_state").val(); 1725 } 1726 $.post('/cart/get_shipping_methods/', { 1727 'shipping_zone_id': shipping_zone_id, 1728 'shipping_state_id': shipping_state_id, 1729 'use_countries': $('#use_countries').val() 1730 }, function (data) { 1731 1732 shipping_options_error = $('#shipping_options_container #shipping_options option[value=""][data-unavailable-option="1"]').length == 1; 1733 1734 $('#shipping_container').removeClass('shipping_options_loading'); 1735 $('#shipping_options_container').removeClass('loading'); 1736 if (!shipping_options_error) { 1737 $('#shipping_options_container #shipping_options').attr('data-default-label', $('#shipping_options_container #shipping_options option[value=""]').text()); 1738 } 1739 $('#shipping_options_container #shipping_options').html(data); 1740 1741 1742 shipping_options_error = $('#shipping_options_container #shipping_options option[value=""][data-unavailable-option="1"]').length == 1; 1743 1744 if (!shipping_options_error) { 1745 $('#shipping_options_container #shipping_options option[value=""]').text($('#shipping_options_container #shipping_options').attr('data-default-label')); 1746 } 1747 window.cart.set_shipping_option_in_session(); 1748 $('#shipping_options').trigger('change'); 1749 1750 if (shipping_zone_id && parseInt(shipping_zone_id)) { 1751 $('#shipping_options_container').addClass('active'); 1752 } else { 1753 $('#shipping_options_container').removeClass('active'); 1754 } 1755 1756 if (shipping_options_error) { 1757 if ($('#shipping_options_container #shipping_options option[value=""][data-unavailable-option="1"]').attr('data-error-type') == 'no_country_item_type_matches') { 1758 var shipping_item_labels = 'Unknown'; 1759 var item_label_attribute = $('#shipping_options_container #shipping_options option[value=""][data-unavailable-option="1"]').attr('data-unavailable-product-labels'); 1760 if (typeof item_label_attribute != 'undefined' && item_label_attribute != '') { 1761 var shipping_item_labels = item_label_attribute; 1762 } 1763 h.alert('<h2>Sorry</h2> Shipping is not available to your selected country for the following item types: ' + shipping_item_labels + '.<br><br>Please remove these items to continue, or contact us for assistance.'); 1764 } else { 1765 h.alert('<h2>Sorry</h2> We could not calculate shipping for the combination of items in your cart. Please purchase each item individually or contact us for assistance.'); 1766 } 1767 } 1768 1769 }, 'html'); 1770 1771 }, 1772 1773 set_shipping_option_in_session: function () { 1774 $('select#shipping_options').unbind().change(function () { 1775 if ($('#sc_checkout_container').data('sales-tax-api-enabled')) { 1776 window.cart.update_shipping(); 1777 } else { 1778 if ($(this).val()) { 1779 $(this).addClass('active'); 1780 $('#shipping_container').addClass('shipping_options_loading'); 1781 $.post('/cart/set_shipping_option/', {'shipping_option_id': $(this).val()}, function () { 1782 $('#shipping_container').removeClass('shipping_options_loading'); 1783 window.cart.update_shipping_cost($('.shipping_cost')); 1784 window.cart.update_shipping_cost_inc_vat($('.shipping_cost_inc_vat')); 1785 window.cart.update_order_total($('.order_total')); 1786 window.cart.update_total_vat_amount(); 1787 $('#sc_total_price_container').addClass('active'); 1788 window.cart.enable_checkout_button(); 1789 }); 1790 } else { 1791 $(this).removeClass('active'); 1792 window.cart.disable_checkout_button(); 1793 } 1794 } 1795 }); 1796 }, 1797 1798 set_qty: function (uid, obj, option) { 1799 var current_value = ''; 1800 var new_value = 1; 1801 current_value = obj.text(); 1802 if (option == '-') { 1803 if (parseInt(current_value) > 1) { 1804 new_value = parseInt(current_value) - 1; 1805 } else { 1806 new_value = 1 1807 } 1808 } else if (option == '+') { 1809 new_value = parseInt(current_value) + 1; 1810 if (parseInt(new_value) > parseInt(obj.closest('.sc_cell_quantity').attr('data-max-qty'))) { 1811 new_value = parseInt(current_value); 1812 //h.alert('Sorry - you have added the maximum quantity of this item.'); 1813 if ($('#scw_popup').length) { 1814 h.alert($('#scw_popup').html(), {buttons: fal
1814se}); 1815 $('.arpromptmessage #scw_popup_close a').click(function () { 1816 $.prompt.close(); 1817 return false; 1818 }); 1819 } 1820 } 1821 } 1822 if (new_value == 1) { 1823 obj.closest('.product_detail').find('.decrease_qty').addClass('disabled').attr('aria-disabled', true); 1824 } else { 1825 obj.closest('.product_detail').find('.decrease_qty').removeClass('disabled').removeAttr('aria-disabled'); 1826 } 1827 obj.text(new_value); 1828 }, 1829 1830 change_qty: function () { 1831 1832 /* Increase or decrease the quantity input field value */ 1833 var uid, $tr; 1834 $('.increase_qty').unbind().click(function () { 1835 // For accessibility - tracks which element to refocus on 1836 try { 1837 h.accessibility.global_variables.element_to_refocus_to = $(this); 1838 } catch (error) { 1839 console.error(error); 1840 } 1841 if (!$(this).hasClass('disabled')) { 1842 uid = $(this).closest('.product_detail').attr('data-uid'); 1843 $tr = $(this).closest('.product_detail').find('.qty'); 1844 var new_qty = parseInt($tr.text()) + 1; 1845 window.cart.update_qty_in_session(uid, new_qty, $tr, '+'); 1846 $('.product_detail[data-uid="' + uid + '"]').find('.decrease_qty').removeClass('disabled'); 1847 } 1848 return false; 1849 }); 1850 1851 $('.decrease_qty').unbind().click(function () { 1852 // For accessibility - tracks which element to refocus on 1853 try { 1854 h.accessibility.global_variables.element_to_refocus_to = $(this); 1855 } catch (error) { 1856 console.error(error); 1857 } 1858 if (!$(this).hasClass('disabled')) { 1859 uid = $(this).closest('.product_detail').attr('data-uid'); 1860 $tr = $(this).closest('.product_detail').find('.qty'); 1861 var new_qty = parseInt($tr.text()) - 1; 1862 window.cart.update_qty_in_session(uid, new_qty, $tr, '-'); 1863 if ($tr.html() == '1') { 1864 $('.product_detail[data-uid="' + uid + '"]').find('.decrease_qty').addClass('disabled').attr('aria-disabled', true); 1865 } 1866 } 1867 return false; 1868 }); 1869 }, 1870 1871 update_qty_in_session: function (uid, qty_value, tr, direction) { 1872 $.post('/cart/qty/', {'unique_id': uid, 'qty_value': qty_value}, function (obj) { 1873 if (obj.error) { 1874 h.alert(obj.error, {'persistent': false}); 1875 } else { 1876 window.cart.set_qty(uid, tr, direction); 1877 window.cart.update_sub_total($('.sub_total')); 1878 window.cart.update_order_total($('.order_total')); 1879 window.cart.update_shipping_cost($('.shipping_cost')); 1880 window.cart.update_shipping_cost_inc_vat($('.shipping_cost')); 1881 window.cart.update_item_total_price(uid); 1882 window.cart.update_total_weight($('.total_weight')); 1883 window.cart.update_total_vat_amount(); 1884 window.cart.cart_summary.init(); 1885 window.cart.cart_summary.get_summary(); 1886 window.cart.update_discount(); 1887 window.cart.cart_validate_qty(); 1888 return true; 1889 } 1890 }, 'json'); 1891 }, 1892 1893 1894 update_sub_total: function (sub_total_el) { 1895 $.post('/cart/get_sub_total/', function (data) { 1896 sub_total_el.html(Number(data).toLocaleString('en', {minimumFractionDigits: 2})); 1897 }, 'html'); 1898 }, 1899 1900 update_total_vat_amount: function () { 1901 $('#shipping_container').addClass('shipping_options_loading'); 1902 $.post('/cart/get_total_vat_amount/', function (data) { 1903 $('.total_vat_amount').html(Number(data).toLocaleStr
1903ing('en', {minimumFractionDigits: 2})); 1904 $('#shipping_container').removeClass('shipping_options_loading'); 1905 }, 'html'); 1906 }, 1907 1908 update_order_total: function (order_total_el) { 1909 $('#shipping_container').addClass('shipping_options_loading'); 1910 $.post('/cart/get_order_total/', function (data) { 1911 order_total_el.each(function () { 1912 $('#shipping_container').removeClass('shipping_options_loading'); 1913 $(this).html(Number(data).toLocaleString('en', {minimumFractionDigits: 2})); 1914 if ($('#sc_checkout_content .proceed_to_payment').hasClass('active')) { 1915 window.cart.enable_checkout_button(); 1916 } 1917 }); 1918 }); 1919 1920 }, 1921 1922 update_total_weight: function (el) { 1923 $.post('/cart/get_total_weight/', function (data) { 1924 el.html(data); 1925 }, 'html'); 1926 }, 1927 1928 update_item_total_price: function (uid) { 1929 $.post('/cart/get_item_total_price/', {'unique_id': uid}, function (obj) { 1930 var $price = $('.product_detail[data-uid="' + uid + '"]').find('.totalprice'), 1931 $price_without_vat = $('.product_detail[data-uid="' + uid + '"]').find('.totalprice_without_vat'); 1932 $price.html(obj.total_price); 1933 $price_without_vat.html(Number(obj.total_price_without_vat).toLocaleString('en', {minimumFractionDigits: 2})); 1934 }, 'json'); 1935 }, 1936 1937 update_shipping_cost: function (shipping_cost_el) { 1938 if ($(shipping_cost_el).length) { 1939 $.post('/cart/get_shipping_cost/', function (data) { 1940 shipping_cost_el.html(Number(data).toLocaleString('en', {minimumFractionDigits: 2})); 1941 }, 'html'); 1942 } 1943 }, 1944 1945 update_shipping_cost_inc_vat: function (shipping_cost_el) { 1946 if ($(shipping_cost_el).length) { 1947 $('#shipping_container').addClass('shipping_options_loading'); 1948 $.post('/cart/get_shipping_cost_inc_vat/', function (data) { 1949 $('#shipping_container').removeClass('shipping_options_loading'); 1950 shipping_cost_el.html(Number(data).toLocaleString('en', {minimumFractionDigits: 2})); 1951 }, 'html'); 1952 } 1953 }, 1954 1955 enable_checkout_button: function () { 1956 if ($('.proceed_to_payment').attr('data-payment-type') == 'stripe') { 1957 // Validate fields first 1958 var fields_validated = true; 1959 // TODO: make validate_shipping_info return a bool and use the result of that. 1960 if ($('#shipping_options_container').length) { 1961 if ($('#shipping_options_container.active').length 1962 && parseInt($('#shipping_options_container select').val())) { 1963 $('#sc_checkout_button').removeClass('sc_shipping_unconfirmed'); 1964 } else { 1965 $('#sc_checkout_button').addClass('sc_shipping_unconfirmed'); 1966 if ($('button.stripe-button-el:not(.stripe_placeholder_button)').length) { 1967 $('button.stripe-button-el:not(.stripe_placeholder_button)').attr("disabled", true); 1968 } 1969 var fields_validated = false; 1970 } 1971 } 1972 if ($('#shipping_zones_container').length) { 1973 if ($('#shipping_zones_container.active').length 1974 && parseInt($('#shipping_zones_container select').val())) { 1975 $('#sc_checkout_button').removeClass('sc_shipping_unconfirmed'); 1976 } else { 1977 $('#sc_checkout_button').addClass('sc_shipping_unconfirmed'); 1978 if ($('button.stripe-button-el:not(.stripe_placeholder_button)').length) { 1979 $('button.stripe-button-el:not(.stripe_placeholder_button)').attr("disabled", true); 1980 } 1981 var fields_validated = false;
1982 } 1983 } 1984 if ($('#sc_terms_box').length) { 1985 if ($('#sc_terms_box #sc_terms_agree').is(':checked')) { 1986 $('#sc_checkout_button').removeClass('sc_terms_unconfirmed'); 1987 } else { 1988 $('#sc_checkout_button').addClass('sc_terms_unconfirmed'); 1989 if ($('button.stripe-button-el:not(.stripe_placeholder_button)').length) { 1990 $('button.stripe-button-el:not(.stripe_placeholder_button)').attr("disabled", true); 1991 } 1992 var fields_validated = false; 1993 } 1994 } 1995 1996 checkout_form_validated = false; 1997 if (fields_validated && !$('#sc_checkout_button').hasClass('sc_fields_unconfirmed') && !$('#sc_checkout_button').hasClass('sc_quantities_failed') && !$('#sc_checkout_button').hasClass('sc_shipping_unconfirmed') && !$('#sc_checkout_button').hasClass('sc_terms_unconfirmed')) { 1998 checkout_form_validated = true; 1999 } 2000 2001 if (checkout_form_validated) { 2002 if ($('#sc_additional_fields form').length) { 2003 window.archimedes.archimedes_core.analytics.track_campaigns.save_form_data($('#sc_additional_fields form')); 2004 } 2005 window.cart.stripe_init_button(); 2006 } 2007 2008 } 2009 }, 2010 2011 disable_checkout_button: function () { 2012 $('.proceed_to_payment').removeClass('active'); 2013 $('#stripe_loader').hide(); 2014 if ($('.proceed_to_payment').attr('data-payment-type') == 'stripe') { 2015 if ($('#stripe_button_container[data-placeholder-button]').length) { 2016 $('.proceed_to_payment').addClass('placeholder'); 2017 $('#stripe_button_container').html(''); 2018 var button_placeholder_content = $('#stripe_button_container').attr('data-placeholder-button'); 2019 $('#stripe_button_container').html(button_placeholder_content); 2020 $('#stripe_button_container').find('button').click(function () { 2021 return false; 2022 }); 2023 } else { 2024 $('#stripe_button_container').html(''); 2025 } 2026 } 2027 }, 2028 2029 proceed_to_payment: function () { 2030 var proceed_button = $('.proceed_to_payment'); 2031 var payment_type = proceed_button.attr('data-payment-type'); 2032 if (payment_type == 'worldpay') { 2033 proceed_button.unbind().click(function () { 2034 if ($('#shipping_zones').val() !== '' && $('#shipping_options').val() !== '') { 2035 $.post('/cart/worldpay/', function (data) { 2036 $('#worldpay').html(data); 2037 $('#worldpay form').submit(); 2038 }, 'html'); 2039 return false; 2040 } else { 2041 return false; 2042 } 2043 }); 2044 } else if (payment_type == 'stripe') { 2045 window.cart.disable_checkout_button(); 2046 } 2047 }, 2048 2049 set_currency: function () { 2050 $('.currency_store_select').change(function () { 2051 $.post('/cart/set_session_currency/', {'currency': $(this).val()}, function (data) { 2052 data = jQuery.parseJSON(data); 2053 console.log(data) 2054 window.cart.set_cart_session_in_localstorage(data.cart_session); 2055 location.reload(); 2056 }); 2057 2058 }); 2059 }, 2060 2061 2062 checkout_first_step_form: function () { 2063 $("#checkout_first_step #sc_next_step_button a").unbind().click(function () { 2064 $(this).closest('form').submit(); 2065 return false; 2066 }); 2067 $("#checkout_first_step").submit(function (event) { 2068 if (!$(this).hasClass('submitting')) { 2069 $(this).addClass('submitting'); 2070 $('#sc_checkout_button .button').addClass('loading'); 2071 event.preventDefault(); 2072 var post_url = $(this).attr("action"); 2073 var request_method = $(this).attr("method"); 2074 var terms_and_conditions = $('#sc_terms_agree').is(':checked') 2075 var terms_and_conditions_accepted = 0 2076 if (terms_and_conditions || $('#sc_terms_agree').length < 1) { 2077 terms_and_conditions_accepted = 1 2078 } 2079 var form_data = $(this).serialize() + '&terms_and_conditions_accepted=' + terms_and_conditions_accepted; 2080 2081 if (typeof window.archimedes != 'undefined') { 2082 if (typeof window.archimedes.proxy_dir != 'undefined') { 2083 if (window.archimedes.proxy_dir != '') { 2084 form_data = form_data + '&proxy_dir=' + window.archimedes.proxy_dir; 2085 } 2086 } 2087 } 2088 2089 // // TODO CLEAN THE ERRORS FROM THE PREVIOUS RESPONSE 2090 $.ajax({ 2091 url: post_url, 2092 type: request_method, 2093 data: form_data, 2094 dataType: "json", 2095 error: function () { 2096 $("#checkout_first_step").removeClass('submitting'); 2097 $('#sc_checkout_button .button').removeClass('loading'); 2098 h.alert('<h2>Sorry</h2><div>An error occurred while trying to process your information.</div>'); 2099 } 2100 }).done(function (response) { 2101 if (!response.validated) { 2102 $("#checkout_first_step").removeClass('submitting'); 2103 $('#sc_checkout_button .button').removeClass('loading'); 2104 $.each(response.errors, function (i, error) { 2105 $('[name="' + error.required_field + '"]').closest('.form_row').addClass('error'); 2106 }); 2107 h.alert('<h2>Sorry</h2>' + response.message); 2108 console.log(response); 2109 } else { 2110 window.location.href = (response.redirect_to) + (window.location.search.indexOf('_cmspreview=1') > 0 ? '&_cmspreview=1' : ''); 2111 } 2112 }); 2113 } else { 2114 return false;
2115 } 2116 }); 2117 if ($("#checkout_first_step #sc_next_step_button a").length) { 2118 $('body').removeClass('checkout_second_step checkout_third_step'); 2119 $('body').addClass('checkout_first_step'); 2120 } 2121 }, 2122 2123 checkout_second_step_form: function () { 2124 $("#save_checkout_delivery_form #sc_next_step_button a").unbind().click(function () { 2125 2126 if (window.cart.test_localstorage()) { 2127 // Store the billing postcode if it is needed for the next step 2128 var postcode = $('#sc_field_postcode_shipping').val(); 2129 if (typeof postcode != 'undefined') { 2130 if (postcode != '' && postcode) { 2131 localStorage.setItem('cart_billing_postcode', postcode); 2132 } 2133 } 2134 } 2135 2136 $(this).closest('form').submit(); 2137 return false; 2138 }); 2139 $("#save_checkout_delivery_form").submit(function (event) { 2140 event.preventDefault(); 2141 if (!$(this).hasClass('submitting')) { 2142 $(this).addClass('submitting'); 2143 $('#sc_checkout_button .button').addClass('loading'); 2144 var post_url = $(this).attr("action"); 2145 var request_method = $(this).attr("method"); 2146 var form_data = $(this).serialize(); 2147 2148 if (typeof window.archimedes != 'undefined') { 2149 if (typeof window.archimedes.proxy_dir != 'undefined') { 2150 if (window.archimedes.proxy_dir != '') { 2151 form_data = form_data + '&proxy_dir=' + window.archimedes.proxy_dir; 2152 } 2153 } 2154 } 2155 2156 // TODO CLEAN THE ERRORS FROM THE PREVIOUS RESPONSE 2157 2158 $.ajax({ 2159 url: post_url, 2160 type: request_method, 2161 data: form_data, 2162 dataType: "json", 2163 error: function () { 2164 $("#save_checkout_delivery_form").removeClass('submitting'); 2165 $('#sc_checkout_button .button').removeClass('loading'); 2166 h.alert('<h2>Sorry</h2><div>An error occurred while trying to process your information.</div>'); 2167 } 2168 }).done(function (response) { 2169 // CHECK RESPONSE!!! 2170 console.log(response); 2171 if (!response.validated) { 2172 $("#save_checkout_delivery_form").removeClass('submitting'); 2173 $('#sc_checkout_button .button').removeClass('loading'); 2174 $.each(response.errors, function (i, error) { 2175 $('[name="' + error.required_field + '"]').closest('.form_row').addClass('error'); 2176 }); 2177 h.alert('<h2>Sorry</h2><div>Please fill in all the required fields and ensure your details are valid.</div>'); 2178 console.log(response); 2179 } else { 2180 window.location.href = (response.redirect_to) + (window.location.search.indexOf('_cmspreview=1') > 0 ? '&_cmspreview=1' : ''); 2181 } 2182 }); 2183 } else { 2184 return false; 2185 } 2186 }); 2187 if ($("#save_checkout_delivery_form #sc_next_step_button a").length) { 2188 $('body').removeClass('checkout_first_step checkout_third_step'); 2189 $('body').addClass('checkout_second_step'); 2190 } 2191 }, 2192 2193 2194 stripe_checkout_payment_intent: function (stripeStyleOverride = "") { 2195 2196 if ($('#stripe-card-button').length) { 2197 2198 $('body').removeClass('checkout_first_step checkout_second_step'); 2199 $('body').addClass('checkout_third_step'); 2200 2201 var stripe_publishable_key = $('#stripe_publishable_key').val() 2202 var connected_stripe_user_id = $('#connected_stripe_user_id').val() 2203 2204 // Removing this because of the potential cloudflare caching issue below - var mm_post_code = $('#mm_post_code').val(); 2205 var mm_post_code = false;
2206 if (window.cart.test_localstorage()) { 2207 // Use localstorage stored billing postcode instead of the 'mm_post_code' field as it is more reliable if it exists. This avoids any issues of cached checkout screens, which is not usually possible but cloudflare may serve the last uncached page if it does not respond. 2208 var localstorage_postcode = localStorage.getItem('cart_billing_postcode'); 2209 if (typeof localstorage_postcode != 'undefined') { 2210 if (localstorage_postcode && localstorage_postcode != '' && localstorage_postcode != 'undefined' && localstorage_postcode != 'null') { 2211 mm_post_code = localstorage_postcode; 2212 } 2213 } 2214 } 2215 2216 if (connected_stripe_user_id) { 2217 var stripe = Stripe(stripe_publishable_key, {stripeAccount: connected_stripe_user_id}); 2218 } else { 2219 var stripe = Stripe(stripe_publishable_key); 2220 } 2221 2222 var elements = stripe.elements({ 2223 //fonts: [ 2224 // { 2225 // cssSrc: 'https://fonts.googleapis.com/css?family=Source+Code+Pro', 2226 // }, 2227 //] 2228 }); 2229 //var cardElement = elements.create('card'); 2230 //cardElement.mount('#card-element'); 2231 2232 var cardholderName = $('#cardholder-name'); 2233 if(stripeStyleOverride!=''){ 2234 var elementStyles = stripeStyleOverride 2235 }else{ 2236 var elementStyles = { 2237 base: { 2238 color: $("body").css("color"), 2239 fontWeight: $("body").css("font-weight"), 2240 fontFamily: $("body").css("font-family"), 2241 fontSize: $("body").css("font-size"), 2242 fontSmoothing: 'antialiased', 2243 '::placeholder': { 2244 color: $("body").css("color"), 2245 }, 2246 ':-webkit-autofill': { 2247 color: $("body").css("color"), 2248 }, 2249 }, 2250 invalid: { 2251 color: '#E25950', 2252 '::placeholder': {
2253 color: '#FFCCA5', 2254 }, 2255 }, 2256 }; 2257 } 2258 //Assign same style to cardholder name 2259 const styles = ["color", "font-weight", "font-family", "font-size"]; 2260 styles.forEach(style => { 2261 $('#cardholder-name').css(style, $("body").css(style)); 2262 }); 2263 2264 2265 var elementClasses = { 2266 focus: 'focused', 2267 empty: 'empty', 2268 invalid: 'invalid', 2269 }; 2270 2271 var cardNumber = elements.create('cardNumber', { 2272 style: elementStyles, 2273 classes: elementClasses, 2274 placeholder: '' 2275 }); 2276 cardNumber.mount('#sc_stripe_card_number'); 2277 2278 var cardExpiry = elements.create('cardExpiry', { 2279 style: elementStyles, 2280 classes: elementClasses, 2281 placeholder: '' 2282 }); 2283 cardExpiry.mount('#sc_stripe_card_expiry'); 2284 2285 var cardCvc = elements.create('cardCvc', { 2286 style: elementStyles, 2287 classes: elementClasses, 2288 placeholder: '' 2289 }); 2290 cardCvc.mount('#sc_stripe_card_cvc'); 2291 2292 if ($('#sc_stripe_card_zip').length) { 2293 if ($('.stripe_checkout_zip_code_disabled').length > 0 && mm_post_code && mm_post_code != '' && mm_post_code != 'undefined' && mm_post_code != 'null') { 2294 var cardZip = elements.create('postalCode', { 2295 style: elementStyles, 2296 disabled: true, 2297 value: mm_post_code, 2298 classes: elementClasses, 2299 placeholder: '' 2300 }); 2301 2302 $('.stripe_checkout_zip_code_disabled .overlay').click(function () { 2303 var basket_url = window.location.pathname; 2304 h.alert('<h2>Changing your payment address</h2> Your payment Postcode/ZIP must match your shipping address. If you need to change this, please go <a href="' + basket_url + '?checkout-step=2">back to the previous step</a>.'); 2305 }); 2306 2307 } else { 2308 var cardZip = elements.create('postalCode', { 2309 style: elementStyles, 2310 classes: elementClasses, 2311 placeholder: '' 2312 }); 2313 $('.stripe_checkout_zip_code_disabled .overlay').remove(); 2314 $('.stripe_checkout_zip_code_disabled').removeClass('stripe_checkout_zip_code_disabled'); 2315 } 2316 cardZip.mount('#sc_stripe_card_zip'); 2317 } 2318 2319 //registerElements([cardNumber, cardExpiry, cardCvc], 'example2'); 2320 2321 2322 $("#stripe-card-button").click(function () { 2323 2324 if (!$('#sc_checkout_payment_form').hasClass('submitting')) { 2325 $('#sc_checkout_payment_form').addClass('submitting'); 2326 $("#stripe-card-button").parent().addClass('loading'); 2327 stripe.createPaymentMethod('card', cardNumber, { 2328 billing_details: { 2329 name: cardholderName.value 2330 } 2331 }).then(function (result) { 2332 if (result.error) { 2333 $('#sc_checkout_payment_form').removeClass('submitting'); 2334 $("#stripe-card-button").parent().removeClass('loading'); 2335 h.alert('<h2>Sorry</h2><div>' + result.error.message + '</div>'); 2336 // h.alert('<h2>Sorry</h2><div>An error occurred while processing your card details. Please try again or contact us for support.</div>'); 2337 // Show error in payment form 2338 } else { 2339 // Otherwise send paymentMethod.id to your server (see Step 2) 2340 2341 var payment_controller = $('#payment_controller').val() 2342 if (!payment_controller) { 2343 payment_controller = '/cart/stripe_create_confirm_payment_intent/' 2344 } 2345 2346 var request = $.ajax({ 2347 url: payment_controller, 2348 type: "POST", 2349 data: { 2350 payment_method_id: result.paymentMethod.id, 2351 payment: JSON.stringify(result.paymentMethod), 2352 proxy_dir: (typeof window.archimedes != 'undefined' ? (typeof window.archimedes.proxy_dir != 'undefined' ? window.archimedes.proxy_dir : '') : ''), 2353 _cmspreview: (window.location.search.indexOf('_cmspreview=1') > 0 ? '1' : '') 2354 }, 2355 dataType: "json", 2356 error: function () { 2357 $('#sc_checkout_payment_form').removeClass('submitting'); 2358 $("#stripe-card-button").parent().removeClass('loading'); 2359 h.alert('<h2>Sorry</h2><div>An error occurred while trying to process your payment information. Please check and try again or c
2359ontact us for support. (Error code 2001)</div>'); 2360 } 2361 }); 2362 2363 request.done(function (result) { 2364 handleServerResponse(result); 2365 }); 2366 } 2367 }); 2368 } 2369 return false; 2370 }); 2371 2372 2373 if ($('#stripe-payment-request-button').length && $('#stripe-payment-request-button').attr('data-currency-code') != '' && $('#stripe-payment-request-button').attr('data-order-total') != '') { 2374 // ApplePay, GooglePay, MicrosoftPay methods 2375 var paymentRequest = stripe.paymentRequest({ 2376 country: $('#stripe-payment-request-button').attr('data-stripe-locale'), 2377 currency: $('#stripe-payment-request-button').attr('data-currency').toLowerCase(), 2378 total: { 2379 label: $('#stripe-payment-request-button').attr('data-payment-name'), 2380 amount: parseInt($('#stripe-payment-request-button').attr('data-order-total')), 2381 }, 2382 requestPayerName: true, 2383 requestPayerEmail: true, 2384 }); 2385 var prButton = elements.create('paymentRequestButton', { 2386 paymentRequest: paymentRequest, 2387 }); 2388 paymentRequest.canMakePayment().then(function (result) { 2389 if (result && result.applePay) { 2390 // Currently only enabled for Apple Pay 2391 // Google/Microsoft pay also supported - for browser stored-card type payments 2392 prButton.mount('#stripe-payment-request-button'); 2393 $('#sc_checkout_payment_methods_container').addClass('sc_multiple_methods'); 2394 } else { 2395 $('#sc_stripe_request_payment_method_container').hide(); 2396 } 2397 }); 2398 2399 paymentRequest.on('paymentmethod', function (result) { 2400 2401 var payment_controller = $('#payment_controller').val() 2402 if (!payment_controller) { 2403 payment_controller = '/cart/stripe_create_confirm_payment_intent/' 2404 } 2405 2406 var request = $.ajax({ 2407 url: payment_controller, 2408 type: "POST", 2409 data: { 2410 payment_method_id: result.paymentMethod.id, 2411 payment: JSON.stringify(result.paymentMethod), 2412 proxy_dir: (typeof window.archimedes != 'undefined' ? (typeof window.archimedes.proxy_dir != 'undefined' ? window.archimedes.proxy_dir : '') : ''), 2413 _cmspreview: (window.location.search.indexOf('_cmspreview=1') > 0 ? '1' : '') 2414 }, 2415 dataType: "json", 2416 error: function () { 2417 $('#sc_checkout_payment_form').removeClass('submitting'); 2418 $("#stripe-card-button").parent().removeClass('loading'); 2419 h.alert('<h2>Sorry</h2><div>An error occurred while trying to process your payment information. Please check and try again or contact us for support. (Error code 2001)</div>'); 2420 } 2421 }); 2422 2423 request.done(function (result) { 2424 handleServerResponse(result); 2425 }); 2426 2427 }); 2428 } 2429 2430 function handleServerResponse(response) { 2431 2432 if (response.error) { 2433 if (response.redirect_to && typeof response.redirect_to != 'undefined') { 2434 window.location.href = (response.redirect_to) + (window.location.search.indexOf('_cmspreview=1') > 0 ? '&_cmspreview=1' : ''); 2435 } else { 2436 console.log(response); 2437 if (response.message) { 2438 var error_message = response.message 2439 } else { 2440 var error_message = (response.error_message_visual && typeof response.error_message_visual != 'undefined' ? response.error_message_visual : 'An error occurred while trying to process your payment information. Please check and try again or c
2440ontact us for support. (Error code 2002'); 2441 } 2442 $('#sc_checkout_payment_form').removeClass('submitting'); 2443 $("#stripe-card-button").parent().removeClass('loading'); 2444 h.alert('<h2>Sorry</h2><div>' + error_message + '</div>'); 2445 } 2446 } else if (response.requires_action) { 2447 2448 // Use Stripe.js to handle required card action 2449 stripe.handleCardAction( 2450 response.payment_intent_client_secret 2451 ).then(function (result) { 2452 if (result.error) { 2453 2454 if (response.redirect_to && typeof response.redirect_to != 'undefined') { 2455 window.location.href = (response.redirect_to) + (window.location.search.indexOf('_cmspreview=1') > 0 ? '&_cmspreview=1' : ''); 2456 } else { 2457 2458 // Show error in payment form 2459 console.log(result); 2460 if (response.message) { 2461 var error_message = response.message 2462 } else { 2463 var error_message = (response.error_message_visual && typeof response.error_message_visual != 'undefined' ? response.error_message_visual : 'An error occurred while trying to process your payment information. Please check and try again or contact us for support. (Error code 2002'); 2464 } 2465 if (result.error && typeof result.error != 'undefined') { 2466 if (result.error.code && typeof result.error.code != 'undefined') { 2467 if (result.error.code == 'payment_intent_authentication_failure') { 2468 var error_message = 'The secondary security check for your payment method has failed. Please ensure the verification details are correct or contact your card provider for further support.' 2469 } 2470 } 2471 } 2472 2473 $('#sc_checkout_payment_form').removeClass('submitting'); 2474 $("#stripe-card-button").parent().removeClass('loading'); 2475 h.alert('<h2>Sorry</h2><div>' + error_message + '</div>'); 2476 } 2477 } else { 2478 // The card action has been handled 2479 // The PaymentIntent can be confirmed again on the server 2480 2481 var payment_controller = $('#payment_controller').val() 2482 if (!payment_controller) { 2483 payment_controller = '/cart/stripe_create_confirm_payment_intent/' 2484 } 2485 2486 var request = $.ajax({ 2487 url: payment_controller, 2488 type: "POST", 2489 data: { 2490 payment_intent_id: result.paymentIntent.id, 2491 proxy_dir: (typeof window.archimedes != 'undefined' ? (typeof window.archimedes.proxy_dir != 'undefined' ? window.archimedes.proxy_dir : '') : ''), 2492 _cmspreview: (window.location.search.indexOf('_cmspreview=1') > 0 ? '1' : '') 2493 }, 2494 dataType: "json", 2495 error: function () { 2496 $('#sc_checkout_payment_form').removeClass('submitting'); 2497 $("#stripe-card-button").parent().removeClass('loading'); 2498 h.alert('<h2>Sorry</h2><div>An error occurred while trying to process your payment information. Please check your email to see if your payment was successful. Alternatively check the form and try again. (Error code 2004)</div>'); 2499 } 2500 }); 2501 2502 request.done(function (result) { 2503 handleServerResponse(result); 2504 }); 2505 } 2506 }); 2507 } else { 2508 if (response.redirect_to && typeof response.redirect_to != 'undefined') { 2509 window.location.href = (response.redirect_to) + (window.location.search.indexOf('_cmspreview=1') > 0 ? '&_cmspreview=1' : ''); 2510 } else { 2511 console.log(response); 2512 h.alert('<h2>Sorry</h2><div>An error occurred while trying to process your payment information. Please check and try again or c
2512ontact us for support. (Error code 2005)</div>'); 2513 } 2514 } 2515 } 2516 2517 } 2518 }, 2519 2520 stripe_init_button: function () { 2521 $('#stripe_form').each(function () { 2522 window.cart.disable_checkout_button(); 2523 $('.proceed_to_payment').addClass('loading').removeClass('placeholder'); 2524 $('#stripe_loader').show(); 2525 2526 $.post('/cart/get_order_total/', function (order_total) { 2527 2528 $('#stripe_button_container') 2529 .delay(400) 2530 .queue(function () { 2531 $('#stripe_button_container').html(''); 2532 var stripe_form = $("#stripe_form"); 2533 var stripe_key = $(stripe_form).attr('data-key'); 2534 var stripe_description = $(stripe_form).attr('data-description'); 2535 var stripe_checkout_icon = ($(stripe_form).attr('data-icon') && typeof $(stripe_form).attr('data-icon') != 'undefined' ? $(stripe_form).attr('data-icon') : ''); 2536 var stripe_name = $(stripe_form).attr('data-name'); 2537 var stripe_currency = $(stripe_form).attr('data-currency'); 2538 var stripe_bitcoin = $(stripe_form).attr('data-bitcoin'); 2539 var stripe_enable_shipping_address = $(stripe_form).attr('data-shipping-address'); 2540 var stripe_email = ($(stripe_form).attr('data-email') && typeof $(stripe_form).attr('data-email') != 'undefined' ? $(stripe_form).attr('data-email') : ''); 2541 var stripe_total = order_total.replace('.', ''); 2542 2543 var script_tag = document.createElement('script'); 2544 script_tag.setAttribute('src', 'https://checkout.stripe.com/checkout.js'); 2545 script_tag.setAttribute('class', 'stripe-button'); 2546 script_tag.setAttribute('data-key', stripe_key); 2547 script_tag.setAttribute('data-amount', stripe_total); 2548 script_tag.setAttribute('data-name', stripe_name); 2549 script_tag.setAttribute('data-currency', stripe_currency); 2550 script_tag.setAttribute('data-description', stripe_description); 2551 script_tag.setAttribute('data-bitcoin', stripe_bitcoin); 2552 script_tag.setAttribute('data-billing-address', 'true'); 2553 script_tag.setAttribute('data-zip-code', 'true'); 2554 script_tag.setAttribute('data-locale', 'auto'); 2555 2556 script_tag.setAttribute('data-shipping-address', stripe_enable_shipping_address); 2557 script_tag.setAttribute('data-email', stripe_email); 2558 script_tag.setAttribute('data-image', stripe_checkout_icon) 2559 2560 script_tag.setAttribute('data-allow-remember-me', 'true'); 2561 script_tag.setAttribute('data-label', 'Pay now'); 2562 document.getElementById('stripe_button_container').appendChild(script_tag); 2563 $(this).dequeue(); 2564 }) 2565 .delay(400) 2566 .queue(function () { 2567 $('#stripe_loader').hide(); 2568 $('.proceed_to_payment').removeClass('loading'); 2569 // Check fields have been validated before making the button active 2570 if (!$('#sc_checkout_button').hasClass('sc_fields_unconfirmed') && !$('#sc_checkout_button').hasClass('sc_shipping_unconfirmed') && !$('#sc_checkout_button').hasClass('sc_terms_unconfirmed')) { 2571 2572 $('.proceed_to_payment').addClass('active'); 2573 } else { 2574 $('.proceed_to_payment').addClass('placeholder'); 2575 } 2576 if (order_total <= 0) { 2577 // If the total value of the transaction is 0, prevent the user getting to Stripe as the process will fail. 2578 $('#stripe_button_container').html(''); 2579 h.alert("Transaction of value £0.00 cannot be completed.") 2580 } 2581 $(this).dequeue(); 2582 }) 2583 ; 2584 2585 //$('#stripe_form #stripe_button_container').html('<div><script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="' + stripe_key + '"data-amount="' + stripe_total + '"data-name="' + stripe_name + '"data-currency="' + stripe_currency + '"data-description="' + stripe_description + '"data-billing-address="true" data-shipping-address="true" data-label="Pay now" data-allow-remember-me="true"></script></div>'); 2586 }); 2587 }); 2588 }, 2589
2590 stripe_payment_request: function () { 2591 if ($('#payment-request-button').length > 0 && typeof Stripe != 'undefined') { 2592 2593 console.log("run stripe_payment_request"); 2594 2595 var paymentRequestContainer = document.getElementById('payment-request-button'); 2596 var quickCartSetup = false; 2597 2598 if (!paymentRequestContainer) { 2599 return; 2600 } 2601 2602 function paymentComplete(intent) { 2603 $.post('/cart/quick_payment_complete/', { 'item': JSON.stringify(intent) }, function (obj) { 2604 var data = JSON.parse(obj); 2605 if (!data.error) { 2606 window.location.href = '/store/confirmation/?transaction_id=' + data.transaction_id; 2607 } 2608 }); 2609 } 2610 2611 var added_item_details = {} 2612 var item_uid = $(paymentRequestContainer.parentElement).find('.store_item').attr('data-uid'); 2613 2614 $('.store_item[data-uid="' + item_uid + '"]').find('input').each(function (data) { 2615 added_item_details[$(this).attr('name')] = $(this).val(); 2616 }); 2617 var stripe = Stripe(paymentRequestContainer.dataset.stripeKey, { 2618 apiVersion: "2022-11-15", 2619 stripeAccount: paymentRequestContainer.dataset.connectedid 2620 }); 2621 2622 var sitename = "Total"; 2623 2624 //fallback if og:site_name is not set 2625 2626 if ($('meta[property="og:site_name"]').attr('content') != ''){ 2627 sitename = $('meta[property="og:site_name"]').attr('content'); 2628 } 2629 2630 var paymentRequest = stripe.paymentRequest({ 2631 country: paymentRequestContainer.dataset.country, 2632 currency: paymentRequestContainer.dataset.currency, 2633 2634 total: { 2635 label: sitename, 2636 2637 amount: Number(paymentRequestContainer.dataset.price), 2638 }, 2639 requestPayerName: true, 2640 requestPayerEmail: true, 2641 requestShipping: true, 2642 requestPayerPhone: true, 2643 disableWallets: ['googlePay', 'link', 'browserCard'], 2644 }); 2645 var elements = stripe.elements(); 2646 var prButton = elements.create('paymentRequestButton', { 2647 paymentRequest: paymentRequest, 2648 }); 2649 2650 paymentRequest.canMakePayment().then(function (result) { 2651 if (result) { 2652 console.log("with result"); 2653 prButton.mount('#payment-request-button'); 2654 } else { 2655 console.log("without result"); 2656 document.getElementById('payment-request-button').style.display = 'none'; 2657 } 2658 }); 2659 2660 paymentRequest.on('shippingaddresschange', function (event) { 2661 var updateWith = event.updateWith; 2662 var shippingAddress = event.shippingAddress; 2663 2664 if (!quickCartSetup) { 2665 $.post('/cart/quick_setup/', { 'item': JSON.stringify(added_item_details) }, function (obj) { 2666 if (!obj.error) { 2667 quickCartSetup = true; 2668 $.post('/cart/quick_address_update/', { 'item': JSON.stringify(shippingAddress) }, function (obj) { 2669 updateWith(JSON.parse(obj)); 2670 }); 2671 } else { 2672 document.getElementById('payment-request-button').style.display = 'none'; 2673 } 2674 }); 2675 } else { 2676 $.post('/cart/quick_address_update/', { 'item': JSON.stringify(shippingAddress) }, function (obj) { 2677 updateWith(JSON.parse(obj)); 2678 }); 2679 } 2680 }); 2681 2682 paymentRequest.on('shippingoptionchange', function (event) { 2683 var updateWith = event.updateWith; 2684 var shippingOption = event.shippingOption; 2685 2686 total_price = { 2687 label: sitename, 2688 amount: Number(paymentRequestContainer.dataset.price) 2689 } 2690 2691 $.post('/cart/quick_shipping_option_update/', { 2692 'item': JSON.stringify(shippingOption), 2693 'total': JSON.stringify(total_price) 2694 2695 }, function (obj) { 2696 updateWith(JSON.parse(obj)); 2697 }); 2698 }); 2699 2700 paymentRequest.on('paymentmethod', function (ev) { 2701 $.post('/cart/quick_intent/',{'data': JSON.stringify(ev)} 2702 ).then(function (result) { 2703 var r = JSON.parse(result); 2704 var clientSecret = r.client_secret; 2705 2706 ev.clientSecret = clientSecret; 2707 const { paymentIntent, error: confirmError } = stripe.confirmCardPayment( 2708 clientSecret, 2709 { payment_method: ev.paymentMethod.id }, 2710 { handleActions: false } 2711 ); 2712 2713 if (confirmError) { 2714 ev.complete('fail'); 2715 h.alert('There was an issue processing your payment. If the issue persists, please contact support.'); //show an error message and close the payment interface. 2716 } else { 2717 ev.complete('success'); 2718 paymentComplete(ev); 2719 } 2720 }); 2721 }); 2722 }else{ 2723 return; 2724 } 2725 }, 2726 2727 2728 // ******************* 2729 // ******************* 2730 // WISHLIST SPECIFIC FUNCTIONS 2731 // ******************* 2732 // ******************* 2733 wishlist_summary: { 2734 2735 init: function () { 2736 2737 $('#wishlist_cart_widget').unbind().click(function () { 2738 $(this).find('.wcw_checkout_link').trigger('click'); 2739 }) 2740 2741 if ($('#wishlist_cart_widget').length > 0 || $('#wishlist_quick_cart_widget').length > 0) { 2742 window.cart.wishlist_summary.widget_setup(); 2743 window.cart.wishlist_summary.quick_wishlist_cart_widget_setup(); 2744 2745 if ($('#wishlist_cart_widget').hasClass('hide_when_empty')) { 2746 $('#wishlist_cart_widget').hide(); 2747 } 2748 2749 //Standard cart summary check, using ajax calls 2750 window.cart.wishlist_summary.get_summary($('#wishlist_quick_cart_widget').hasClass('wqcw_initialised') ? false : true); 2751 2752 } 2753 2754 }, 2755 2756 widget_setup: function () { 2757 if ($('#wishlist_cart_widget').hasClass('hide_when_empty')) { 2758 $('#wishlist_cart_widget').hide(); 2759 } 2760 if ($('#wishlist_quick_cart_widget').length > 0) { 2761 $('#wishlist_cart_widget .wcw_checkout_link').unbind('click.wcw_basket_link').bind('click.wcw_basket_link', function () { 2762 window.cart.wishlist_summary.quick_cart_widget_open(); 2763 return false;
2764 }); 2765 } 2766 }, 2767 2768 quick_wishlist_cart_widget_setup: function () { 2769 if ($('#wishlist_quick_cart_widget').length) { 2770 $('#wishlist_quick_cart_widget #wqcw_close').unbind().click(function () { 2771 window.cart.wishlist_summary.quick_wishlist_cart_widget_close(); 2772 }); 2773 } 2774 }, 2775 2776 get_summary: function (quick_cart_auto_open, error_info) { 2777 quick_cart_auto_open = (typeof quick_cart_auto_open != 'undefined' ? quick_cart_auto_open : true); 2778 error_info = (typeof error_info != 'undefined' ? error_info : {}); 2779 2780 if ($('#wishlist_cart_widget').length > 0 || $('#wishlist_quick_cart_widget').length > 0) { 2781 $.ajax({ 2782 type: "POST", 2783 url: '/cart/get_wishlist_info/', 2784 success: function (data) { 2785 if ($('#wishlist_cart_widget').length) { 2786 window.cart.wishlist_summary.update(data.count); 2787 } 2788 if ($('#wishlist_quick_cart_widget').length) { 2789 var count = data.count && typeof data.count != 'undefined' ? data.count : 0; 2790 if (count > 0) { 2791 $('#wishlist_quick_cart_widget').removeClass('wqcw_empty'); 2792 } else { 2793 $('#wishlist_quick_cart_widget').addClass('wqcw_empty'); 2794 } 2795 $('#wishlist_quick_cart_widget #wqcw_item_count').html(count); 2796 $('#wishlist_quick_cart_widget #wqcw_items').html(data.quick_cart_widget_html); 2797 // Initialise cart functions 2798 window.cart.remove_from_wishlist($('#wqcw_items .remove'), false); 2799 window.cart.remove_all_wishlist_items($('#wqcw_remove_all'), false); 2800 // Open the panel if the auto open setting is enabled 2801 if (quick_cart_auto_open && $('#wishlist_quick_cart_widget').hasClass('wqcw_auto_open') && $('#wishlist_quick_cart_widget').hasClass('wqcw_initialised')) { 2802 if (error_info.errortype) { 2803 window.cart.wishlist_summary.quick_cart_widget_open(error_info.item_uid, error_info.errortype, error_info.errormsg); 2804 } else { 2805 window.cart.wishlist_summary.quick_cart_widget_open(); 2806 } 2807 } 2808 if (!quick_cart_auto_open) { 2809 window.cart.wishlist_summary.quick_wishlist_cart_widget_open_callback(); 2810 } 2811 // Add the initialised class to show this has finished loading for the first time 2812 $('#wishlist_quick_cart_widget').addClass('wqcw_initialised'); 2813 } 2814 2815 var all_uids = []; 2816 if (typeof data.cart_contents != 'undefined') { 2817 var all_uids = Object.keys(data.cart_contents).map(function (key) { 2818 return key; 2819 }); 2820 window.cart.update_wishlist_uids('replace', all_uids); 2821 } 2822 2823 window.cart.set_cart_session_in_localstorage(data.cart_session); 2824 }, 2825 dataType: 'json' 2826 }); 2827 } 2828 2829 }, 2830 2831 update: function (total_count, action, uid) { 2832 // debugger; 2833 if ($('#wishlist_cart_widget').length > 0) { 2834 2835 if (total_count < 1) { 2836 $('#wishlist_cart_widget').addClass('empty'); 2837 } else { 2838 $('#wishlist_cart_widget').removeClass('empty'); 2839 $('#wishlist_cart_widget').addClass('active'); 2840 } 2841 2842 if ($('#wishlist_cart_widget').hasClass('hide_when_empty') && total_count == 0) { 2843 $('#wishlist_cart_widget').hide(); 2844 } else { 2845 $('#wishlist_cart_widget').fadeOut('fast', function () { 2846 $('.wcw_total_count').html(total_count); 2847 $('#wishlist_cart_widget').fadeIn('fast'); 2848 // $('.scw_total_price_amount').html(sub_total); 2849 }); 2850 } 2851 2852 } 2853 }, 2854 2855 2856 quick_cart_widget_open: function (item_uid, errortype, errormsg) { 2857 if (!($('#sc_contents_container').length || $('#sc_checkout_container').length)) { 2858 var item_uid = item_uid && typeof item_uid != 'undefined' ? item_uid : false;
2859 var errortype = errortype && typeof errortype != 'undefined' ? errortype : false; 2860 var errormsg = errormsg && typeof errormsg != 'undefined' ? errormsg : ''; 2861 2862 $('#wishlist_quick_cart_widget').addClass('active'); 2863 $('body').addClass('sc_wishlist_quick_cart_widget_active'); 2864 $('#wishlist_quick_cart_widget').clearQueue().delay(10).queue(function () { 2865 $(this).addClass('animate').dequeue(); 2866 $('body').addClass('sc_wishlist_quick_cart_widget_animate'); 2867 }); 2868 if (errortype) { 2869 if (errortype == 'max_qty' && item_uid) { 2870 $('#wqcw_items_list li[data-uid="' + item_uid + '"]').addClass('wqcw_max_qty_error'); 2871 $('#wqcw_items_list li[data-uid="' + item_uid + '"] .wqcw_item_error').text(errormsg); 2872 } 2873 } 2874 $('.notify_panel_mask').unbind('click.wishlist_panel').bind('click.wishlist_panel', function () { 2875 if ($('#wishlist_quick_cart_widget').hasClass('active')) { 2876 $('#wishlist_quick_cart_widget .notify_panel_close').trigger('click'); 2877 } 2878 }); 2879 window.cart.wishlist_summary.quick_wishlist_cart_widget_open_callback(); 2880 } 2881 }, 2882 2883 quick_wishlist_cart_widget_open_callback: function () { 2884 try { 2885 h.accessibility.on_popup_opening('#wishlist_quick_cart_widget', false, '#wqcw_close'); 2886 } catch (error) { 2887 2888 } 2889 }, 2890 2891 quick_wishlist_cart_widget_close: function () { 2892 $('#wishlist_quick_cart_widget').removeClass('animate'); 2893 $('body').removeClass('sc_wishlist_quick_cart_widget_animate'); 2894 $('#wishlist_quick_cart_widget').clearQueue().delay(600).queue(function () { 2895 $(this).removeClass('active').dequeue(); 2896 $('body').removeClass('sc_wishlist_quick_cart_widget_active'); 2897 }); 2898 window.cart.wishlist_summary.quick_wishlist_cart_widget_close_callback(); 2899 }, 2900 2901 quick_wishlist_cart_widget_close_callback: function () { 2902 try { 2903 h.accessibility.on_popup_closing(); 2904 } catch (error) { 2905 2906 } 2907 }, 2908 2909 }, 2910 2911 wishlist_forms: { 2912 2913 init: function () { 2914 window.cart.wishlist_forms.enquiry_confirmation(); 2915 if ($('#wishlistEnquiryForm').length) { 2916 $('#wishlist_enquiry_form_submit_button a').click(function () { 2917 if (!$(this).closest('form').hasClass('wl_fields_unconfirmed')) { 2918 $(this).closest('form').submit(); 2919 } else { 2920 window.cart.wishlist_forms.wishlist_enquiry_fields(false, false, true); 2921 h.alert('Please fill out all the required fields and ensure you have entered a valid phone number and email address.<br><br>The phone number must only include numbers and dashes.'); 2922 } 2923 }); 2924 $('input, textarea', '#wishlistEnquiryForm') 2925 .each(function () { 2926 if ($(this).val() == '' && $(this).val() == $(this).attr('default-value')) { 2927 $(this).addClass('sc_field_edited') 2928 } 2929 }) 2930 .focus(function () { 2931 $(this).closest('.form_row').removeClass('sc_field_error error'); 2932 }) 2933 .bind('change', function (e) { 2934 $(this).addClass('sc_field_edited'); 2935 window.cart.wishlist_forms.wishlist_enquiry_fields(false, $(this)); 2936 }) 2937 ; 2938 window.cart.wishlist_forms.wishlist_enquiry_fields(true); 2939 window.cart.wishlist_forms.submit_loader(); 2940 window.cart.wishlist_forms.captcha_init(); 2941 2942 } 2943 2944 }, 2945 2946 enquiry_confirmation: function () { 2947 // The enquiry form posts back to the server rather than submitting over ajax, so the
2948 // confirmation is rendered into the page by wishlist_basket_sidebar.mako and shown from 2949 // here instead - by which point h.alert() (archimedes-frontend-core.js) has loaded. This 2950 // is the same way the contact form reads its confirmation wording from 2951 // #contact_form_overlay (see contact_form_popup.js). 2952 var confirmation = $('#wishlist_enquiry_confirmation').not('.confirmation_shown'); 2953 if (!confirmation.length) { 2954 return; 2955 } 2956 2957 // init() runs again after an ajax page load, so the popup is only shown once 2958 confirmation.addClass('confirmation_shown'); 2959 2960 var heading = confirmation.attr('data-confirmation-heading') || ''; 2961 var message = confirmation.attr('data-confirmation-message') || ''; 2962 if (!heading && !message) { 2963 return; 2964 } 2965 2966 h.alert((heading ? '<h2>' + heading + '</h2>' : '') + message); 2967 }, 2968 2969 submit_loader: function () { 2970 // The submit button is an <input>, so the loader is a sibling of it rather than a child 2971 // and 'loading' goes on the wrapper around the two (see wishlist_basket_sidebar.mako). 2972 // This form posts back to the server, so the loader stays on until the new page arrives - 2973 // it is only turned off again if the captcha stops the post going through (captcha_execute). 2974 // 2975 // Bound before captcha_init's handler so the loader is on while the challenge runs. 2976 $('#wishlistEnquiryForm') 2977 .off('submit.wishlist_loader') 2978 .on('submit.wishlist_loader', function () { 2979 // The spinner itself is only there if misc.loaders() has run, which it has not on 2980 // sites with the core gallery js disabled. Without it 'loading' would just blank 2981 // the button label, so leave the button as it is instead. 2982 if (!$('.wishlist_submit_button .button_loader svg', this).length) { 2983 return; 2984 } 2985 $('.wishlist_submit_button', this).addClass('loading'); 2986 }) 2987 ; 2988 }, 2989 2990 captcha_init: function () { 2991 // The wishlist enquiry form is given the shared 'contact_form_captcha_enabled' class 2992 // when captcha is enabled for it (see wishlist_basket_sidebar.mako). That is the class 2993 // google_captcha_onload() (recaptcha.js) looks for when generating captcha widgets. 2994 var form = $('#wishlistEnquiryForm.contact_form_captcha_enabled'); 2995 if (!form.length) { 2996 return; 2997 } 2998 2999 // Generate the captcha for this form. google_captcha_onload() is only defined when the 3000 // captcha scripts have been included for this site and it skips forms it has already 3001 // initialised, so this is safe to call again after an ajax page load. 3002 if (typeof google_captcha_onload != 'undefined') { 3003 if (typeof Artlogic != 'undefined' && typeof Artlogic.import == 'function') { 3004 // js-modules sites: the recaptcha module loads the reCAPTCHA api itself 3005 Artlogic.import('galleries_js/recaptcha.js').then(function (module) { 3006 google_captcha_onload(); 3007 }).catch(function (err) { 3008 console.error('Wishlist enquiry form: the recaptcha module failed to load', err); 3009 }); 3010 } else { 3011 google_captcha_onload(); 3012 } 3013 } 3014 3015 // Unlike the contact form, this form posts back to the server rather than submitting 3016 // over ajax, so the challenge has to be completed before the post is allowed through. 3017 form.off('submit.wishlist_captcha').on('submit.wishlist_captcha', function (e) { 3018 var instance = $(this); 3019 if ($('input[name="g-recaptcha-response"]', instance).val()) { 3020 // The challenge has already been completed, let the form post 3021 return true; 3022 } 3023 if (instance.hasClass('contact_form_captcha_visible')) { 3024 // A visible captcha is completed by the user themselves. The token is verified 3025 // on the server, so there is nothing to execute here. 3026 return true; 3027 } 3028 e.preventDefault(); 3029 window.cart.wishlist_forms.captcha_execute(instance); 3030 return false;
3031 }); 3032 3033 }, 3034 3035 captcha_execute: function (instance) { 3036 var captcha_error = '<h2>Sorry</h2>An error occurred, please refresh this page and try again. If you continue to have problems please contact us for support.'; 3037 3038 if (typeof grecaptcha == 'undefined') { 3039 h.alert(captcha_error); 3040 $('.wishlist_submit_button', instance).removeClass('loading'); 3041 return; 3042 } 3043 3044 window.recaptcha_success_callback_instance = instance; 3045 window.recaptcha_success_callback = function (instance) { 3046 // The token has been set on the form's hidden field by the captcha callback, so the 3047 // form can now be posted. The native submit is used to avoid running this handler again. 3048 $(instance)[0].submit(); 3049 }; 3050 3051 try { 3052 grecaptcha.execute(); 3053 } catch (error_message) { 3054 h.alert(captcha_error); 3055 $('.wishlist_submit_button', instance).removeClass('loading'); 3056 $.ajax({ 3057 url: "/api/process_js_error/", 3058 data: 'type=captcha&message=Captcha error on the wishlist enquiry form on page ' + window.location.pathname + ' Error message: ' + error_message + '&fullurl=' + window.location.href, 3059 cache: false 3060 }); 3061 } 3062 3063 }, 3064 3065 wishlist_enquiry_fields: function (first_load, changed_field, all_fields) { 3066 if ($('#wishlistEnquiryForm').length) { 3067 additional_fields_verified = true; 3068 additional_fields_errors = []; 3069 all_fields = all_fields && typeof all_fields != 'undefined' ? true : false; 3070 $('#wishlistEnquiryForm').each(function () { 3071 $('input, textarea', this).each(function () { 3072 if ($(this).hasClass('requiredField')) { 3073 if ($(this).hasClass('sc_field_edited') || all_fields) { 3074 if ($(this).val() == '' || $(this).val() == $(this).attr('default-value')) { 3075 additional_fields_errors.push($(this).attr('name')); 3076 } 3077 } 3078 } 3079 if ($(this).attr('name') == 'f_email') { 3080 if (!window.cart.validate_fields.email($(this).val())) { 3081 additional_fields_verified = false; 3082 if ($(this).hasClass('sc_field_edited') || all_fields) { 3083 additional_fields_errors.push($(this).attr('name')); 3084 } 3085 } 3086 } 3087 if ($(this).attr('name') == 'f_phone') { 3088 if (!window.cart.validate_fields.phone($(this).val())) { 3089 additional_fields_verified = false; 3090 if ($(this).hasClass('sc_field_edited') || all_fields) { 3091 additional_fields_errors.push($(this).attr('name')); 3092 } 3093 } 3094 } 3095 }); 3096 }) 3097 .promise() 3098 .done(function () { 3099 if (additional_fields_verified && additional_fields_errors.length == 0) { 3100 cart_user_email_has_changed = false; 3101 $('#wishlistEnquiryForm').removeClass('wl_fields_unconfirmed'); 3102 $('input, textarea', '#wishlistEnquiryForm').removeClass('sc_field_error error'); 3103 } else { 3104 $('#wishlistEnquiryForm').addClass('wl_fields_unconfirmed'); 3105 $('#wishlistEnquiryForm .form_row').removeClass('sc_field_error error'); 3106 if (!first_load) { 3107 $.each(additional_fields_errors, function (index, field_name) { 3108 var associated_field = $('#wishlistEnquiryForm input[name="' + field_name + '"]'); 3109 associated_field.closest('.form_row').addClass('sc_field_error error'); 3110 }); 3111 } 3112 3113 } 3114 }); 3115 } 3116 } 3117 3118 }, 3119 3120 update_wishlist_row: function (uid, total_count, forced) { 3121 var forced = forced && typeof forced != 'undefined' ? forced : false;
3122 3123 $('.wishlist_button[data-uid="' + uid + '"]').each(function () { 3124 if (total_count > 0) { 3125 $(this).addClass('active'); 3126 } else { 3127 var $that = $(this); 3128 setTimeout(function () { 3129 $that.removeClass('active'); 3130 if (!forced) { 3131 $that.find('.add_to_wishlist').focus(); 3132 } 3133 }, 150); 3134 } 3135 $('.store_item_remove_container', this).each(function () { 3136 if (total_count > 0) { 3137 $(this).delay(250).fadeIn(); 3138 } else { 3139 $(this).fadeOut(); 3140 } 3141 }); 3142 $('.store_item_total', this).each(function () { 3143 $('.store_item_total_number', this).html(total_count); 3144 }); 3145 }); 3146 }, 3147 3148 update_wishlist_uids: function (action, uid) { 3149 }, 3150 3151 remove_all_wishlist_items: function (remove_button, reload) { 3152 var item_uid; 3153 remove_button.unbind().click(function () { 3154 3155 if ($(this).closest('.wishlist_button').length > 0) { 3156 parent_row = $(this).closest('.wishlist_button'); 3157 } else if ($(this).closest('#wishlist_quick_cart_widget').length > 0) { 3158 parent_row = $('#wqcw_items'); 3159 } else { 3160 item_uid = $(this).attr('data-uid'); 3161 parent_row = false; 3162 } 3163 3164 parent_row_delay = $(this).closest('#wishlist_quick_cart_widget').attr('data-remove-delay') ? $(this).closest('#wishlist_quick_cart_widget').attr('data-remove-delay') : false; 3165 3166 $.post('/cart/remove_all_wishlist_items/', {}, function (obj) { 3167 if (reload == 'reload') { 3168 window.cart.wishlist_summary.update(obj.total_count, 'remove', obj.uid); 3169 window.cart.update_wishlist_row(obj.uid, obj.item_count); 3170 window.location.pathname = window.location.pathname; 3171 } else { 3172 remove_from_wishlist_after = function () { 3173 3174 var wishlist_uids = localStorage.getItem('wishlist_uids') ? JSON.parse(localStorage.getItem('wishlist_uids')) : false; 3175 if (wishlist_uids && typeof wishlist_uids != 'undefined') { 3176 $.each(wishlist_uids, function (i, uid) { 3177 window.cart.wishlist_summary.update(0, 'remove', uid); 3178 window.cart.update_wishlist_row(uid, 0, true); 3179 }); 3180 } 3181 3182 3183 localStorage.setItem('wishlist_uids', ''); 3184 window.cart.wishlist_summary.get_summary(false); 3185 } 3186 3187 if (parent_row && parent_row_delay) { 3188 $(parent_row) 3189 .addClass('hide') 3190 .delay(parseInt(parent_row_delay) / 2) 3191 .queue(function () { 3192 remove_from_wishlist_after(); 3193 $(this).dequeue(); 3194 var instance = $(this); 3195 setTimeout(function () { 3196 instance.removeClass('hide') 3197 }, 500, instance); 3198 }) 3199 ; 3200 } else { 3201 remove_from_wishlist_after(); 3202 } 3203 } 3204 }, 'json'); 3205 3206 return false; 3207 }); 3208 }, 3209 3210 remove_from_wishlist: function (remove_button, reload) { 3211 var item_uid; 3212 3213 remove_button.unbind().click(function () { 3214 // $('body').delegate(remove_button.selector, 'click', function() { 3215 // $('body').off('click.remove_from_wl_'+remove_button.selector).on('click.remove_from_wl_'+remove_button.selector, remove_button.selector, function(event) { 3216 var element = $(event.target); 3217 3218 // For accessibility - tracks which element to refocus on 3219 try { 3220 h.accessibility.global_variables.element_to_refocus_to = element; 3221 } catch (error) { 3222 console.error(error); 3223 } 3224 3225 if (element.closest('.wishlist_button').length > 0) { 3226 item_uid = element.closest('.wishlist_button').attr('data-uid'); 3227 parent_row = element.closest('.wishlist_button'); 3228 } else if (element.closest('.product_detail').length > 0) { 3229 item_uid = element.closest('.product_detail').attr('data-uid'); 3230 parent_row = element.closest('.product_detail'); 3231 } else { 3232 item_uid = element.attr('data-uid'); 3233 parent_row = false;
3234 } 3235 3236 parent_row_delay = element.closest('#wishlist_quick_cart_widget').attr('data-remove-delay') ? element.closest('#wishlist_quick_cart_widget').attr('data-remove-delay') : false; 3237 3238 $.post('/cart/remove_wishlist_item/', {'unique_id': item_uid}, function (obj) { 3239 if (reload == 'reload') { 3240 window.cart.wishlist_summary.update(obj.total_count, 'remove', obj.uid); 3241 window.cart.update_wishlist_row(obj.uid, obj.item_count); 3242 window.location.pathname = window.location.pathname; 3243 } else { 3244 remove_from_cart_after = function () { 3245 window.cart.wishlist_summary.get_summary(false); 3246 window.cart.wishlist_summary.update(obj.total_count, 'remove', obj.uid); 3247 window.cart.update_wishlist_row(obj.uid, obj.item_count, true); 3248 } 3249 if (parent_row && parent_row_delay) { 3250 $(parent_row) 3251 .addClass('hide') 3252 .delay(parent_row_delay) 3253 .queue(function () { 3254 remove_from_cart_after(); 3255 $(this).dequeue(); 3256 }) 3257 ; 3258 } else { 3259 remove_from_cart_after(); 3260 } 3261 } 3262 }, 'json'); 3263 3264 return false; 3265 }); 3266 }, 3267 3268 remove_from_wishlist_summary_page: function () { 3269 if ($('#wishlist_information .remove').length) { 3270 window.cart.remove_from_wishlist($('#wishlist_information .remove'), 'reload'); 3271 } 3272 }, 3273 enable_framing_option: function () { 3274 $(".extra-option-enabled .item-without-extra").addClass("switch-on"); 3275 //remove same item in the cart when click, for extra-option-page buttons only 3276 $(".extra-option-enabled .store_item_add_to_cart").click(function(){ 3277 $('.extra-option-enabled .store-item-switch:not(.switch-on) .store_item').each(function () { 3278 $(this).find('.store_item_remove_from_cart').trigger('click'); 3279 }); 3280 }) 3281 //class on / off in radio click 3282 $('.framing-option-container .store-item-extras-radio').unbind().bind().change(function (event) { 3283 //current radio row index: 3284 //console.log("click option"); 3285 click_index = $(this).parent().index() 3286 $(".framing-option-container .store-item-extras-option").removeClass('active'); 3287 $(this).parent().addClass('active'); 3288 3289 $(".extra-option-enabled .item-with-extra").removeClass('active'); 3290 $(".extra-option-enabled .item-with-extra").removeClass('switch-on'); 3291 $(".extra-option-enabled .item-without-extra").hide(); 3292 $(".extra-option-enabled .item-without-extra").removeClass("switch-on"); 3293 if ($(this).val() == 'no-frame') { 3294 //for no-frame option 3295 $(".extra-option-enabled .item-without-extra").show(); 3296 $(".extra-option-enabled .item-without-extra").addClass('switch-on') 3297 } else { 3298 //for other option 3299 //version 1: 3300 //$(".store_item input[name=extras_option][value='"+$(this).val()+"']").parent().parent().addClass("active switch-on"); 3301 //version 2: 3302 //console.log("click index = " + click_index); 3303 $(".extra-option-enabled .store-item-switch").eq((click_index - 1)).addClass("active switch-on"); 3304 } 3305 3306 }); 3307 3308 } 3309 3310 }; 3311 3312})(jQuery); 3313 3314 3315has_localstorage = false; 3316$(document).ready(function () { 3317 3318 window.cart.init(); 3319 3320 // set TR with ID values based on index 3321 $('.product_detail').each(function () { 3322 $(this).attr('id', $(this).index() - 1); 3323 }); 3324 3325 // Change Quantity 3326 3327 $('.qty').keyup(function () { 3328 var row_id = $(this).parent().parent().attr('id'); 3329 var qty = $(this).val(); 3330 $.post('/cart/qty/', {'row_id': row_id, 'qty_value': qty}, function (data) { 3331 update_cart_qty(); 3332 if (parseInt(data) == 0) { 3333 $('#shopping_cart_container').html('<p>Your shopping basket empty.</p>'); 3334 } 3335 }); 3336 if (qty == 0) { 3337 $('#' + row_id).hide(); 3338 } 3339 }); 3340 3341}); 3342 3343 3344function update_cart_qty() { 3345 var count = 0 3346 $.post('/cart/count/', function (data) { 3347 $('.shopping_cart .items_count').html(data); 3348 count = data 3349 }, 'html'); 3350 return parseInt(count) 3351}
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.