1

I am using Check WooCommerce User Role and Payment Gateway and if they match - Apply fee answer code, and fee is added according to user role ok, but when customer confirms payment, fee is not charged. In my case payment gateway is MercadoPago, something that in my country works like "Paypal".

The fact is that fee is ignored. Any idea?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Welcome to stackoverflow. Please be more specific about your problem. Describe your problem, what did you try, what works and what does not. Please be not offended and read: https://stackoverflow.com/help/how-to-ask – zolty13 Aug 07 '20 at 18:10

1 Answers1

0

Maybe what you need is to find out the payment method id(s) enabled for mercado pago.

The following code will display on checkout page for administrators the payment ID(s) on checkout available payment methods nearby the payment method title:

add_filter( 'woocommerce_gateway_title', 'display_payment_method_id_for_admins_on_checkout', 100, 2 );
function display_payment_method_id_for_admins_on_checkout( $title, $payment_id ){
    if( is_checkout() && ( current_user_can( 'administrator') || current_user_can( 'shop_manager') ) ) {
        $title .= ' <code style="border:solid 1px #ccc;padding:2px 5px;color:red;">' . $payment_id . '</code>';
    }
    return $title;
}

Code goes in functions.php file of your active child theme (or active theme).

enter image description here

Once you found the related payment method id(s) you are looking for, remove this code.


I have revisited Check WooCommerce User Role and Payment Gateway and if they match - Apply fee answer code, handling multiple payment ids:

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

    if ( ! is_user_logged_in() )
        return;

    ## SETTINGS:
    $user_roles  = array( 'vendor', 'administrator' ); // Defined  user roles
    // Targeted payment method Ids
    $payment_ids = array('cod', 'paypal'); 
    $percentage  = 5; // Fee percentage

    $user           = wp_get_current_user();  // Get current WP_User Object
    $chosen_payment = WC()->session->get('chosen_payment_method'); // Choosen payment method
    $cart_subtotal  = $cart->subtotal; // Including taxes - to exclude taxes use $cart->get_subtotal()

    // For matched payment Ids and user roles
    if ( in_array( $chosen_payment, $payment_ids ) && array_intersect( $user->roles, $user_roles ) ) { 
        $fee_cost = $percentage * $cart_subtotal / 100;  // Calculation
        $cart->add_fee( __('Payment Fee'), $fee_cost, true ); // Add fee (incl taxes)
    }
}

Now something was missing: The following that will refresh checkout on payment method choice:

// 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() ) :
    ?>
    <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 your active child theme (or active theme). Tested and works.

Apparently Mercado pago payment plugin doesn't accept Fees.

Similar: Add fee based on specific payment methods in WooCommerce

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Loic, Thank you su much for your reply to my question. I tested code, fee is correctly calculated , added to subtotal and shown at check out, but Mercadopago does not charge fee amount, it only charges subtotal not adding fee, – Marcelo García Aug 07 '20 at 23:39
  • @MarceloGarcía That's not normal, then there is something that is making trouble, it can be a plugin, your theme's customization or some other code made by you… – LoicTheAztec Aug 08 '20 at 00:03
  • 1
    thank you again for your help, after some research I found out that it´s a matter of MercadoPago code, unfortunately it does not allow extra fees... – Marcelo García Aug 10 '20 at 15:36