Appending currency to URL on currency switch

Hello @brandon123and4,

I’m not sure if something like this will work but it can give you another idea. The goal is to get the current currency using the wcml_price_currency filter and to check if the currency is already set in the URL.

function append_currency_to_url($new_currency) {
    // Check if the currency is already set in the URL to prevent infinite loop
    if (isset($_GET['currency']) && $_GET['currency'] === $new_currency) {
        return;
    }

    // Get the current currency using the WCML filter.
    $current_currency = apply_filters('wcml_price_currency', NULL);

    if (!empty($current_currency)) {
        // Get the current URL without query strings.
        $current_url = strtok($_SERVER["REQUEST_URI"], '?');

        // Append the currency parameter to the URL using add_query_arg.
        $updated_url = add_query_arg(array('currency' => $current_currency), $current_url);

        // Sanitize the updated URL.
        $updated_url = esc_url($updated_url);

        // Redirect to the sanitized URL.
        wp_redirect($updated_url);
        exit;
    }
}

// Add the action.
add_action('wcml_switch_currency', 'append_currency_to_url', 10, 1);

Regards,
Andrés

i found a solution using get_woocommerce_price(), thanks Andrés!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer