2017-03-15 51 views
2

在WooCommerce中,我试图为客户和访客用户应用set_is_vat_exempt()方法。为非登录用户禁用税收

对于记录的客户它工作正常。任何人都可以建议我该怎么做?

有一个问题可能是因为用户未登录,因此可能是$woocommerce->customer无法启用它。

这里是我的代码:

$bilalId = get_current_user_id(); 

add_filter('woocommerce_cart_totals_order_total_html', 'wc_cart_totals_order_total_html_bn'); 

function wc_cart_totals_order_total_html_bn() { 
    global $woocommerce; 

    if(current_user_can('customer' || $bilalId == 0)) { 

       $woocommerce->customer->set_is_vat_exempt(true); 

     } 

} 

最后,我只想禁用对用户不登录的任何税率。

即使我试过“零率”,但不适合我。

任何类型的指导将不胜感激。

谢谢。

回答

3

所以,你需要的是使用WordPress的条件is_user_logged_in()init动作钩子钩住一个自定义的功能,这样一来:

add_action('init', 'wc_tax_exempt_unlogged'); 
function wc_tax_exempt_unlogged() { 

    // Getting user data for logged users 
    if(is_user_logged_in()){ 
     $current_user = wp_get_current_user(); 
     $current_user_id = $current_user->ID; 
     $current_user_roles = $current_user->roles; 
     $bilal_id = 0; 
    } 

    // Exempting of VAT non logged users, customers and the main admin ID (you) 
    if(! is_user_logged_in() || in_array('customer', $current_user_roles) || $bilal_id == $current_user_id){ 
     WC()->customer->set_is_vat_exempt(true); 
    } 
} 

的代码放在您的活动子function.php文件主题(或主题)或任何插件文件。

此代码已经过测试并可正常工作。

+0

不工作:(仍然显示错误的价格 –

+0

@BilalHussain我已经测试过,它在我的测试服务器上适用于我...此代码从购物车总计和结帐总计中删除税款 – LoicTheAztec

+0

这可能是主题问题?如果我一旦打开结账页面,完美的工作,但清除缓存后再次显示错误? –