Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据用户角色更改默认货币_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 根据用户角色更改默认货币

Php 根据用户角色更改默认货币,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我需要根据WoodPress中的用户角色更改WooCommerce中的默认货币 对于名为“customer”的用户角色,默认价格必须以瑞典克朗为单位,对于所有其他角色,价格必须以丹麦克朗为单位 我尝试了很多不同的解决方案,但似乎找不到一个有效的 global $current_user; if (in_array('customer', $current_user->roles)) { Don't know what to put here.. :D } 现在我没有前进的路,似

我需要根据WoodPress中的用户角色更改WooCommerce中的默认货币

对于名为“customer”的用户角色,默认价格必须以瑞典克朗为单位,对于所有其他角色,价格必须以丹麦克朗为单位

我尝试了很多不同的解决方案,但似乎找不到一个有效的

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

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

 }

现在我没有前进的路,似乎在谷歌或StackOverflow上找不到任何有助于我的情况的东西


我找不到可以更改默认货币程序的函数。您可以使用
woocommerce\u currency
过滤器

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
}

您好,请检查下面的代码

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

}

嗨,shir,非常感谢你的建议。基于代码,它应该可以工作,但当以客户身份登录时,所有的价格仍然在DKK中,这让我觉得有点奇怪。我把它放在functions.php的底部,但结果并不像预期的那样。我认为全局$current\u用户是错误的。试试这个:$user=get_current_user();如果(在_数组中('customer',$user->roles))…感谢您再次尝试:-D Hm,结果仍然相同。仍然在DKK发行。这很奇怪。尝试清除函数,只需添加返回'SEK';让我们检查一下这个过滤器是否被称为Orry Shir,您的代码实际工作正常。我有一个插件,这是固定的。谢谢您的帮助:-)!