如何将 WooCommerce 中的网关费用分配给多个用户角色?

How to Assign a Gateway Fee in WooCommerce to Multiple User Roles?

我在 WooCommerce 上建立了一个网上商店,既可以零售也可以批发。但是信用卡手续费太他妈贵了。我很乐意为我的零售客户支付信用卡费用,但如果我的经销商选择使用信用卡支付,我想向他们收费。

我设法想出了以下非常有效的代码。但是我有两种不同类型的经销商,所以我需要扩展代码,但我不确定如何去做。

我正在尝试扩展这段代码以包含三个不同的用户角色:

角色 1:批发商 - 非增值税注册 角色 2:default_wholesaler 角色 3:管理员

知道怎么做吗?

   add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee_for_gateway' );

function bbloomer_add_checkout_fee_for_gateway() {
    if ( ! current_user_can( 'wholesaler-non-vat-registered' ) )
    return;

  global $woocommerce;

  $chosen_gateway = $woocommerce->session->chosen_payment_method;

  if ( $chosen_gateway == 'cardgatecreditcard' ) {

    $percentage = 0.085;
    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;   
  $woocommerce->cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true, '');

  }

}

此代码也有效,但它也会向已登录的客户添加信用卡费用。所以该代码适用于客人,但一旦客户登录他们的帐户,他们就会被收取信用卡费用。

add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee_for_gateway' );
function bbloomer_add_checkout_fee_for_gateway() {
global $woocommerce;
$user_role = wp_get_current_user();
$order_fee_roles = array( 'customer', 'wholesale', 'administrator' );
if (! is_user_logged_in() && !in_array($order_fee_roles, $user_role->roles ))
return;
$chosen_gateway = WC()->session->get( 'chosen_payment_method' );
if ( $chosen_gateway == 'cardgatecreditcard' ){
$percentage = 0.085;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;   
$woocommerce->cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true, '');  
}}

add_action( 'woocommerce_review_order_before_payment', 'bbloomer_refresh_checkout_on_payment_methods_change' ); 
function bbloomer_refresh_checkout_on_payment_methods_change(){ ?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}

这是感谢 Sara McMahon 的解决方案。很有魅力!

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

    if (!(is_checkout() && !is_wc_endpoint_url()))
        return;

    if (!is_user_logged_in())
        return;

    $user = wp_get_current_user();
    $roles = (array) $user->roles;
    $roles_to_check = array('administrator', 'default_wholesaler', 'wholesaler-non-vat-registered');
    $compare = array_diff($roles, $roles_to_check);

    if (empty($compare)){
        $payment_method = WC()->session->get('chosen_payment_method');
        if ($payment_method == 'cardgatecreditcard'){
            $percentage = 0.085;
            $surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
            $cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true );
        }
    }
}

第二个代码的问题可能是您设置的特定条件。

$order_fee_roles = array( 'customer', 'wholesale', 'administrator' ); 如果 (!is_user_logged_in() && !in_array($order_fee_roles, $user_role->角色 ))

您可以考虑将它们分解,以便于测试。所以首先检查用户是否登录。然后在里面检查他们是否登录然后检查用户角色。

您在设置条件时遇到的一个问题是您说“如果用户未登录并且不是这些角色之一,则不要 运行”。当用户注销时,他们没有角色,当用户登录时,此代码不适用。这就是为什么 运行ning 适合所有人。要设置条件以便检查用户是否已登录,只需删除感叹号即可。这会将您的支票更改为 'if the user is logged in and not one of the following roles'...

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

    if (!(is_checkout() && !is_wc_endpoint_url()))
        return;

    if (!is_user_logged_in())
        return;

    $user = wp_get_current_user();
    $roles = (array) $user->roles;
    $roles_to_check = array('administrator', 'default_wholesaler', 'wholesaler-non-vat-registered');
    $compare = array_diff($roles, $roles_to_check);

    if (empty($compare)){
        $payment_method = WC()->session->get('chosen_payment_method');
        if ($payment_method == 'cardgatecreditcard'){
            $percentage = 0.085;
            $surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
            $cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true );
        }
    }
}