1

我正在寻找一种方法来为 WooCommerce 中的特定用户角色添加额外的购物车/税费。我正在使用WooCommerce PDF 发票和装箱单插件。至少应将此费用添加到发送给用户的电子邮件和随它一起发送的 PDF 发票中。

我看了下面的代码,并认为我可以使用它。我只是不明白如何为产品添加不同的“税级”。所以现在发生的事情是根本没有加税。

 add_filter( 'woocommerce_before_cart_contents', 'wc_diff_rate_for_user', 1, 2 );
 add_filter( 'woocommerce_before_shipping_calculator', 'wc_diff_rate_for_user', 1, 2);
 add_filter( 'woocommerce_before_checkout_billing_form', 'wc_diff_rate_for_user', 1, 2 );
 add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );
 function wc_diff_rate_for_user( $tax_class ) {

if ( !is_user_logged_in() || current_user_can( 'customer' ) ) {
    $tax_class = 'Zero Rate';
}
return $tax_class;
}

我希望任何人都可以帮助我。

4

1 回答 1

1

更新:您使用的代码已过时,不需要。尝试以下示例,该示例将根据用户角色添加百分比费用:

add_action( 'woocommerce_cart_calculate_fees', 'custom_percentage_fee', 20, 1 );
function custom_percentage_fee( $cart ) {
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) )  )
        return;

    // Get current WP_User Object
    $user = wp_get_current_user();

    // For "customer" user role
    if ( in_array( 'customer', $user->roles ) ) {
        $percent = 5; // <== HERE set the percentage
        $cart->add_fee( __( 'Fee', 'woocommerce')." ($percent%)", ($cart->get_subtotal() * $percent / 100), true );
    }
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。

于 2019-04-04T17:40:43.703 回答