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

时间:2020-05-24 14:14:30

标签: php wordpress logic hook-woocommerce 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 );
        }
    }
}

2 个答案:

答案 0 :(得分:0)

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

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

您可以考虑将它们分解以使其更易于测试。因此,首先检查用户是否已登录。然后在该用户内部检查他们是否已登录,然后检查用户角色。

您所设置的条件存在的一个问题是,您说的是“如果用户未登录并且不是这些角色之一,则不要运行”。当用户注销时,他们将没有角色,并且在用户登录时,此代码将不适用。这就是为什么它为所有人运行。要设置条件,以便您检查用户是否登录,只需删除感叹号即可。这会将您的检查更改为“如果用户已登录并且不是以下角色之一” ...

答案 1 :(得分:0)

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 );
        }
    }
}