根据用户角色删除价格后缀 [英] Removing price suffix based on user role

查看:84
本文介绍了根据用户角色删除价格后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未能添加基于用户角色的后缀,因为它不会显示Woo价格显示简码,我现在正从另一个方向解决此问题-我已将后缀添加到Woos税标签中,现在却想删除后缀(除一个用户角色以外的所有用户角色).

Having failed at adding a suffix based on User Role because it wont display the Woo price display shortcode, I'm now approach the problem from the other direction - I have added the suffix to Woos tax tab, and now instead want to remove the suffix (from all user roles except one).

我在github上找到了以下代码,以从产品中删除后缀:

I found this code on github to remove the suffix from products:

add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 2 );

function custom_woocommerce_get_price_suffix( $price_display_suffix, $product ) {
  if ( ! $product->is_taxable() ) {
    return '';
  }
  return $price_display_suffix;
}

并且我已经对其进行了修改以隐藏某些用户类型的后缀

and I have modified it to hide the suffix from certain user types

add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 2 );

function custom_woocommerce_get_price_suffix( $price_display_suffix, $product ) {

 // check current user role
    $user = wp_get_current_user();
    $roles = ( array ) $user->roles;

   if ( in_array( 'administrator', $roles ) ) {
        $price = $your_suffix;
    } elseif ( in_array( 'default_wholesaler', $roles ) ) {
        $price = '$your_suffix';

    return '';
  }
  return $price_display_suffix;
}

这可行,但是我不得不切换用户(我希望管理员和批发商看到后缀)并放入客户等.

This worked, however I had to switch the users (I want Admin and Wholesalers to see the suffix) and put in Customers, etc.

问题在于来宾客户仍然看到后缀.

The problem is that guest customers still see the suffix.

除了登录的默认批发商"用户和管理员"以外,有人可以建议对任何人隐藏后缀吗?

Could someone suggest a way of hiding the suffix from anyone except the logged in 'default-wholesaler' user and 'Administrator'?

谢谢!

推荐答案

您可以为此使用以下内容.

You could use the following for this.

适用于管理员角色!您可以将其他角色自己添加为运动"

This works for the administrator role! you can add the other role yourself as an 'exercise'

function custom_woocommerce_get_price_suffix( $html, $product, $price, $qty ) { 
    // check current user role
    $user = wp_get_current_user();
    $roles = ( array ) $user->roles;

    // if NOT in array user roles
    if ( !in_array( 'administrator', $roles ) ) {
        $html = '';
    }

    return $html;
}
add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 4 );

这篇关于根据用户角色删除价格后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