根据WooCommerce中的用户角色或自定义数据的额外购物车费用

问题描述 投票:1回答:1

我正在寻找一种方法来为WooCommerce中的特定用户角色添加额外的购物车/税费。我正在使用WooCommerce PDF Invoices & Packing Slips插件。此费用至少应添加到发送给用户的电子邮件中以及随其发送的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;
}

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

php woocommerce cart user-roles fee
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文件中。经过测试和工作。

© www.soinside.com 2019 - 2024. All rights reserved.