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

Wanderlust Consulting

I've built an online shop on WooCommerce that sells retail as well as wholesale. But credit card fees are so damn expensive. I'm happy to pay the credit card fees for my retail customers but I want to charge my resellers if they choose to pay by credit card.

I managed to come up with the following code that works really well. But I have two different types of resellers so I need to expand the code and I'm not sure how to go about it.

I'm trying to expand this bit of code to include three different user roles:

Role 1: wholesaler-non-vat-registered Role 2: default_wholesaler Role 3: administrator

Any idea how to go about it?

   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, '');

  }

}

This code works too, but it adds the credit card fee to logged in Customers too. So the code works for Guests, but as soon as the customer signs into their account they get charged a credit card fee.

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
}

Here's the solution thanks to Sara McMahon. Works like a charm!

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Assign a percentage fee to WooCommerce payment methods based on user roles

how to Assign Multiple Roles for a user in wordpress?

How to assign Multiple roles to a single user in wordpress

Woocommerce hide payment gateway for user roles

Check WooCommerce User Role and Payment Gateway and if they match - Apply fee

Rolify Assign Multiple User Roles at Once

Disable WooCommerce payment gateway for guests and specific user roles

Add a fee for specific user roles when "Ship to a different address?" has been checked on WooCommerce checkout

How to assign roles to a user with devise, cancan and rolify?

How to automatically assign USER ROLES in Oracle Apex

Add a custom fee for a specific payment gateway in Woocommerce

Assign roles to user

Is it possible to assign multiple roles to a user or group in Azure AD?

Show WooCommerce products only to multiple authorized user roles

How to assign multiple roles to multiple service accounts in GCP using Terraform?

WooCommerce - Adding shipping fee for free user plan

Laravel 5.4 how to assign roles to user by form request?

How to add user roles dropdown in registration and login woocommerce wordpress

Add a fee based on chosen payment gateways and user roles

Allow WooCommerce Gateway Percentage Fee to work also on order pay

Make taxable a WooCommerce custom fee based on payment gateway and country

Multiple User Roles in Authorize

How to make a custom cart fee to be taxable in WooCommerce

How to give a unique style to a fee on WooCommerce

How to create multiple roles for single user in ruby on rails?

how to add multiple roles to an admin user in Apache tomcat 8?

how to add multiple roles to an admin user in Apache tomcat 8?

How to implement multiple user types with different roles and permissions in Django?

How to structure a .NET Core MVC project for multiple user roles?