gpt4 book ai didi

php - 根据用户角色和产品类别应用不同的税收 (Woocommerce)

转载 作者:搜寻专家 更新时间:2023-10-31 21:26:43 25 4
gpt4 key购买 nike

如果用户具有特定角色,我需要应用不同的税,但仅限于某些产品类别。

示例:如果角色为“Vip”的客户 A 购买类别为“Bravo”或“Charlie”的商品,适用的税费将为 4% 而不是 22%

这段代码一部分是我自己写的另一部分是在google上截取的,但是我不明白我哪里错了。

有人能帮帮我吗?

function wc_diff_rate_for_user( $tax_class, $product ) {
global $woocommerce;

$lundi_in_cart = false;

foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );

foreach ($terms as $term) {
$_categoryid = $term->term_id;
}
if (( $_categoryid === 81 ) || ( $_categoryid === 82 ) )) {

if ( is_user_logged_in() && current_user_can( 'VIP' ) ) {
$tax_class = 'Reduced Rate';
}
}
}

return $tax_class;
}

最佳答案

税收是按购物车中的每行商品计算的。您不必循环购物车商品。相反,检查当前项目是否具有您要查找的类别。

像这样尝试...

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

// not logged in users are not VIP, let's move on...
if (!is_user_logged_in()) {return $tax_class;}

// this user is not VIP, let's move on...
if (!current_user_can( 'VIP' ) ) {return $tax_class;}

// it's already Reduced Rate, let's move on..
if ($tax_class == 'Reduced Rate') {return $tax_class;}

// let's get all the product category for this product...
$terms = get_the_terms( $product->id, 'product_cat' );
foreach ( $terms as $term ) { // checking each category
// if it's one of the category we'er looking for
if(in_array($term->term_id, array(81,82))) {
$tax_class = 'Reduced Rate';
// found it... no need to check other $term
break;
}
}

return $tax_class;
}

关于php - 根据用户角色和产品类别应用不同的税收 (Woocommerce),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35032661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com