MichelH

Add Fees on Payment Method

Apr 5th, 2023 (edited)
44
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.42 KB | Source Code | 0 0
  1. add_action( 'woocommerce_cart_calculate_fees', 'payment_fees' );
  2. function payment_fees ( $cart ) {
  3.     if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  4.         return;
  5.  
  6.     $chosen_payment_id = WC()->session->get('chosen_payment_method');
  7.  
  8.     if ( empty( $chosen_payment_id ) )
  9.         return;
  10.  
  11.     $subtotal = $cart->cart_contents_total + $cart->shipping_total ;
  12.  
  13.     // SETTINGS: Here set in the array the (payment Id) / (fee cost) pairs
  14.     $targeted_payment_ids = array(
  15.         'ppcp-gateway' => ((6.5 * $subtotal / 100)+1)* 1.05  , // Percentage fee
  16.         'ppcp-card-button-gateway' => ((6.5 * $subtotal / 100)+1)* 1.05  , // Percentage fee
  17.         'stripe' => ((3.9 * $subtotal / 100)+1)* 1.05  , // Percentage fee
  18.         'cod' => ((3 * $subtotal / 100)+0.2)* 1.05  , // Percentage fee
  19.         'wallet' => ((2.9 * $subtotal / 100)+0.1)* 1.05  , // Percentage fee
  20.         'bacs' => ((2.99 * $subtotal / 100)+1)* 1.05  , // Percentage fee
  21.     );
  22.  
  23.     // Loop through defined payment Ids array
  24.     foreach ( $targeted_payment_ids as $payment_id => $fee_cost ) {
  25.         if ( $chosen_payment_id === $payment_id ) {
  26.             if ($payment_id === 'cod' || $payment_id === 'wallet') {
  27.                 $cart->add_fee( __('Handling & Service Fees', 'woocommerce'), $fee_cost, true );
  28.             } else {
  29.                 $cart->add_fee( __('Payment Fees', 'woocommerce'), $fee_cost, true );
  30.             }
  31.         }
  32.     }
  33. }
  34.  
Advertisement
Comments
  • MichelH
    3 years
    # text 0.10 KB | 0 0
    1. If payment is COD or Wallet it shows Handling & Service Fees
    2.  
    3. For other payment, it show Payment fees
Add Comment
Please, Sign In to add comment