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

Tags: , , , ,



How do I expand this snippet to include 2 more payment gateways with different fees?

The payment gateways I want to add are ‘cardgategiropay’ and ‘cardgateideal’ and the fees are 3% and 2% respectively .

    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('order-pay') ) )
        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', 'shop_manager');
    $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 );
        }
    }
}

This code works fine. Just checking if my syntax is correct.

    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', 'shop_manager');
    $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 );
        }
        if ($payment_method == 'cardgateideal'){
            $percentage = 0.02;
            $surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
            $cart->add_fee( 'Card Fee (2%)', $surcharge, true );
        }
        if ($payment_method == 'cardgategiropay'){
            $percentage = 0.03;
            $surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
            $cart->add_fee( 'Card Fee (3%)', $surcharge, true );
        }
    }
}

Answer

You could also array_intersect() to check user roles, use IF / ELSE statements, instead of only multiple IF statements and optimize and compact the code as follows:

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

    // Only on checkout page and logged in users
    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) || ! is_user_logged_in() )
        return;

    $wpuser_object = wp_get_current_user();
    $allowed_roles = array('administrator', 'default_wholesaler', 'wholesaler-non-vat-registered', 'shop_manager');

    if ( array_intersect($allowed_roles, $wpuser_object->roles) ){
        $payment_method = WC()->session->get('chosen_payment_method');

        if ( 'cardgatecreditcard' === $payment_method ){
            $percentage = 8.5;
        }
        elseif ( 'cardgateideal' === $payment_method ){
            $percentage = 2;
        }
        elseif ( 'cardgategiropay' === $payment_method ){
            $percentage = 3;
        }
    }
    if ( isset($percentage) ) {
        $surcharge = ($cart->cart_contents_total + $cart->shipping_total) * $percentage / 100;
        $cart->add_fee( sprintf( __('Card Fee (%s)', 'woocommerce'), $percentage . '%' ), $surcharge, true );
    }
}

Also something is missing to allow refresh checkout on payment gateway change:

// jQuery - Update checkout on payment method change
add_action( 'wp_footer', 'custom_checkout_jquery_script' );
function custom_checkout_jquery_script() {
    if ( ( is_checkout() && ! is_wc_endpoint_url() ) && is_user_logged_in() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('form.checkout').on('change', 'input[name="payment_method"]', function(){
            $(document.body).trigger('update_checkout');
        });
    });
    </script>
    <?php
    endif;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.


Related: Add fee based on specific payment methods in WooCommerce



Source: stackoverflow