为用户角色设置价格显示后缀

Set a Price Display Suffix for user role

我想为我的 WooC 网站上的两个不同用户角色使用不同的价格后缀。

一般客户将没有后缀和含税价格,我在Woo settings Tax选项卡中设置了。

Trade customer 有税前价格,我想给它后缀 "ex VAT ({price_including_tax} inc VAT)"。

我可以做的另一种方法是使用此后缀设置 Woo 设置,而不是尝试对一般客户和其他用户角色隐藏后缀。但是我更愿意添加它,并尝试了我找到并修改的下面的代码,但它不起作用。

任何人都可以指出它有什么问题吗?

add_filter( 'custom_price_suffix', 100, 2 ); 
function custom_price_suffix($price, $current_user_role) { 
$your_suffix = 'ex VAT ({price_including_tax} inc VAT)'; 
if($current_user_role == 'default_wholesaler') { 
$price .= '$your_suffix '; } 

return apply_filters( 'custom_price_suffix', $your_suffix ); 

} 

感谢下面的建议。我目前有以下代码:

function custom_price_suffix( $price, $product ) {
    $your_suffix = 'ex VAT ({price_including_tax} inc VAT)';

    // 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 = 'ex VAT ({price_including_tax} inc VAT)';
    }

    // return $price;
    return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );

这段代码的结果是后缀现在只出现一次。然而,它已经从显示中删除了价格,它仍然无法识别 {price_including_tax} 简码。

As displays for default-wholesaler user on front end

你可以使用下面的wp_get_current_user()

https://developer.wordpress.org/reference/functions/wp_get_current_user/

function custom_price_suffix( $price, $product ) {
    // for debug purposes, delete after testing
    echo '1 = ' . $price;

    $your_suffix = 'suffix here';

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

    // for debug purposes, delete after testing
    echo '<pre>2 = ' , print_r($roles, 1), '</pre>';

    if ( in_array( 'administrator', $roles ) ) {
        $price = $price . ' - ' . $your_suffix;
    } elseif ( in_array( 'user...', $roles ) ) {
        $price = 'something';
    }

    // return $price;
    return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );