admin-currency-selector.js
1 year ago
admin-currency-selector.min.js
1 year ago
bacs-accounts-currencies.js
5 years ago
bacs-accounts-currencies.min.js
5 years ago
cart_widget.js
2 months ago
cart_widget.min.js
2 months ago
currency-switcher-settings.js
1 year ago
currency-switcher-settings.min.js
1 year ago
dialogs.js
5 years ago
dialogs.min.js
5 years ago
exchange-rates.js
3 years ago
exchange-rates.min.js
3 years ago
files.js
5 years ago
files.min.js
5 years ago
languages_notice.js
5 years ago
languages_notice.min.js
5 years ago
lock_fields.js
2 years ago
lock_fields.min.js
2 years ago
multi-currency.js
5 years ago
multi-currency.min.js
5 years ago
pointer.js
1 year ago
pointer.min.js
1 year ago
prices.js
5 years ago
prices.min.js
5 years ago
products-screen-option.js
5 years ago
products-screen-option.min.js
5 years ago
scripts.js
1 year ago
scripts.min.js
1 year ago
taxonomy_translation.js
1 year ago
taxonomy_translation.min.js
1 year ago
tooltip_init.js
5 years ago
tooltip_init.min.js
5 years ago
trnsl_interface_dialog_warning.js
5 years ago
trnsl_interface_dialog_warning.min.js
5 years ago
troubleshooting.js
1 year ago
troubleshooting.min.js
1 year ago
wcml-attributes.js
5 years ago
wcml-attributes.min.js
5 years ago
wcml-messages.js
5 years ago
wcml-messages.min.js
5 years ago
wcml-multi-currency.js
2 months ago
wcml-multi-currency.min.js
2 months ago
wcml-setup.js
3 years ago
wcml-setup.min.js
3 years ago
wcml-translation-editor.js
4 weeks ago
wcml-translation-editor.min.js
4 weeks ago
widgets.js
5 years ago
widgets.min.js
5 years ago
wpml_tm.js
5 years ago
wpml_tm.min.js
5 years ago
wcml-multi-currency.js
106 lines
| 1 | document.addEventListener('DOMContentLoaded', function() { |
| 2 | document.addEventListener('click', wcml_switch_currency_handler); |
| 3 | }); |
| 4 | |
| 5 | function wcml_switch_currency_handler(event) { |
| 6 | var eventTargetElement = event.target; |
| 7 | |
| 8 | if (eventTargetElement.matches('.wcml_currency_switcher a, .wcml_currency_switcher a *')) { |
| 9 | event.preventDefault(); |
| 10 | var targetElement = eventTargetElement.closest('.wcml_currency_switcher a'); |
| 11 | |
| 12 | if (targetElement.disabled || |
| 13 | targetElement.parentElement.classList.contains('wcml-cs-active-currency') || |
| 14 | targetElement.classList.contains('wcml-cs-active-currency')) { |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | document.removeEventListener('click', wcml_switch_currency_handler); |
| 19 | wcml_load_currency(targetElement.getAttribute('rel')); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | function wcml_load_currency(currency, force_switch) { |
| 24 | force_switch = force_switch || 0; |
| 25 | |
| 26 | var ajax_loader = document.createElement('img'); |
| 27 | ajax_loader.className = 'wcml-spinner'; |
| 28 | ajax_loader.width = 16; |
| 29 | ajax_loader.height = 16; |
| 30 | ajax_loader.src = wcml_mc_settings.wcml_spinner; |
| 31 | |
| 32 | var switchers = document.querySelectorAll('.wcml_currency_switcher'); |
| 33 | switchers.forEach(function(switcher) { |
| 34 | switcher.appendChild(ajax_loader.cloneNode(true)); |
| 35 | }); |
| 36 | |
| 37 | var formData = new FormData(); |
| 38 | formData.append('action', 'wcml_switch_currency'); |
| 39 | formData.append('currency', currency); |
| 40 | formData.append('force_switch', force_switch); |
| 41 | formData.append('params', window.location.search.substr(1)); |
| 42 | |
| 43 | var xhr = new XMLHttpRequest(); |
| 44 | xhr.open('POST', woocommerce_params.ajax_url); |
| 45 | xhr.responseType = 'json'; |
| 46 | xhr.onload = function() { |
| 47 | var response = xhr.response; |
| 48 | |
| 49 | if (response.error) { |
| 50 | alert(response.error); |
| 51 | } else if (response.data && response.data.prevent_switching) { |
| 52 | wcml_insert_ajax_html_response_in_dom(response.data.prevent_switching); |
| 53 | } else { |
| 54 | var target_location = window.location.href; |
| 55 | if (target_location.includes('#') || wcml_mc_settings.cache_enabled) { |
| 56 | var url_dehash = target_location.split('#'); |
| 57 | var hash = url_dehash.length > 1 ? '#' + url_dehash[1] : ''; |
| 58 | |
| 59 | target_location = url_dehash[0].replace(/&wcmlc(\=[^&]*)?(?=&|$)|wcmlc(\=[^&]*)?(&|$)/, '').replace(/\?$/, ''); |
| 60 | var url_glue = target_location.includes('?') ? '&' : '?'; |
| 61 | target_location += url_glue + 'wcmlc=' + currency + hash; |
| 62 | } |
| 63 | |
| 64 | wcml_reset_cart_fragments(); |
| 65 | |
| 66 | window.location = wcml_maybe_adjust_widget_price(target_location, response.data);; |
| 67 | } |
| 68 | }; |
| 69 | xhr.send(formData); |
| 70 | } |
| 71 | |
| 72 | function wcml_insert_ajax_html_response_in_dom(html) { |
| 73 | // Insert the HTML fragment without the script |
| 74 | const div = document.createElement('div'); |
| 75 | div.innerHTML = html; |
| 76 | const script = div.getElementsByTagName('script')[0]; |
| 77 | const scriptContent = script.innerText || script.textContent; |
| 78 | |
| 79 | // Remove the script tags from the div and grab their content |
| 80 | script.parentNode.removeChild(script); |
| 81 | |
| 82 | // Now insert the cleaned HTML |
| 83 | document.body.insertAdjacentHTML('beforeend', div.innerHTML); |
| 84 | |
| 85 | // Create a new script element and add the extracted script content |
| 86 | const scriptToExecute = document.createElement('script'); |
| 87 | scriptToExecute.type = 'text/javascript'; |
| 88 | scriptToExecute.id = 'wcml-cart-dialog-script'; |
| 89 | scriptToExecute.appendChild(document.createTextNode(scriptContent)); |
| 90 | |
| 91 | // Append the new script element to the document so it will execute |
| 92 | document.body.appendChild(scriptToExecute); |
| 93 | } |
| 94 | |
| 95 | function wcml_maybe_adjust_widget_price(target_location, response) { |
| 96 | if (response.min_price) { |
| 97 | target_location = target_location.replace(/(min_price=)(\d+)/, "$1" + response.min_price); |
| 98 | } |
| 99 | |
| 100 | if (response.max_price) { |
| 101 | target_location = target_location.replace(/(max_price=)(\d+)/, "$1" + response.max_price); |
| 102 | } |
| 103 | |
| 104 | return target_location; |
| 105 | } |
| 106 |