简体   繁体   中英

Change WooCommerce default currency based on user role

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

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
}

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';
} 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM