Snippets
Created by
Oscar García Arenas
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | <?php
/**
* Init hooks on the front-end init.
*/
function wcpbc_switcher_flags_init() {
add_action( 'wp_enqueue_scripts', 'wcpbc_load_switcher_flags_scripts', 20 );
add_action( 'wp_footer', 'wcpbc_switcher_flags_script', 20 );
}
add_action( 'wc_price_based_country_before_frontend_init', 'wcpbc_switcher_flags_init' );
/**
* Enqueue required scripts and styles on the front-end init.
*/
function wcpbc_load_switcher_flags_scripts() {
/**
* SelectWoo (Select2).
*
* @see https://select2.org/
*/
wp_enqueue_script( 'selectWoo' );
wp_enqueue_style( 'select2' );
/**
* World flags.
*
* @see https://github.com/lafeber/world-flags-sprite
*/
wp_enqueue_style( 'world-flags-sprite', '//github.com/downloads/lafeber/world-flags-sprite/flags16.css', array(), '0.0.2' );
}
/**
* Outpus the script that initializes the select2
*/
function wcpbc_switcher_flags_script() {
?>
<script>
;( function( $ ) {
var switcher_flags = {
template: function( data ) {
if ( ! data.id ) {
return data.text;
}
var flag = '';
var currency = $(data.element).data('currency');
if (typeof currency !== 'undefined' && currency ) {
if ('EUR' === currency) {
flag = '_European_Union';
} else if( 'X' !== currency.substring(0,1) ) {
flag = currency.toLowerCase().substring(0,2);
}
}
if ( ! flag ) {
flag = 'ca' === data.id.toLowerCase() ? data.id.toLowerCase() : 'us';
}
var $element = $( '<span><i class="flag" style="vertical-align: sub;margin-right: 6px;"></i><span></span></span>' );
$element.find('i.flag').addClass(flag);
$element.find('span').text(data.text);
return $element;
},
selectionTemplate: function( data ) {
var el = switcher_flags.template( data );
return el.html();
},
afterSelectionTemplate: function() {
$('.wc-price-based-country .select2-selection__rendered').each( function(){
var text = $(this).text();
if ( text.indexOf('span>') > 0 ) {
$(this).html( text );
}
});
},
init_select2: function() {
$('select.wcpbc-currency-switcher,select.wcpbc-country-switcher').select2({
dropdownCssClass: 'f16 :all: wcpbc-switcher',
containerCssClass: 'f16',
templateResult: switcher_flags.template,
templateSelection: switcher_flags.selectionTemplate,
minimumResultsForSearch: Infinity,
width: 'auto'
});
switcher_flags.afterSelectionTemplate();
$('select.wcpbc-currency-switcher,select.wcpbc-country-switcher').on('change', function(){
switcher_flags.afterSelectionTemplate();
});
},
init: function() {
$(document).ready(switcher_flags.init_select2);
/*
* After AJAX geolocation.
*/
$(document.body).on('wc_price_based_country_after_ajax_geolocation', switcher_flags.init_select2);
}
};
switcher_flags.init();
})( jQuery );
</script>
<?php
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.