我有两个不同的用户角色.厨师和顾客.对于厨师,我想要添加税,但对于客户,我不想添加税.问题是,对于Chef来说,像WooCommerce允许的那样增加税收没有问题.但我不想为用户角色Customer增加税收.

这就是我通过放入unctions.php来try 的方法,但它不起作用.

function exclude_tax_for_customer_role($cart) {
    if (is_user_logged_in()) {
        $user = wp_get_current_user();
        $user_roles = $user->roles;

        // Check if the user has the "Customer" role
        if (in_array('customer', $user_roles)) {
            // Set tax amounts to zero
            $cart->remove_taxes();
        }
    }
}
add_action('woocommerce_cart_loaded', 'exclude_tax_for_customer_role');

推荐答案

WC_Customer类有is_vat_exempt个属性,这对用户免税很有用.try 以下操作:

add_action( 'template_redirect', 'set_customer_user_role_vat_exempt' );
function set_customer_user_role_vat_exempt(){
    global $current_user;
    if ( ! is_user_logged_in() ) 
        return; 

    // Set "customer" user role "Vat exempt" if is not set yet
    if ( in_array( 'customer', $current_user->roles ) && ! WC()->customer->is_vat_exempt() ) {
        WC()->customer->set_is_vat_exempt( true );
    }   
}

代码放在主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

使用额外的输入字段自定义WooCommerce单个产品

添加自定义Metabox到WooCommerce管理订单,启用HPOS

获得客户S在WooCommerce为期1年的总支出

在ShipStation for WooCommerce中使用自定义订单项目元数据

照明\Support\Collection::Filter():参数#1($Callback)的类型必须为?可调用、已给定数组、已调用

WooCommerce在store 页面上显示兼容多语言的产品属性

在WooCommerce中设置带有国家/地区和年份前缀的顺序序号

在PHP中,如何将介于0和1之间(包括0和1)的浮点概率转换为数组索引?

将下拉 Select 的值从WooCommerce后端保存并显示到前端

WordPress:今天的计数';修改和发布的帖子

在WooCommerce存档页面中显示可变产品的库存变化属性

主机中未检索用户表数据

使用 wp_footer 挂钩添加一些 CSS 不起作用

获取 Shopware 6 集合元素的值,PHP

如何在 php 中获取正确的时间

如何在WooCommerce中为访客购买者分配自定义客户代码

服务器遇到内部错误或配置错误,无法完成您的请求

按前 3 列对二维数组中的行数据进行分组,然后用逗号连接每组中的其余列

如何在光速网络服务器中使用 apache ..htaccess

如何获取最后一条消息属于 Laravel 中特定用户的对话列表