【问题标题】:Extra cart fee based on user role or custom data in WooCommerce基于 WooCommerce 中用户角色或自定义数据的额外购物车费用
【发布时间】:2019-08-26 09:17:33
【问题描述】:

我正在寻找一种方法来为 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】:

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

    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 文件中。经过测试并且可以工作。

    【讨论】:

    猜你喜欢
    • 2018-04-10
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    相关资源
    最近更新 更多