税收类别“零费率”每个用户在特定产品ID上的角色 [英] Tax class "Zero rate" per user role on specific product ID's

查看:45
本文介绍了税收类别“零费率”每个用户在特定产品ID上的角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这段代码,该代码将对用户角色免税,无论他们订购什么,这都很好。

I got this code that will apply tax free on a user role regardless of what they order, which is fine.

但是现在我需要另一个用户角色,该角色将对特定产品ID免税 ,而且我不确定如何完成此任务。

But now I need another user role that will apply tax free on specific products id, and I'm not sure how to acomplish that.

目前针对特定用户角色在所有产品上使用的免税代码是:

The code im using right now for tax free on all products for specific user role is:

// Apply a different tax rate based on the user role.

function wc_diff_rate_for_user( $tax_class, $product ) {
// Getting the current user 
$current_user = wp_get_current_user();
$current_user_data = get_userdata($current_user->ID);

if ( in_array( 'administrator', $current_user_data->roles ) || in_array( 'userrolename', $current_user_data->roles ) )
    $tax_class = 'Zero Rate';

return $tax_class;
}
add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );
// Fin Apply a different tax rate based on the user role.


推荐答案

以下是将应用此零费率的代码某些定义的产品和某些定义的用户角色的税种:

Here is the code that will apply this "Zero Rate" tax class for some defined products and some defined user roles:

add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );
function wc_diff_rate_for_user( $tax_class, $product ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Define HERE your targeted products IDs
    $products_ids_arr = array(12 ,15, 24);

    // Define HERE your targeted user roles
    $users_role_arr = array('administrator', 'userrolename');

    //Getting the current user data
    $user_data = get_userdata(get_current_user_id());

    foreach ($users_role_arr as $user_role)
        if ( in_array( $user_role, $user_data->roles ) && in_array( $cart_item->id, $products_ids_arr ) ) {
            $tax_class = 'Zero Rate';
            break;
        }

    return $tax_class;

}

此代码已经过测试并且有效。

This code is tested and works.

代码会出现在您活动的子主题(或主题)的任何php文件中,也可能出现在任何插件的php文件中。

这篇关于税收类别“零费率”每个用户在特定产品ID上的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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