- Joined
- Aug 17, 2022

- Messages
- 1

- Reaction score
- 0

I'm new here, and maybe I'm posting wrong. For previous advice, it's an issue about WordPress, two plugins and one code.

If that's the place, or WordPress is supported here, here I go!

I'm using "deposits for Woocommerce" from Pluginhive and Woocommerce, and I have a question with a code I want to implement.

I would like to be able to charge a small amount (2%) to people who use the credit card payment. I am using the plugin "WooCommerce Payment Gateway Based Fees" for this.

Everything would be great if that would work, but there's a problem.

Deposits for Woocommerce allows you to split the payment, I split it into 30% for the first payment and 70 for the rest. But the plugin "WooCommerce Payment Gateway Based Fees" calculates 2% of the total, that is, not 30% but 100%. But when it comes to collecting the second payment, it does calculate 60% of the payment correctly.

I have tried to include codes like the following to change the plugin, but what happens is that they load perfectly in the first payment (they calculate 30%), BUT they don't work in the second one, so they don't add anything. So, I thought I could use that code to reduce the rate on the first payment (as a hidden discount).

Code:

add_action( 'woocommerce_cart_calculate_fees','ts_add_discount', 10, 1 );

function ts_add_discount( $cart_object ) {

if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

// Mention the payment method e.g. cod, bacs, cheque or paypal

$payment_method = 'redsys';

// The percentage to apply

$percent = 4.755; // 4.755%.

$cart_total = $cart_object->subtotal_ex_tax;

$chosen_payment_method = WC()->session->get('$chosen_payment_method'); //Get the selected payment method

if( $payment_method == $chosen_payment_method ){

$label_text = __( "Discount International Charge" );

// Calculating percentage

$discount = number_format(($cart_total / 100) * $percent, 2);

// Adding the discount

$cart_object->add_fee( $label_text, -$discount, false );

$label_text = __( "Result" );

//// Calculating percentage

//$result = $fees_calc - $discount;

//// Adding the discount

//$cart_object->add_fee( $label_text,-$result, false );

}

}

add_action( 'woocommerce_review_order_before_payment', 'ts_refresh_payment_method' );

function ts_refresh_payment_method(){

// jQuery

?>

<script type="text/javascript">

(function($){

$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {

$('body').trigger('update_checkout');

});

})(jQuery);

</script>

<?php

}

add_action( 'woocommerce_cart_calculate_fees','ts_add_discount', 10, 1 );

So far everything seemed to be working correctly. I then realized that there is a bug: When someone tries to pay 100% directly, the plugin correctly calculates 2% of the total price, but my code jumps too, reducing that 2% and actually creating a discount.

I have tried several codes, but none of them works on the second payment. Only the plugin has managed to work there, so there must be some code or string that uses that plugin to affect the price of that second payment, which my code is not detecting.

I would be delighted if anyone could help with that code.

Thanks!