1
votes

In WooCommerce I need to apply a custom handling fee for a specific payment gateway.

A custom handling fee for percentage cost and a custom handling for per fixed cost.

I have this 2 pieces of codes:

A) PERCENTAGE COST - function

/************************************************************/
/*  PERCENTACE COST 
**/
// Add a custom fee based o cart subtotal
// Add a custom fee based o cart subtotal

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

    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
        return; // Only checkout page

    $payment_method = WC()->session->get( 'chosen_payment_method' );

    if ( 'cod' == $payment_method ) {
        $surcharge = $cart->subtotal * 0.025;
        $cart->add_fee( 'Percentage Cost', $surcharge, true );
    }
}

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

Result frontend

enter image description here


B) FIXED COST - function

/************************************************************/
/*  FIXED COST 
**/
// Add a custom fee based o cart subtotal
add_action( 'woocommerce_cart_calculate_fees', 'custom_fixed_fee', 10, 1 );
function custom_fixed_fee ( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( 'cod' === WC()->session->get('chosen_payment_method') ) {
        $fee = 0.31;
        $cart->add_fee( 'Fixed Cost', $fee, true );
    }
}

// jQuery - Update checkout on methode payment change
add_action( 'wp_footer', 'custom_checkout_jqscript' );
function custom_checkout_jqscript() {
    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;
}

Result frontend

enter image description here



The two distinct functions work perfectly separately. Now, I would like to combine the two functions to get the sum of the two costs.

My code attempt (A + B): function evolution with addition other payment systems

enter image description here

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

     if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
        return; // Only checkout page

    $payment_method = WC()->session->get( 'chosen_payment_method' );

     if ( 'stripe' == $payment_method ) {
        $surcharge = $cart->subtotal * 0.025;
        $fee = 0.31;
    } 
     if ( 'paypal_credit_card_rest' == $payment_method ) {
        $surcharge = $cart->subtotal * 0.036;
        $fee = 0.35;
    }
     if ( 'paypal_express' == $payment_method ) {
        $surcharge = $cart->subtotal * 0.036;
        $fee = 0.35;
    }
     if ( 'bacs' == $payment_method ) {
        return;
    }
  
      $together = $surcharge + $fee;
      $cart->add_fee( 'Plus Cost', $together, true );
}


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

The function returns the following errors:

Notice: Undefined variable: surcharge in /MY-FOLDER..../snippet-ops.php(446) : eval()'d code on line 79

Notice: Undefined variable: fee in /MY FOLDER..../snippet-ops.php(446) : eval()'d code on line 79

That is exactly here:

$together = $surcharge + $fee;

Result frontend

enter image description here

Any advice on how to combine the 2 codes above into 1?

1
If an if condition is met, the variables will be set. However, if this is not the case, you will get the error message 'Undefined variable'. You can solve this by using if / elseif / else where the variables are set in the else condition. You can also use the php isset function or for the if conditions set the variables with a standard value.7uc1f3r

1 Answers

1
votes

The difference between the 2 examples you've posted is really minimal. You can just use:

if ( 'cod' == $payment_method ) {
    $surcharge = $cart->subtotal * 0.025;
    $fee = 0.31;

    $together = $surcharge + $fee;
    $cart->add_fee( 'Plus Cost', $together, true );
}