1/** 2 * Currency Switcher for WooCommerce â Frontend JS 3 * 4 * Handles currency selection via dropdown or inline list. 5 * Modes (from localized switchingMethod): 6 * ajax â POST to set currency (REST preferred; admin-ajax fallback), then reload without fmecs_* query args. 7 * query_string â add fmecs_currency to URL and navigate (CurrencyEngine reads GET). 8 * 9 * Localized object: fmecs_localized_data { ajaxUrl, setCurrencyUrl, nonce, switchingMethod } 10 */ 11/* global fmecs_localized_data */ 12jQuery( function ( $ ) { 13 'use strict'; 14 15 var L = fmecs_localized_data; 16 var switchingMethod = L.switchingMethod || 'ajax'; 17 18 /** 19 * Navigate with fmecs_currency in the query string (CurrencyEngine reads GET and sets cookie). 20 * 21 * @param {string} currency Three-letter currency code. 22 */ 23 function fmecsNavigateWithQueryString( currency ) { 24 if ( ! L.nonce ) { 25 return; 26 } 27 try { 28 var u = new URL( window.location.href ); 29 u.searchParams.set( 'fmecs_currency', currency ); 30 u.searchParams.set( 'fmecs_cur_nonce', L.nonce ); 31 window.location.href = u.toString(); 32 } catch ( e ) { 33 var href = window.location.href; 34 var sep = href.indexOf( '?' ) >= 0 ? '&' : '?'; 35 window.location.href = href + sep + 'fmecs_currency=' + encodeURIComponent( currency ) + '&fmecs_cur_nonce=' + encodeURIComponent( L.nonce ); 36 } 37 } 38 39 /** 40 * Send the selected currency to the server (AJAX mode), then reload the page. 41 * 42 * @param {string} currency Three-letter currency code. 43 */ 44 function fmecsReloadWithoutFmecsParams() { 45 try { 46 var u = new URL( window.location.href ); 47 u.searchParams.delete( 'fmecs_currency' ); 48 u.searchParams.delete( 'fmecs_cur_nonce' ); 49 window.location.href = u.toString(); 50 } catch ( e ) { 51 window.location.reload(); 52 } 53 } 54 55 function fmecsSetCurrency( currency ) { 56 if ( ! currency ) return; 57 58 if ( switchingMethod === 'query_string' ) { 59 fmecsNavigateWithQueryString( currency ); 60 return; 61 } 62 63 var switchUrl = ( L.setCurrencyUrl && String( L.setCurrencyUrl ).length ) ? L.setCurrencyUrl : L.ajaxUrl; 64 var postData = { nonce: L.nonce, currency: currency }; 65 if ( switchUrl === L.ajaxUrl ) { 66 postData.action = 'fmecs_set_currency'; 67 } 68 69 $.ajax( { 70 url: switchUrl, 71 type: 'POST', 72 data: postData, 73 success: function () { 74 window.onbeforeunload = null; 75 fmecsReloadWithoutFmecsParams(); 76 }, 77 error: function () { 78 window.onbeforeunload = null; 79 fmecsNavigateWithQueryString( currency ); 80 } 81 } ); 82 } 83 84 // ââ Dropdown switcher ââââââââââââââââââââââââââââââââââââââââââââââââââââ 85 $( document ).on( 'change', '.fmecs-currency-switcher-select', function () { 86 fmecsSetCurrency( $( this ).val() ); 87 } ); 88 89 // ââ List/link switcher âââââââââââââââââââââââââââââââââââââââââââââââââââ 90 $( document ).on( 'click', '.fmecs-currency-list a', function ( e ) { 91 e.preventDefault(); 92 fmecsSetCurrency( $( this ).data( 'currency' ) ); 93 } ); 94 95} );
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.