2

在 WooCommerce 中,我正在尝试set_is_vat_exempt()为 customer和 guest user应用方法。

对于登录的客户,它工作正常。谁能建议我该怎么做?

一个问题可能是用户未登录,因此可能$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);

         } 

}

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

即使我尝试了“零利率”,但对我不起作用。

任何形式的指导将不胜感激。

谢谢。

4

1 回答 1

3

所以你需要的是is_user_logged_in()在一个挂在动作钩子中的自定义函数中使用 wordpress 条件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 文件中,也位于任何插件文件中。

此代码经过测试并且可以工作。

于 2017-03-15T09:15:24.373 回答