Skip to content

Instantly share code, notes, and snippets.

@skymaiden
Created September 2, 2018 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skymaiden/be26d9fa3189c29e23e7deb22b1e1af7 to your computer and use it in GitHub Desktop.
Save skymaiden/be26d9fa3189c29e23e7deb22b1e1af7 to your computer and use it in GitHub Desktop.
Format WooCommerce currencies per WPML language

Problem

Currencies in French are formatted differently to currencies in English:

  • English: $00.00, €00.00, £00.00
  • French: 00,00 $, 00,00 €, 00,00 £

WPML says the only way to format the same currency in two ways is via string translation of woocommerce_currency_pos, woocommerce_price_decimal_sep and woocommerce_price_thousand_sep – but that it only works for one language/currency combination... :thinking_face: (See https://wpml.org/forums/topic/two-formats-for-same-currency-in-woocommerce-multilingual/ for details.)

Solution

Let's use hooks to manually override the decimal separator, the currency symbol position and the thousands separator for all currencies in French. :dancer:

<?php
// Show currency symbol on the right for prices in French language
function filter_currency_pos( $format ) {
$current_lang = apply_filters( 'wpml_current_language', NULL );
if ($current_lang == 'fr') {
$format = '%2$s&nbsp;%1$s';
}
return $format;
};
// Use comma as decimal separator for prices in French language
function filter_decimal_sep($decimal_separator) {
$current_lang = apply_filters( 'wpml_current_language', NULL );
if ($current_lang == 'fr') {
$decimal_separator = ',';
}
return $decimal_separator;
}
// Use space as thousands separator for prices in French language
function filter_thousands_sep($thousand_separator) {
$current_lang = apply_filters( 'wpml_current_language', NULL );
if ($current_lang == "fr") {
$thousand_separator = ' ';
}
return $thousand_separator;
}
add_filter( 'woocommerce_price_format', 'filter_currency_pos' );
add_filter( 'wc_get_price_decimal_separator', 'filter_decimal_sep' );
add_filter( 'wc_get_price_thousand_separator', 'filter_thousands_sep' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment