税收类别“零费率”每个用户在特定产品ID上的角色

编程入门 行业动态 更新时间:2024-10-26 16:29:19
本文介绍了税收类别“零费率”每个用户在特定产品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上的角色

本文发布于:2023-10-12 20:57:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:费率   税收   角色   类别   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!