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.
/* 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
    ?>
    
    

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.