Fee depending on Payment Gateway

Viewed 14

Do you know if there is a way to include a fee or discount depending on the type of payment chosen, but as open as possible?

I currently have this code, but it doesn't always pop up. I have some plugins active and I guess it doesn't detect all the possibilities.

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 = 5.435; // 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
                   //$resultado = $fees_calc - $discount;
                     //// Adding the discount
                   //$cart_object->add_fee( $label_text,-$resultado, 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 );

This code pops up in the traditional WooCommerce payment gateway (where you see the checkout form, payment types, etc.), but not in a WooCommerce checkout also called by another plugin, deposits for WooCommerce. I understand that there must be a broader way to call that code

Lot of thanks!

1 Answers

The code is working fine. but you have a few typos and need some improvements.

Here is the fixed & updated code.

/**
 * Add extra discount for specific payment methods.
 *
 * @param WC_Cart $cart_object WC cart object.
 */
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    = 5.435; // 4.755%
    $cart_total = $cart_object->subtotal_ex_tax;

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

    if ( $payment_method === $chosen_payment_method ) {
        $label_text = __( 'Discount International Charge', 'text-domain' );

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

        // Adding the discount.
        $cart_object->add_fee( $label_text, -$discount, false );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'ts_add_discount', 10, 1 );

/**
 * Custom JS to trigger update checkout on payment change
 */
function ts_refresh_payment_method() {
    if ( ! is_checkout() ) {
        return;
    }
    ?>
    <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', '#payment input[name^="payment_method"]', function() {
                $('body').trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
}
add_action( 'wp_footer', 'ts_refresh_payment_method' );
Related