Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 1 reply, has 2 voices.

Last updated by Alejandro 1 year, 5 months ago.

Author Posts
December 12, 2023 at 10:03 am #15043943

wimerH

I'm trying to create a custom currency switcher that also includes the currency name (United States (US) dollar) like displayed in the backend next to the currency code (USD).

I'm following this guide: https://wpml.org/documentation/related-projects/woocommerce-multilingual/designing-custom-currency-switchers-using-template-files/

And have been able to create my own template, but I can't seem to figure out how I can modify the `currencies` array that's it's looping over in the twig template:

`{% for currency in currencies %}...{% endfor %}`

So I'm trying to use my own function in the loop (just like the get_formatted_price) from my namespaced php class to retrieve the currency name

`{{ function('App\Controllers\Wcml', 'getCurrencyName', currency)|raw }}`

but it's not recognized in twig.
I can see the `get_formatted_price` function is being registered in the construct of the WCML_Currency_Switcher_Template class.

Is it possible to use my own function in twig or do I need to register it first with `WPML\Core\Twig_SimpleFunction`?

Any help is appreciated!
Thanks

custom_currency_switcher.png
December 12, 2023 at 12:42 pm #15046009

Alejandro
WPML Supporter since 02/2018

Languages: English (English ) Spanish (Español ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Hello!

I'll help you get started while another supporter takes your ticket.

You can use WooCommerce hooks for this:

- hidden link
- hidden link
- hidden link

First get_woocommerce_currency() to get the current currency, and then match it by get_woocommerce_currency_symbols()

Here's a small example that mght get you started

$currency_code = get_woocommerce_currency();
$currencies = get_woocommerce_currencies();
$currency_name = isset($currencies[$currency_code]) ? $currencies[$currency_code] : 'Unknown Currency';
echo 'Currency Name: ' . $currency_name;

-----------------
Just as a disclaimer, i remind you we can't write code for you so we're a bit restricted on what we can do (I know you didn't ask for it, this is just a general disclaimer)

December 19, 2023 at 8:47 am #15089803

wimerH

Thanks, I ended up using my own blade partial with a view composer using those functions you mentioned instead of trying to use twig. All you have to make sure is to add the 'wcml_currency_switcher' class so it will still use the bundled javascript from WPML.