How to dynamically change currency in WooCommerce?

I’m working on a WooCommerce store and trying to find a way to allow my customers to switch between different currencies on the site. I have some experience with PHP and WordPress development, but I’m new to WooCommerce, and I’m not exactly sure how to approach this.

<form method="get">
  <label for="currency">Currency:</label>
  <select id="currency" name="currency">
    <option value="USD">USD</option>
    <option value="EUR">EUR</option>
    <option value="GBP">GBP</option>
  </select>
  <button type="submit">Switch</button>
</form>

I want to allow my customers to change the currency by passing a parameter in the URL, for example,

https://example.com/?currency=EUR

I would then like to use this parameter to dynamically change the currency displayed on the site. I understand that I may need to write custom code to achieve this, but I’m unsure where to start. Any help or guidance on this topic would be greatly appreciated.

You can use the $_GET variable to retrieve the selected currency value and then use the setcookie function to store the currency on the user’s browser so you can remember user preference. Then you can use the woocommerce_currency filter to dynamically change the currency in WooCommerce based on the selected or stored currency in the cookie.

global $dynamic_currency;
if ( isset($_GET['currency']) && in_array($_GET['currency'], array('USD', 'EUR', 'GBP')) ) {
	$dynamic_currency = $_GET['currency'];
	setcookie('currency', $dynamic_currency, time() + (86400 * 30), "/");
}
else if( isset($_COOKIE['currency']) ){
	$dynamic_currency = $_COOKIE['currency'];
}
else{
	$dynamic_currency = 'USD';
}

function dynamic_change_currency( $currency ) {
    global $dynamic_currency;
    if ( in_array($dynamic_currency, array('USD', 'EUR', 'GBP')) ) {
        return $dynamic_currency;
    }
    return $currency;
}
add_filter( 'woocommerce_currency', 'dynamic_change_currency' );

This is just a basic example to get you started. You’ll likely need to modify this code to meet your specific needs and you may also want to consider implementing additional features, such as currency conversion or the ability to switch the currency on the fly using JavaScript.