FacetWP – currency switcher support for WooCommerce (#13807)

mgibbs189

{{gist file=”functions.php” lang=”php”}}
<?php

/**
* Facetwp currency switch – Ferenc Farkas – Vitamin360, a.s.
*/

function fwp_slider_set_label() {
global $woocommerce_wpml;

$currency_code = get_woocommerce_currency();
$symbol = get_woocommerce_currency_symbol( $currency_code );
$exchange_rate = $woocommerce_wpml->multi_currency->exchange_rate_services->get_currency_rate($currency_code);
?>
<script>
(function($) {
$(document).on(‘facetwp-loaded’, function() {
FWP.hooks.addAction(‘facetwp/set_label/slider’, function($this) {
var exchange_rate = <?php echo $exchange_rate; ?>;
var symbol = ‘<?php echo $symbol; ?>’;
var currency_code = ‘<?php echo $currency_code; ?>’;
var facet_name = ‘price’;
var min = FWP.settings[facet_name][‘lower’] * exchange_rate * 1.2;
var max = FWP.settings[facet_name][‘upper’] * exchange_rate * 1.2;
var format = FWP.settings[facet_name][‘format’];
var opts = {
decimal_separator: FWP.settings[facet_name][‘decimal_separator’],
thousands_separator: FWP.settings[facet_name][‘thousands_separator’]
};
if ( min === max ) {
if (currency_code != ‘HUF’){
var label = symbol
+ nummy(min).format(format, opts)
+ FWP.settings[facet_name][‘suffix’];
}else {
var label = nummy(min).format(‘0’, opts)
+ FWP.settings[facet_name][‘suffix’]
+ ‘ ‘ + symbol;
}
}
else {
if (currency_code != ‘HUF’){
var label = symbol
+ nummy(min).format(format, opts)
+ FWP.settings[facet_name][‘suffix’]
+ ‘ — ‘
+ symbol
+ nummy(max).format(format, opts)
+ FWP.settings[facet_name][‘suffix’];
} else {
var label = nummy(min).format(‘0’, opts)
+ FWP.settings[facet_name][‘suffix’] + ‘ ‘
+ symbol
+ ‘ — ‘
+ nummy(max).format(‘0’, opts)
+ FWP.settings[facet_name][‘suffix’] + ‘ ‘ + symbol;
}
}
$this.find(‘.facetwp-slider-label’).html(label);
});
});
})(jQuery);
</script>
<?php
}
add_action( ‘wp_head’, ‘fwp_slider_set_label’, 999 );

{{/gist}}