Increase cart item prices based on payment method in WooCommerce.

Jul 31, 2022 | WooCommerce Tips | 0 comments

This function increases the cart item prices based on the payment method enabled in WooCommerce.
<?php
/* Increase cart item prices based on payment method in WooCommerce BEGIN */
add_action( 'woocommerce_cart_calculate_fees','ts_add_discount', 20, 1 );

function ts_add_discount( $cart_object ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
  
$label_text=__("");

$service_charge=0;
    // Mention the payment method e.g. cod, bacs, cheque or paypal
    $cart_total = $cart_object->subtotal_ex_tax;
    $chosen_payment_method = WC()->session->get('chosen_payment_method');  //Get the selected payment method
    if( $chosen_payment_method == "paypal" ){
                  $label_text = __( "PayPal Service Charge" );
                  // The service charge to apply
                  $service_charge=200;        
     }

    else if( $chosen_payment_method == "cod"){
                  $label_text = __( "Credit Card Service Charge" );                   
                  // The service charge to apply 
                  /* $service_charge=3.5;  */
                  $service_charge = (($cart_total / .965) - $cart_total); /* This is dividing to get a 3.5 percent of the price as a fee */
    }
  
    else if( $chosen_payment_method == "bacs"){
                  $label_text = __( "Direct Bank Transfer Service Charge" );
                  $service_charge=100;  
    }

    else if( $chosen_payment_method == "cheque"){
                  $label_text = __( "Cheque Payment Service Charge" );
                  $service_charge=150;
    }
 
    else {
                  $label_text = __( "Credit Card Fee" );
                  $service_charge=0;
    }
        
     // Adding the service charge
        $cart_object->add_fee( $label_text, $service_charge, 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
}
/* Increase cart item prices based on payment method in WooCommerce END */

Need help with this?
Hire me for a consultation and I'll walk you thru the process.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

5 star reviews
"WOW! Money WELL spent. Joseph was incredibly knowledgeable in helping me make some final tweeks to my Divi website. He was very caring and went above and beyond. Will 100% use him for any future Divi website needs."
Claire - CMMC

5 star reviews
"You really get what you pay for with Joe. He does more than simply build a website, he sets up websites for success. Functional, clean, professional designs and he keeps up/implements the latest plugins and technology. Thank you Joe. I will continue to refer you work."
Mark Winger

5 star reviews
"The best WordPress guy I’ve worked with on WordPress, hands-down (and I’ve worked with dozens). Remarkably good with theme customizations with a quick turn. Kept it on budget as well. A rarity with hourly contracts."
Nate Wright

5 star reviews
"Thanks for your prompt reply. You’re always so comprehensive. If we could write another review just for your communication skills, then we would."
Alex N

More Tips

Looking for some deals?

Ad - Web Hosting from SiteGround - Crafted for easy site management. Click to learn more.

Cloudways Hosting

WPEngine Hosting

Pressable Hosting

AccuWeb Hosting

Divi PHP Code Module

Ask a Question

"*" indicates required fields