按用户角色划分的 WooCommerce 商店货币

WooCommerce shop currency by user role

当登录用户名包含数组“usd”时,我想将我的 wordpress/woocommerce 商店的货币切换为美元。

我尝试使用此代码,但没有成功。你能帮帮我吗?

谢谢

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);

也许您只需要解决这个问题

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

由于您只传递了 1 个参数,可能会将优先级设置为稍后:

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