Skip to content

Commit

Permalink
* Dev - Added in an option to convert all multiple currencies to your…
Browse files Browse the repository at this point in the history
… base currency on the frontend.
  • Loading branch information
krugazul committed Jul 23, 2019
1 parent d62264d commit 73c3572
Show file tree
Hide file tree
Showing 8 changed files with 1,166 additions and 39 deletions.
148 changes: 147 additions & 1 deletion assets/js/lsx-currencies-admin.min.js
Original file line number Diff line number Diff line change
@@ -1 +1,147 @@
jQuery(document).ready(function(){jQuery(document).on("change",".lsx-select-trigger select",function(e){e.preventDefault(),e.stopPropagation();var a=jQuery(this),t=a.attr("name"),i=a.val();jQuery('[data-trigger="'+t+'"] li.hidden input[checked="checked"]').removeAttr("checked").parents("li").show().removeClass("hidden"),jQuery('[data-trigger="'+t+'"] input[name="additional_currencies['+i+']"]').attr("checked","checked").parents("li").hide().addClass("hidden")}),void 0===window.lsx_thumbnail_subtabs_nav&&(jQuery(document).on("click",".ui-tab-nav a",function(e){e.preventDefault(),e.stopPropagation();var a=jQuery(this);return jQuery(".ui-tab-nav a.active").removeClass("active"),a.addClass("active"),jQuery(".ui-tab.active").removeClass("active"),a.closest(".uix-field-wrapper").find(a.attr("href")).addClass("active"),!1}),window.lsx_thumbnail_subtabs_nav=!0)});
var lsx_money = fx.noConflict();

LSX_Currencies = {
initThis: function() {
if ( '1' === lsx_currencies_params.script_debug ) {
console.log('[LSX_Currencies.switchCurrency] lsx_currencies_params:');
console.log(lsx_currencies_params);
}

if ('' === lsx_currencies_params.rates) {
return;
}

//Set the money rates and the base, we will always be converting first from the base.
lsx_money.rates = lsx_currencies_params.rates;
lsx_money.base = 'USD';

this.current_currency = lsx_currencies_params.current_currency;

//If the user has a previous selection, then change the amounts to that base
if (this.current_currency !== lsx_currencies_params.base || '1' === lsx_currencies_params.convert_to_single ) {
this.checkAmounts(lsx_currencies_params.base);
}

this.watchMenuSwitcher();
},

checkAmounts: function(from) {
var $this = this;

jQuery('.amount.lsx-currencies').each(function() {

var amount = '',
new_price = '',
strict_amount = '';
base_currency = lsx_currencies_params.base;

if ( '1' === lsx_currencies_params.script_debug ) {
base_currency = $this.findAvailableCurrency( base_currency, this );
}

if ( jQuery( this ).hasClass( 'woocommerce-Price-amount') ) {
strict_amount = jQuery(this).attr('data-price-' + $this.current_currency);
amount = jQuery(this).attr('data-price-' + base_currency );
} else {
strict_amount = jQuery(this).find('.value').attr('data-price-' + $this.current_currency);
amount = jQuery(this).find('.value').attr('data-price-' + base_currency );
}

if (typeof strict_amount !== typeof undefined && strict_amount !== false && '0.00' !== strict_amount) {
new_price = strict_amount;
} else {
new_price = $this.switchCurrency(base_currency, $this.current_currency, amount );
}

if ( jQuery( this ).hasClass( 'woocommerce-Price-amount') ) {

var currency_symbol = $this.current_currency;
if ( undefined !== lsx_currencies_params.currency_symbols[ $this.current_currency ] ) {
currency_symbol = lsx_currencies_params.currency_symbols[ $this.current_currency ];
}

var currency_span = '<span class="woocommerce-Price-currencySymbol">' + currency_symbol + '</span>' + new_price;
jQuery(this).html(currency_span);
} else {
jQuery(this).find('.value').html(new_price);
jQuery(this).find('.currency-icon').prop('class', '').addClass('currency-icon').addClass($this.current_currency.toLowerCase()).html($this.current_currency);

}

});
},

switchCurrency: function(from, to, amount) {
if ( '1' === lsx_currencies_params.script_debug ) {
console.log('[LSX_Currencies.switchCurrency] from: ' + from);
console.log('[LSX_Currencies.switchCurrency] to: ' + to);
}

//If the current from price is not the base
amount = lsx_money(amount).from(from).to(to);
amount = this.formatAmount(amount);
return amount;
},

formatAmount: function(amount) {
amount = accounting.formatNumber(amount, 2, ',', '.');
return amount;
},

watchMenuSwitcher: function() {
var $this = this;

jQuery('.menu-item-currency a').on('click',function(event) {
event.preventDefault();
from = $this.current_currency;
var currency_class = jQuery(this).attr('href').replace('#', '');
$this.current_currency = currency_class.toUpperCase();

//Find the UL submenu from which ever button was clicked, and insert a new currency option.
var selector = '';

if (!jQuery(this).hasClass('current')) {
//Check if the currency flag should display
var currency_flag = '';

if (true == lsx_currencies_params.flags) {
currency_flag = '<span class="flag-icon flag-icon-' + currency_class.substring(0, 2) + '"></span> ';
}

//move the new labels up to the current selector
jQuery(this).parents('li.menu-item-currency-current').find('a.current').attr('href', '#' + currency_class).html(currency_flag + $this.current_currency + '<span class="currency-icon ' + currency_class + '"></span><span class="caret"></span>');

//show the old selection from the drop down and
jQuery(this).parents('li.menu-item-currency-current').find('li.hidden').show().removeClass('hidden');

//Hide the new one
jQuery(this).parent().hide().addClass('hidden');
}

//Set the COokie with your currency selection
Cookies.set('lsx_currencies_choice', $this.current_currency);

//Cycle through the divs and convert the amounts.
$this.checkAmounts(from, $this.current_currency);
});
},

menuLabelToggle: function(amount) {
amount = accounting.formatNumber(amount, 2, ',', '.');
return amount;
},

findAvailableCurrency: function( base_currency, current_selector ) {
for (const [key, value] of Object.entries(lsx_currencies_params.currency_symbols)) {
strict_amount = jQuery( current_selector ).find('.value').attr('data-price-' + key);
if ( undefined !== strict_amount && '0.00' !== strict_amount ) {
base_currency = key;
}
}
return base_currency;
}
};

jQuery(document).ready( function() {
LSX_Currencies.initThis();
});
48 changes: 31 additions & 17 deletions assets/js/src/lsx-currencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ var lsx_money = fx.noConflict();

LSX_Currencies = {
initThis: function() {
if ('' === lsx_currencies_params.rates) {
return;
if ( '1' === lsx_currencies_params.script_debug ) {
console.log('[LSX_Currencies.switchCurrency] lsx_currencies_params:');
console.log(lsx_currencies_params);
}

console.log( lsx_currencies_params );
if ('' === lsx_currencies_params.rates) {
return;
}

//Set the money rates and the base, we will always be converting first from the base.
lsx_money.rates = lsx_currencies_params.rates;
Expand All @@ -15,36 +18,39 @@ LSX_Currencies = {
this.current_currency = lsx_currencies_params.current_currency;

//If the user has a previous selection, then change the amounts to that base
if (this.current_currency != lsx_currencies_params.base) {
if (this.current_currency !== lsx_currencies_params.base || '1' === lsx_currencies_params.convert_to_single ) {
this.checkAmounts(lsx_currencies_params.base);
}

this.watchMenuSwitcher();
},

checkAmounts: function(from) {
console.log('[LSX_Currencies.checkAmounts] from: ' + from);

var $this = this;

jQuery('.amount.lsx-currencies').each(function() {

var amount = '',
new_price = '',
strict_amount = '';
strict_amount = '';
base_currency = lsx_currencies_params.base;

if ( '1' === lsx_currencies_params.script_debug ) {
base_currency = $this.findAvailableCurrency( base_currency, this );
}

if ( jQuery( this ).hasClass( 'woocommerce-Price-amount') ) {
strict_amount = jQuery(this).attr('data-price-' + $this.current_currency);
amount = jQuery(this).attr('data-price-' + lsx_currencies_params.base);
amount = jQuery(this).attr('data-price-' + base_currency );
} else {
strict_amount = jQuery(this).find('.value').attr('data-price-' + $this.current_currency);
amount = jQuery(this).find('.value').attr('data-price-' + lsx_currencies_params.base);
amount = jQuery(this).find('.value').attr('data-price-' + base_currency );
}

if (typeof strict_amount !== typeof undefined && strict_amount !== false) {
if (typeof strict_amount !== typeof undefined && strict_amount !== false && '0.00' !== strict_amount) {
new_price = strict_amount;
} else {
new_price = $this.switchCurrency(lsx_currencies_params.base, $this.current_currency, amount );
new_price = $this.switchCurrency(base_currency, $this.current_currency, amount );
}

if ( jQuery( this ).hasClass( 'woocommerce-Price-amount') ) {
Expand All @@ -66,15 +72,14 @@ LSX_Currencies = {
},

switchCurrency: function(from, to, amount) {
/*console.log('[LSX_Currencies.switchCurrency] from: ' + from);
console.log('[LSX_Currencies.switchCurrency] to: ' + to);
console.log('[LSX_Currencies.switchCurrency] lsx_currencies_params:');
console.log(lsx_currencies_params);*/
if ( '1' === lsx_currencies_params.script_debug ) {
console.log('[LSX_Currencies.switchCurrency] from: ' + from);
console.log('[LSX_Currencies.switchCurrency] to: ' + to);
}

//If the current from price is not the base
amount = lsx_money(amount).from(from).to(to);
amount = this.formatAmount(amount);
console.log(amount);
return amount;
},

Expand Down Expand Up @@ -124,8 +129,17 @@ LSX_Currencies = {
menuLabelToggle: function(amount) {
amount = accounting.formatNumber(amount, 2, ',', '.');
return amount;
}
},

findAvailableCurrency: function( base_currency, current_selector ) {
for (const [key, value] of Object.entries(lsx_currencies_params.currency_symbols)) {
strict_amount = jQuery( current_selector ).find('.value').attr('data-price-' + key);
if ( undefined !== strict_amount && '0.00' !== strict_amount ) {
base_currency = key;
}
}
return base_currency;
}
};

jQuery(document).ready( function() {
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Dev - Code Standards updates
* Dev - Added in a fallback API for exchange rates.
* Dev - Namespacing and instances for all classes.
* Dev - Added in an option to convert all multiple currencies to your base currency on the frontend.

### 1.1.2
* Dev - Added in WooCommerce integration
Expand Down
4 changes: 2 additions & 2 deletions classes/class-currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Currencies {
public $multi_prices = false;

/** @var boolean */
public $conver_to_single = false;
public $convert_to_single = false;

/** @var boolean */
public $app_id = false;
Expand Down Expand Up @@ -167,7 +167,7 @@ public function set_defaults() {

if ( isset( $this->options['general']['convert_to_single_currency'] ) && 'on' === $this->options['general']['convert_to_single_currency'] ) {
$this->convert_to_single = true;
}
}

if ( isset( $this->options['api']['openexchange_api'] ) && '' !== $this->options['api']['openexchange_api'] ) {
$this->app_id = $this->options['api']['openexchange_api'];
Expand Down
47 changes: 40 additions & 7 deletions classes/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'assets' ), 5 );
add_filter( 'wp_nav_menu_items', array( $this, 'wp_nav_menu_items_filter' ), 10, 2 );
add_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ), 10, 2 );
add_filter( 'get_post_metadata', array( $this, 'filter_post_meta' ), 100, 4 );
}
}

Expand Down Expand Up @@ -108,18 +109,30 @@ public function assets() {
wp_enqueue_script( 'lsx-jquery-cookie', LSX_CURRENCIES_URL . 'assets/js/vendor/cookie.min.js', array( 'jquery' ), LSX_CURRENCIES_VER, true );

$prefix = '.min';
$src = '';
$script_debug = false;
if ( defined( 'SCRIPT_DEBUG' ) ) {
$prefix = '';
$src = 'src/';
$script_debug = true;
}
wp_enqueue_script( 'lsx-currencies', LSX_CURRENCIES_URL . 'assets/js/' . $src . 'lsx-currencies' . $prefix . '.js', array( 'jquery', 'lsx-moneyjs', 'lsx-accountingjs', 'lsx-jquery-cookie' ), LSX_CURRENCIES_VER, true );

$base_currency = lsx_currencies()->base_currency;
$current_currency = $this->current_currency;
if ( true === lsx_currencies()->convert_to_single ) {
$current_currency = $base_currency;
}
wp_enqueue_script( 'lsx-currencies', LSX_CURRENCIES_URL . 'assets/js/src/lsx-currencies' . $prefix . '.js', array( 'jquery', 'lsx-moneyjs', 'lsx-accountingjs', 'lsx-jquery-cookie' ), LSX_CURRENCIES_VER, true );

$params = apply_filters( 'lsx_currencies_js_params', array(
'current_currency' => $this->current_currency,
'currency_symbols' => $this->get_available_symbols(),
'rates' => $this->rates,
'rates_message' => $this->rates_message,
'base' => lsx_currencies()->base_currency,
'flags' => lsx_currencies()->display_flags,
'current_currency' => $current_currency,
'currency_symbols' => $this->get_available_symbols(),
'rates' => $this->rates,
'rates_message' => $this->rates_message,
'base' => $base_currency,
'flags' => lsx_currencies()->display_flags,
'convert_to_single' => lsx_currencies()->convert_to_single,
'script_debug' => $script_debug,
));

wp_localize_script( 'lsx-currencies', 'lsx_currencies_params', $params );
Expand Down Expand Up @@ -353,4 +366,24 @@ public function wp_kses_allowed_html( $allowedtags, $context ) {

return $allowedtags;
}

/**
* Allow empty prices if the convert to single currency is active.
*
* @param null $metadata
* @param string $object_id
* @param string $meta_key
* @param boolean $single
* @return void
*/
public function filter_post_meta( $metadata = null, $object_id, $meta_key, $single ) {
if ( true === lsx_currencies()->convert_to_single && 'price' === $meta_key ) {
$meta_cache = wp_cache_get( $object_id, 'post_meta' );

if ( ! isset( $meta_cache[ $meta_key ] ) || '' === $meta_cache[ $meta_key ] ) {
$metadata = '0';
}
}
return $metadata;
}
}
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const plumber = require('gulp-plumber');
const autoprefixer = require('gulp-autoprefixer');
const gutil = require('gulp-util');
const rename = require('gulp-rename');
const minify = require('gulp-minify-css');
const map = require('map-stream');
const browserlist = ['last 2 version', '> 1%'];
const TerserPlugin = require('terser-webpack-plugin')

const errorreporter = map(function(file, cb) {
if (file.jshint.success) {
Expand Down
Loading

0 comments on commit 73c3572

Please sign in to comment.