提问者:小点点

商业中基于角色的税收


我正在尝试建立一个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 );
    }
}

这似乎在前端工作,但破坏了后端。将其添加到函数之后。当我回到管理区并看到以下内容时:http://i.imgur.com/nNHMSAZ.png(这只是新的chrome错误页面吗?)

另一件我无法理解的事情是如何添加两个角色而不是一个角色。

谢啦


共2个答案

匿名用户

以下内容适用于我的用户角色“批发商”。添加到函数中。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') ) 

匿名用户

我注意到,当使用“woocommerce\u before\u checkout\u billing\u form”时,您必须首先更新或刷新结帐页面,然后必须刷新购物车页面才能使其生效。

使用这些操作挂钩,“woocommerce\u before\u cart\u contents”和“woocommerce\u before\u shipping\u calculator”使免税生效,而无需首先刷新结账页面。

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