skip to Main Content

I need to change the default currency in WooCommerce based on user roles in WordPress.

For the user role named “customer”, the default price needs to be in SEK and for all others the price must be in DKK

I’ve tried a lot of different solutions but can’t seem to find one that is working.

global $current_user;
 if (in_array('customer', $current_user->roles)) {

Don't know what to put here.. :D

 }

Right now i have no way of moving forward, can’t seem to find anything on Google or StackOverflow which helps my situation.

I can’t find a function that can change default currency programmaticly

2

Answers


  1. You can use woocommerce_currency filter

    add_filter('woocommerce_currency', 'set_role_currency', 200);
    function set_role_currency($currency){
     global $current_user;
     if (in_array('customer', $current_user->roles)) { return 'SEK'; }
    return $currency; //this will return your woocommerce default currency
    }
    
    Login or Signup to reply.
  2. Hi please check below code.

    add_filter('woocommerce_currency','ji_woocommerce_currency',10);
    function ji_woocommerce_currency( $currency ){     
    $user_info = get_userdata(get_current_user_id());
    if ( $user_info->roles[0]=="administrator" ) { 
        return 'USD'; 
    } elseif ( $user_info->roles[0]=="subscriber" ) { 
        return 'GBP'; 
    } else {
        return 'EUR';
    } 
    

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search