【问题标题】:WooCommerce shop currency by user roleWooCommerce 商店货币(按用户角色)
【发布时间】:2021-02-26 12:23:36
【问题描述】:

当登录的用户名包含数组“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);

【问题讨论】:

  • "用户名包含数组"usd"" ?这是否意味着您的用户名包含像这样的“usd”一词:“username_usd”或者您是否将用户的任何角色设置为“usd”?
  • 用户角色包含“usd”,如何仔细检查wordpress比较的角色是否真的包含字符串“usd”?

标签: wordpress woocommerce user-roles


【解决方案1】:

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

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

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

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

【讨论】:

  • 感谢您的快速回复。我认为问题在于在用户角色数组中找到字符串“usd”。我使用插件 WooCommerce Wholesale Pro 创建了额外的用户角色,并将其命名为“wholesale_usd”。我认为显示名称是“wholesale_usd”,但它背后的真实名称不同。如何查看用户角色的名称?
  • @Ahmed 你总是可以使用内置的 WC 记录器。 $logger = 新的 WC_Logger(); $logger_name = "我的记录器名称"; $logger->add($logger_name,print_r($user->roles, true));
猜你喜欢
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
相关资源
最近更新 更多