php - Add fee based on specific payment methods in WooCommerce

Php - Add fee based on specific payment methods in WooCommerce

To add a fee based on specific payment methods in WooCommerce using PHP, you can use WooCommerce hooks and filters to modify the checkout process. Here's a basic example of how you can achieve this:

// Add a custom fee based on payment method function add_custom_fee_based_on_payment_method( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Define the payment methods and their corresponding fees $payment_method_fees = array( 'paypal' => 2.50, 'credit_card' => 1.50, // Add more payment methods and their fees here ); // Get the chosen payment method $chosen_payment_method = WC()->session->get( 'chosen_payment_method' ); // Add a fee if the chosen payment method is found in the array if ( isset( $payment_method_fees[ $chosen_payment_method ] ) ) { $fee = $payment_method_fees[ $chosen_payment_method ]; $cart->add_fee( __( 'Payment Method Fee', 'your-text-domain' ), $fee, false ); } } add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee_based_on_payment_method', 10, 1 ); 

In this code:

  1. We define a function add_custom_fee_based_on_payment_method that adds a fee based on the chosen payment method.
  2. We define an array $payment_method_fees where the keys are the payment method slugs and the values are the corresponding fees.
  3. We get the chosen payment method using WC()->session->get( 'chosen_payment_method' ).
  4. If the chosen payment method is found in the array, we add the corresponding fee using $cart->add_fee().
  5. We hook this function to the woocommerce_cart_calculate_fees action, which is called when cart fees are calculated.

Make sure to adjust the payment method slugs (paypal, credit_card, etc.) and their corresponding fees according to your setup.

Place this code in your theme's functions.php file or in a custom plugin. Remember to test this thoroughly on a staging site before applying it to a production environment.

