Pricing Process Reviews Contact

Divi Guy blog

Increase cart item prices based on payment method in WooCommerce.

This function increases the cart item prices based on the payment method enabled in WooCommerce.

/* 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 a hand?

Need help with your Divi website?

Book a Divi consultation and we can fix it together over Zoom, or ask for an estimate for bigger jobs.

0 Comments

Submit a Comment

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

Search

Browse

More tips

Got a question?

Ask me anything

Stuck on something in Divi or WordPress? Ask away and I will reply by email, usually within 24 hours.

Ask a Question

Tell me about your project. The more details the better.

"*" indicates required fields

Make a payment

Got an invoice?

Pay an invoice, a deposit or a tip in about a minute. Card or PayPal, secure checkout, and I never see your card number.

Make a payment