我有固定客户以及固定和分级批发商。我住在加拿大,需要缴纳两种税(GST和PST)。我们也有一些客户/批发商仅免税一种或两种税。
我有一个常规批发角色,有两个免税角色:“ wholesale_customer”,“ wholesale_pst_exempt”,“ wholesale_tax_exempt”。
我有4个级别的批发角色,每个角色都有自己的免税角色:
'wholesale_silvia_silver', 'wholesale_silvia_gold', 'wholesale_silvia_premium', 'wholesale_silvia_union'
'wholesale_silvia_silver_pst_exempt', 'wholesale_silvia_gold_pst_exempt', 'wholesale_silvia_premium_pst_exempt', 'wholesale_silvia_union_pst_exempt'
'wholesale_silvia_silver_tax_exempt', 'wholesale_silvia_gold_tax_exempt', 'wholesale_silvia_premium_tax_exempt', 'wholesale_silvia_union_tax_exempt'
这些等级是新的,我正在尝试更新现有功能,该功能根据客户用户角色应用不同的税率。我还具有过滤器,可以根据客户角色更改不同税种的运输税。
以下是我已更新的功能和过滤器,以添加其他的层级批发角色。我所做的更改无法正常工作,因为我看不到免税项目。这两种税总是被征收。
有人可以帮我弄清楚我对阻止其工作的代码做错了什么吗?我不精通PHP,因此正竭尽全力使这项工作有效。
/*
* APPLY DIFFERENT TAX RATE BASED ON CUSTOMER USER ROLE
* (Code compacted in one unique hook instead of 5 functions with the same hook)
*/
function all_custom_tax_classes( $tax_class, $product ) {
global $current_user;
// Getting the current user
$curr_user = wp_get_current_user();
$curr_user_data = get_userdata($current_user->ID);
// 1 customer_tax_exempt
/* special tax rate: zero if role: Customer Tax Exempt */
/*if ( in_array( 'customer_tax_exempt', $curr_user_data->roles ) )
$tax_class = 'CustomerTaxExemptClass';
// 2 customer_pst_exempt
// special tax rate: charge only GST if role: Customer PST Exempt
if ( in_array( 'customer_pst_exempt', $curr_user_data->roles ) )
$tax_class = 'CustomerPSTExemptClass';
*/
// 3, 4 & 5 WHOLESLE SUITE SPECIAL WHOLESALE TAX RATES
if (isset($current_user) && class_exists('WWP_Wholesale_Roles')) {
$wwp_wholesale_roles = WWP_Wholesale_Roles::getInstance();
$wwp_wholesale_role = $wwp_wholesale_roles->getUserWholesaleRole();
// special tax rate: charge both PST and GST if roles: Wholesale Customer, Wholesale Silvia Silver, Wholesale Silvia Gold, Wholesale Silvia Premium, Wholesale Silvia Union
if (!empty($wwp_wholesale_role) && in_array('wholesale_customer', $wwp_wholesale_role) && in_array('wholesale_silvia_silver', $wwp_wholesale_role) && in_array('wholesale_silvia_gold', $wwp_wholesale_role) && in_array('wholesale_silvia_premimum', $wwp_wholesale_role) && in_array('wholesale_silvia_union', $wwp_wholesale_role)) {
// Where 'wholesale_customer, wholesale_silvia_silver, wholesale_silvia_gold, wholesale_silvia_premium, wholesale_silvia_union' are the names of the wholesale roles to target
$tax_class = 'WholesalePSTGST';
}
// special tax rate: charge only GST if roles: Wholesale PST Exempt, Wholesale Silvia Silver PST Exempt, Wholesale Silvia Gold PST Exempt, Wholesale Silvia Premium PST Exempt, Wholesale Silvia Union PST Exempt
if (!empty($wwp_wholesale_role) && in_array('wholesale_pst_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_silver_pst_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_gold_pst_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_premium_pst_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_union_pst_exempt', $wwp_wholesale_role)) {
// Where 'wholesale_pst_exempt, wholesale_silvia_silver_pst_exempt, wholesale_silvia_gold_pst_exempt, wholesale_silvia_premium_pst_exempt, wholesale_silvia_union_pst_exempt' are the names of the wholesale roles to target
$tax_class = 'WholesalePSTExempt';
}
// special tax rate: zero if roles: Wholesale Tax Exempt, Wholesale Silvia Silver Tax Exempt, Wholesale Silvia Gold Tax Exempt, Wholesale Silvia Premium Tax Exempt, Wholesale Silvia Union Tax Exempt
if (!empty($wwp_wholesale_role) && in_array('wholesale_tax_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_silver_tax_exempt', $wwp_wholesale_role)&& in_array('wholesale_silvia_gold_tax_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_premium_tax_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_union_tax_exempt', $wwp_wholesale_role)) {
// Where 'wholesale_tax_exempt, wholesale_silvia_silver_tax_exempt, wholesale_silvia_gold_tax_exempt, wholesale_silvia_premium_tax_exempt, wholesale_silvia_union_tax_exempt' are the names of the wholesale role to target
$tax_class = 'WholesaleZeroTax';
}
}
return $tax_class;
}
/* ADDITIONAL FILTERS TO ALTER THE SHIPPING TAX FOR DIFFERENT TAX CLASSES BASED ON CUSTOMER USER ROLE */
add_filter( 'woocommerce_product_get_tax_class', 'all_custom_tax_classes', 1, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'all_custom_tax_classes', 1, 2 );
add_filter( 'option_woocommerce_shipping_tax_class' , function( $option_value ) {
global $wc_wholesale_prices;
if ( $wc_wholesale_prices && is_a( $wc_wholesale_prices , 'WooCommerceWholeSalePrices' ) ) {
$current_user_wholesale_roles = $wc_wholesale_prices->wwp_wholesale_roles->getUserWholesaleRole();
if ( in_array( 'wholesale_customer', $current_user_wholesale_roles ) ){
return 'wholesalepstgst';
} elseif (in_array( 'wholesale_silvia_silver', $current_user_wholesale_roles) ){
return 'wholesalepstgst';
} elseif (in_array( 'wholesale_silvia_gold', $current_user_wholesale_roles) ){
return 'wholesalepstgst';
} elseif (in_array( 'wholesale_silvia_premium', $current_user_wholesale_roles) ){
return 'wholesalepstgst';
} elseif (in_array( 'wholesale_silvia_union', $current_user_wholesale_roles) ){
return 'wholesalepstgst';
} elseif (in_array( 'wholesale_pst_exempt', $current_user_wholesale_roles) ){
return 'wholesalepstexempt';
} elseif (in_array( 'wholesale_silvia_silver_pst_exempt', $current_user_wholesale_roles) ){
return 'wholesalepstexempt';
} elseif (in_array( 'wholesale_silvia_gold_pst_exempt', $current_user_wholesale_roles) ){
return 'wholesalepstexempt';
} elseif (in_array( 'wholesale_silvia_premium_pst_exempt', $current_user_wholesale_roles) ){
return 'wholesalepstexempt';
} elseif (in_array( 'wholesale_silvia_union_pst_exempt', $current_user_wholesale_roles) ){
return 'wholesalepstexempt';
} elseif (in_array( 'wholesale_tax_exempt', $current_user_wholesale_roles) ){
return 'wholesalezerotax';
} elseif (in_array( 'wholesale_silvia_silver_tax_exempt', $current_user_wholesale_roles) ){
return 'wholesalezerotax';
} elseif (in_array( 'wholesale_silvia_gold_tax_exempt', $current_user_wholesale_roles) ){
return 'wholesalezerotax';
} elseif (in_array( 'wholesale_silvia_premium_tax_exempt', $current_user_wholesale_roles) ){
return 'wholesalezerotax';
} elseif (in_array( 'wholesale_silvia_union_tax_exempt', $current_user_wholesale_roles) ){
return 'wholesalezerotax';
}
}
return $option_value;
} , 10 , 1 );
谢谢 溶解