Examples

  1. How to add a fee for specific payment methods in WooCommerce?

    Description: This snippet adds a fee when a specific payment method (e.g., "cod" for Cash on Delivery) is selected during checkout.

    add_action('woocommerce_cart_calculate_fees', 'add_payment_method_fee'); function add_payment_method_fee() { if (is_admin() && !defined('DOING_AJAX')) { return; } $chosen_payment_method = WC()->session->get('chosen_payment_method'); if ($chosen_payment_method === 'cod') { WC()->cart->add_fee(__('COD Fee', 'woocommerce'), 5); } } 
  2. WooCommerce add fee for PayPal payments

    Description: This code adds a fee when the PayPal payment method is selected.

    add_action('woocommerce_cart_calculate_fees', 'add_paypal_fee'); function add_paypal_fee() { if (is_admin() && !defined('DOING_AJAX')) { return; } $chosen_payment_method = WC()->session->get('chosen_payment_method'); if ($chosen_payment_method === 'paypal') { WC()->cart->add_fee(__('PayPal Fee', 'woocommerce'), 3); } } 
  3. Adding a dynamic fee based on the chosen payment method in WooCommerce

    Description: This example shows how to add a dynamic fee based on different payment methods.

    add_action('woocommerce_cart_calculate_fees', 'add_dynamic_payment_method_fee'); function add_dynamic_payment_method_fee() { if (is_admin() && !defined('DOING_AJAX')) { return; } $chosen_payment_method = WC()->session->get('chosen_payment_method'); $fees = [ 'cod' => 5, 'paypal' => 3, 'stripe' => 2 ]; if (array_key_exists($chosen_payment_method, $fees)) { WC()->cart->add_fee(sprintf(__('%s Fee', 'woocommerce'), ucfirst($chosen_payment_method)), $fees[$chosen_payment_method]); } } 
  4. Apply a surcharge for credit card payments in WooCommerce

    Description: Adds a surcharge specifically for credit card payments.

    add_action('woocommerce_cart_calculate_fees', 'add_credit_card_surcharge'); function add_credit_card_surcharge() { if (is_admin() && !defined('DOING_AJAX')) { return; } $chosen_payment_method = WC()->session->get('chosen_payment_method'); if ($chosen_payment_method === 'stripe') { $surcharge = WC()->cart->subtotal * 0.02; // 2% surcharge WC()->cart->add_fee(__('Credit Card Surcharge', 'woocommerce'), $surcharge); } } 
  5. Adding a handling fee for bank transfer payment method in WooCommerce

    Description: This snippet adds a handling fee for bank transfer payments.

    add_action('woocommerce_cart_calculate_fees', 'add_bank_transfer_handling_fee'); function add_bank_transfer_handling_fee() { if (is_admin() && !defined('DOING_AJAX')) { return; } $chosen_payment_method = WC()->session->get('chosen_payment_method'); if ($chosen_payment_method === 'bacs') { WC()->cart->add_fee(__('Bank Transfer Handling Fee', 'woocommerce'), 2); } } 
  6. WooCommerce add custom fee based on payment method selected

    Description: Adds a custom fee depending on the selected payment method.

    add_action('woocommerce_cart_calculate_fees', 'add_custom_payment_method_fee'); function add_custom_payment_method_fee() { if (is_admin() && !defined('DOING_AJAX')) { return; } $chosen_payment_method = WC()->session->get('chosen_payment_method'); if ($chosen_payment_method === 'cheque') { WC()->cart->add_fee(__('Cheque Fee', 'woocommerce'), 4); } } 
  7. How to add a fee for specific payment methods in WooCommerce checkout

    Description: This code snippet demonstrates adding a fee at checkout for specific payment methods.

    add_action('woocommerce_cart_calculate_fees', 'add_checkout_payment_method_fee'); function add_checkout_payment_method_fee() { if (is_admin() && !defined('DOING_AJAX')) { return; } $chosen_payment_method = WC()->session->get('chosen_payment_method'); if ($chosen_payment_method === 'cash') { WC()->cart->add_fee(__('Cash Payment Fee', 'woocommerce'), 6); } } 
  8. WooCommerce charge additional fee for specific payment gateways

    Description: Charges an additional fee for specific payment gateways.

    add_action('woocommerce_cart_calculate_fees', 'add_gateway_specific_fee'); function add_gateway_specific_fee() { if (is_admin() && !defined('DOING_AJAX')) { return; } $chosen_payment_method = WC()->session->get('chosen_payment_method'); if ($chosen_payment_method === 'payu') { WC()->cart->add_fee(__('PayU Fee', 'woocommerce'), 7); } } 
  9. Adding a fixed fee for WooCommerce payment methods

    Description: Adds a fixed fee for a specific payment method, regardless of the cart total.

    add_action('woocommerce_cart_calculate_fees', 'add_fixed_payment_method_fee'); function add_fixed_payment_method_fee() { if (is_admin() && !defined('DOING_AJAX')) { return; } $chosen_payment_method = WC()->session->get('chosen_payment_method'); if ($chosen_payment_method === 'gateway_name') { WC()->cart->add_fee(__('Fixed Gateway Fee', 'woocommerce'), 10); } } 
  10. WooCommerce add surcharge for cash on delivery payments

    Description: Adds a surcharge specifically for cash on delivery (COD) payments.

    add_action('woocommerce_cart_calculate_fees', 'add_cod_surcharge'); function add_cod_surcharge() { if (is_admin() && !defined('DOING_AJAX')) { return; } $chosen_payment_method = WC()->session->get('chosen_payment_method'); if ($chosen_payment_method === 'cod') { $surcharge = 5; // Fixed surcharge for COD WC()->cart->add_fee(__('COD Surcharge', 'woocommerce'), $surcharge); } } 

More Tags

wifimanager derby mysql-python has-many nested-if axon export virtual-machine shell dhtml

More Programming Questions

More Date and Time Calculators

More Bio laboratory Calculators

More Geometry Calculators

More Mixtures and solutions Calculators