Search code examples
wordpresswoocommerceuser-roles

WooCommerce shop currency by user role


I would like to switch the currency of my wordpress/woocommerce shop to USD when the logged in user name contains the array "usd".

I tried with this code but it didn't work. Could you please help me?

Thank you

function set_role_currency($currency){
    if( is_user_logged_in() ) {
 $user = wp_get_current_user();
 $roles = ( array ) $user->roles;}
 if (in_array('usd', $roles)) { return 'USD'; }
 return $currency; 
}
add_filter('woocommerce_currency', 'set_role_currency', 10, 2);

Solution

  • Maybe all you need is just to fix this

    add_filter('woocommerce_currency', 'set_role_currency', 10, 2);
    

    Since you are passing only 1 param, and maybe set the priority to be later:

    add_filter('woocommerce_currency', 'set_role_currency', 100, 1);