首页 文章

在woocommerce中基于角色的税收

提问于
浏览
5

我正在尝试 Build 一个woocommerce商店,以便具有批发商或设计师角色的用户将自动免税,只需从购物车/结账中消税 . 我使用动态定价插件为不同的角色提供不同的价格,但税收变化没有选项 .

有人发布了这段代码:

// Place the following code in your theme's functions.php file and replace tax_exempt_role with the name of the role to apply to
add_action( 'init', 'woocommerce_customer_tax_exempt' );
function woocommerce_customer_tax_exempt() {
    global $woocommerce;
    if ( is_user_logged_in() ) {
        $tax_exempt = current_user_can( 'tax_exempt_role');
        $woocommerce->customer->set_is_vat_exempt( $tax_exempt );
    }
}

这似乎是在前端工作但打破了后端 . 将它添加到functions.php后,当我回到管理区域并看到:http://i.imgur.com/nNHMSAZ.png(这只是新的Chrome错误页面?)

我无法弄清楚的另一件事是如何添加2个角色而不只是一个角色 .

谢谢

2 回答

  • 2

    以下为我的用户角色“批发商”工作 . 添加到functions.php .

    add_filter( 'woocommerce_before_checkout_billing_form', 'prevent_wholesaler_taxes' );
    
    function prevent_wholesaler_taxes() {
    
         global $woocommerce;
    
         if( current_user_can('wholesaler')) {
    
                  $woocommerce->customer->set_is_vat_exempt(true);
    
             } else {
    
                  $woocommerce->customer->set_is_vat_exempt(false);
             }
    } //end prevent_wholesaler_taxes
    

    要添加多个用户角色,只需添加到 current_user_can(); 函数即可 . 我认为这可行:

    if( current_user_can('wholesaler')||current_user_can('another_user_role') )
    
  • 7

    我注意到在使用 'woocommerce_before_checkout_billing_form' 时,您必须先更新或刷新结帐页面,然后您必须刷新购物车页面才能使其生效 .

    使用这些操作挂钩 'woocommerce_before_cart_contents''woocommerce_before_shipping_calculator' 免税,无需先刷新结帐页面即可生效 .

    注意:使用与上面相同的回调函数代码 .

相关问题