You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于用户角色为多个WooCommerce商品变体排除税费的调试问题

适配多商品变体的税费排除修改方案

以下是可直接运行的修改后代码:

add_filter( 'woocommerce_product_get_tax_class', 'change_tax_class_user_role', 10, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'change_tax_class_user_role', 10, 2 );
function change_tax_class_user_role( $tax_class, $product ) {
    // 可按需添加更多需要排除免税的变体ID,用英文逗号分隔即可
    $excluded_variation_ids = [618, 1380];
    $targeted_user_role    = 'ResellerCO';

    if ( $product->is_type('variation') && in_array($product->get_id(), $excluded_variation_ids) ) {
        return $tax_class;
    } 
    elseif ( current_user_can( $targeted_user_role ) ) {
        return 'Reduced rate';
    }
    return $tax_class;
}

修改说明

  • 将原代码中单个变体ID变量替换为数组格式的排除ID集合,支持存放任意数量的需要排除计税规则的变体ID
  • 用in_array()函数替代原有的等值判断,即可校验当前商品变体是否属于排除列表,实现多ID匹配
  • 逻辑完全匹配需求:排除列表内的变体不管用户身份都正常计税;仅持有ResellerCO角色的用户购买非排除列表的商品/变体时,才适用优惠税率实现免税

内容的提问来源于stack exchange,提问作者Brienne Friedman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 22:24:03