Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

add_filter( 'wcml_is_cache_enabled_for_switching_currency', '__return_true' );

can be used for this.

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.

Tagged: 

This topic contains 8 replies, has 2 voices.

Last updated by Bruno Kos 7 months ago.

Assisted by: Bruno Kos.

Author Posts
February 16, 2024 at 12:03 pm #15311448

Hai Zheng

Hi,

is there is any secret code in WPML/WCML , that I can force the page URL to attach with ?currency=XXX in every page it shows up ?

Best regards,

February 16, 2024 at 2:13 pm #15312003

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

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

Hi,

Thank you for contacting WPML support!

There is no such feature, but can you check this?
https://wpml.org/forums/topic/how-to-change-currecy-using-a-url-parameter/#post-6854911

Or you can try something like this:

add_action('init', 'custom_switch_currency');
function custom_switch_currency() {
    if(isset($_GET['currency']) && !empty($_GET['currency'])) {
        $currency = sanitize_text_field($_GET['currency']);
        // Ensure the currency code is valid and exists in your WooCommerce settings
        if(in_array($currency, get_woocommerce_currencies())) {
            // Use WooCommerce Multilingual or WPML API to switch the currency
            do_action('wcml_switch_currency', $currency);
            // Note: This is a simplified approach. You might need to handle sessions or redirect.
        }
    }
}

https://wpml.org/documentation/related-projects/woocommerce-multilingual/wcml-hooks-reference/#hook-1113509

But this code is not tested so I can't say whether it would work, such coding not included within out support scope. That being said, please consult our https://wpml.org/contractors/ for further help.

Regards,
Bruno Kos

February 16, 2024 at 8:54 pm #15313139

Hai Zheng

Hi,

thanks for the reply , but it's not exactly what I wanted

I was not exactly looking for a way to change the currency via query string, but more like to detect the currency by the query string

basically what I am looking for , is similar as you did with language query string , `?lang=XX`

but I want the `?currency=` query to show up on any and all pages regardless what is the default currency or what is the language

like default language and default currency, any and all pages will be like

hidden link
hidden link

or other language to be

hidden link
hidden link

is it technically achievable through some secret code snippet ?

Best regards,

February 19, 2024 at 7:35 am #15316085

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

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

Can you check this?
https://wpml.org/forums/topic/woocommerce-multilingual-multicurrency-with-wpml/#post-12723911

Another approach may be ( this is not tested and requires further coding and checks) something like the following:

1. **Detect Query String and Set Currency**: Use WordPress hooks to detect the `currency` parameter in the URL and set the user's session or cookie to reflect the chosen currency.

    add_action('init', 'custom_set_currency_from_url');
    function custom_set_currency_from_url() {
        if(isset($_GET['currency']) && !empty($_GET['currency'])) {
            // Assuming 'currency' is your query parameter
            $currency = sanitize_text_field($_GET['currency']);
            // Set this currency to your currency switcher/session
            // This will depend on how your currency switching functionality is implemented
        }
    }

2. **Ensure Currency Parameter Persists Across Pages**: You'll need to ensure that once a user selects a currency, this preference is carried across all pages they visit. This might involve adjusting links site-wide to append the `currency` query parameter dynamically.

For WooCommerce or similar, you might hook into URL generation functions to append the query string:

    add_filter('post_type_link', 'append_currency_query_string', 10, 2);
    add_filter('page_link', 'append_currency_query_string', 10, 2);
    // Add more filters as needed for custom post types, taxonomy terms, etc.

    function append_currency_query_string($url, $post) {
        if (!is_admin()) {
            // Check if the currency is set in session/cookie
            $currency = isset($_SESSION['currency']) ? $_SESSION['currency'] : 'USD'; // Default to USD if not set
            // Append currency to URL
            $url = add_query_arg('currency', $currency, $url);
        }
        return $url;
    }

3. **WPML Language Switcher Adjustment**: If you're using WPML's language switcher, you might need to customize it to ensure the currency parameter is retained when switching languages. This could involve filtering the language switcher's URLs through the `wpml_ls_language_url` filter.

    add_filter('wpml_ls_language_url', function ($url, $language_code) {
        // Append the currency query parameter to the URL
        $currency = isset($_SESSION['currency']) ? $_SESSION['currency'] : 'USD';
        return add_query_arg('currency', $currency, $url);
    }, 10, 2);

These code snippets can be added to your theme's `functions.php` file or ideally, in a custom plugin.

But note that this is not a tested and may not work as expected or at all - you need to check with our https://wpml.org/contractors/ to assist with this because this is not within our support scope, hopefully this will give you some guidelines on how this may be achieved.

February 19, 2024 at 6:54 pm #15319895

Hai Zheng

Hi,

many thanks, this is getting close to what I needed , just one thing short

I was able to make all links attached with `?currency=USD`

now t seems only problem right now is how to make the language switcher to update the URL

```
add_filter('wpml_ls_language_url', function ($url, $language_code) {
// Append the currency query parameter to the URL
$currency = isset($_SESSION['currency']) ? $_SESSION['currency'] : 'USD';
return add_query_arg('currency', $currency, $url);
}, 10, 2);
```

this one seems doesn't change the URL

for example when I open the page as USD currency with ?currency=USD , when I do swtich to EUR by switcher , despite the page is now displaying EUR, but the URL is still on `?currency=EUR` , I think this is the last thing I need to get over with.

best regards,

February 19, 2024 at 7:22 pm #15319924

Hai Zheng

hmmm, by logging out the variable I can see it should returns URL with new currency like /product/belt/?currency=EUR when I go from USD to EUR , but the browser seems still stick to =USD qs

so last thing I need now , is seems to make the URL change accordingly when switch currency

SCR-20240219-tdbn.png
February 20, 2024 at 6:25 am #15320621

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

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

I cannot assist further here - please ask our https://wpml.org/contractors/ for custom code as it is not within our https://wpml.org/purchase/support-policy/.

February 20, 2024 at 11:12 am #15322150

Hai Zheng

alright , thanks anyway , it's kind of annoying just one step away

you are already half-way there , this feature will be very useful for cache plugin compatibility though.

I do strongly suggest you to implement it , since you got the language query string already , it shouldn't be too much work to get currency query string as well.

February 20, 2024 at 2:35 pm #15323459

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

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

We always tend to help clients as much as possible, but to the limits of our knowledge and support policies. This is where contractors step in.

Also, as for the feature it may be implemented into one of the future versions, however we take every feature request into consideration based on the forum cases, etc.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.