根据用户组或登录状态显示或隐藏定价后添加的文本

问题描述 投票:0回答:1

我正在使用 B2B 插件,它允许我为不同的用户组创建不同的定价。向所有用户显示标准定价,直到他们注册并帐户并添加到具有更好定价的组(对于批发客户等)。

我在标准定价之后添加了一些代码来表示零售定价。问题是我不希望在用户登录或某些用户组后显示此后缀,因为这些客户的定价不再是零售,而是批发。

我使用的代码是:

function woo_text_after_price( $price ) {
    $price .= ' Retail Price';
    return $price;
}
add_filter( 'woocommerce_get_price_html', 'woo_text_after_price' );
add_filter( 'woocommerce_cart_item_price', 'woo_text_after_price' );

我使用的 B2B 插件名为 WooCommerce B2B,是从 code4lifeitalia 开发的 Code Canyon 购买的。

提前谢谢您。

我还没有尝试过任何解决方案,因为我不知道该尝试什么。我知道我需要某种过滤器或规则,但我不知道如何创建一个。

php wordpress woocommerce user-roles usergroups
1个回答
0
投票

“用户组”在 WordPress 中称为“用户角色”,因此在下面的代码中尝试为您的 Wholesale 用户组定义正确的用户角色:

function woo_text_after_price( $price ) {
    global $current_user;

    $targeted_user_role = 'wholesale'; // <== Here define the correct user role slug for B2B user group

    if ( ! in_array($targeted_user_role, $current_user->roles) ) {
        $price .= __(' Retail Price');
    }
    return $price;
}
add_filter( 'woocommerce_get_price_html', 'woo_text_after_price' );
add_filter( 'woocommerce_cart_item_price', 'woo_text_after_price' );

应该可以。


相同,但对于未登录的用户(不针对用户组)

function woo_text_after_price( $price ) {
    if ( ! is_user_logged_in() ) {
        $price .= __(' Retail Price');
    }
    return $price;
}
add_filter( 'woocommerce_get_price_html', 'woo_text_after_price' );
add_filter( 'woocommerce_cart_item_price', 'woo_text_after_price' );

© www.soinside.com 2019 - 2024. All rights reserved.