我正在使用一个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开发.

先谢谢你.

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

推荐答案

"用户组"在WordPress中被称为"用户角色",所以在下面的代码中try 为您的用户组定义正确的用户角色slug(s):

function woo_text_after_price( $price ) {
    global $current_user;

    // Here below, define the correct user role(s) slug(s) for your user group(s)
    $targeted_user_roles = array('user-group1', 'user-group2'); 

    if ( ! array_intersect($targeted_user_roles, $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' );

应该能行得通.


对于未登录用户(without targeting user groups):

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

Php相关问答推荐

在AJX请求中使用简写后,FormData出现非法调用错误

有什么方法可以阻止WordPress主题升级通知吗?

从含有特殊字符的ANSI文本文件中读取数据是在移值,为什么?

在WooCommerce中禁用特定日期之前的免费送货

如何显示每个分类的当前术语的帖子中包含的前20个标签,不包括标签分类

来自POST请求的(无效)json字符串中的奇怪字符(编码问题)

Codeigniter 3中SMTP.office 365.com的截断问题

为什么只有最后一次点击的点赞/心形会按预期改变 colored颜色 ,而其他的保持正常 colored颜色 ?

从WooCommerce管理订单汇总中隐藏原始小计行

如何在PHP中根据会话将用户重定向到不同的页面?

invalid_grant 和无效的 JWT 签名

如何在 Woocommerce 中自动删除旧的已完成订单

为什么 php 只能在我的 Nginx Web 服务器的某些目录中工作?

将 WooCommerce 0 销售价格替换为自定义文本,并保留常规价格删除线

根据WooCommerce中的产品重量更改简单产品的输入数量步值

Woocommerce注册中的自定义复选框验证错误提示问题

仅在管理员新订单通知中显示WooCommerce订单商品自定义元数据

带有 nginx 的 Symfony 不提供 .js 和 .css 文件

使用 --exclude-dir 选项从 PHP 执行 grep 时出现问题

使用 v-for 的存储链接中的 Img src 在 Laravel vuejs 中不起作用