Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
00 Days
00 Hours
00 Minutes
00 Seconds

How to Add Payment Gateway Fees for Specific Products in WooCommerce?

Not all payment gateways cost the same. Popular options like PayPal or Stripe often charge a percentage-based fee on every transaction, which can significantly impact a store owner’s profitability, especially when selling high-margin items.

That’s why the ability to add payment gateway fees on a per-product basis in WooCommerce can be extremely useful.

In this guide, we’ll show you how to apply gateway-specific fees or discounts for individual products using custom code.

Solution: Add Payment Gateway Fees for Specific Products in WooCommerce

This code snippet adds a custom payment gateway fee for a specific product in WooCommerce, but only when a certain payment method is selected during checkout.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/**
* Store chosen payment method on checkout update.
*/
add_action( 'woocommerce_before_calculate_totals', function() {
if ( isset( $_POST['payment_method'] ) ) {
WC()->session->set( 'chosen_payment_method', wc_clean( wp_unslash( $_POST['payment_method'] ) ) );
}
});
/**
* Add gateway-specific fee per product ID.
*/
add_action( 'woocommerce_cart_calculate_fees', 'ts_custom_product_gateway_fee_only' );
function ts_custom_product_gateway_fee_only( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( ! is_checkout() ) return;
$chosen_gateway = WC()->session->get( 'chosen_payment_method' );
if ( ! $chosen_gateway ) return;
$rules = [
[
'product_id' => 2301, // Replace with your product ID
'gateway' => 'ppcp-gateway', // Payment method ID
'amount' => 10,
'label' => 'PayPal Fee for Product A',
],
];
foreach ( $cart->get_cart() as $cart_item ) {
foreach ( $rules as $rule ) {
if ( (int) $cart_item['product_id'] === (int) $rule['product_id'] && $chosen_gateway === $rule['gateway'] ) {
$fee_amount = $rule['amount'] * $cart_item['quantity'];
$cart->add_fee( $rule['label'], $fee_amount, false );
}
}
}
}
/** * Store chosen payment method on checkout update. */ add_action( 'woocommerce_before_calculate_totals', function() { if ( isset( $_POST['payment_method'] ) ) { WC()->session->set( 'chosen_payment_method', wc_clean( wp_unslash( $_POST['payment_method'] ) ) ); } }); /** * Add gateway-specific fee per product ID. */ add_action( 'woocommerce_cart_calculate_fees', 'ts_custom_product_gateway_fee_only' ); function ts_custom_product_gateway_fee_only( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; if ( ! is_checkout() ) return; $chosen_gateway = WC()->session->get( 'chosen_payment_method' ); if ( ! $chosen_gateway ) return; $rules = [ [ 'product_id' => 2301, // Replace with your product ID 'gateway' => 'ppcp-gateway', // Payment method ID 'amount' => 10, 'label' => 'PayPal Fee for Product A', ], ]; foreach ( $cart->get_cart() as $cart_item ) { foreach ( $rules as $rule ) { if ( (int) $cart_item['product_id'] === (int) $rule['product_id'] && $chosen_gateway === $rule['gateway'] ) { $fee_amount = $rule['amount'] * $cart_item['quantity']; $cart->add_fee( $rule['label'], $fee_amount, false ); } } } }
/**
 * Store chosen payment method on checkout update.
 */
add_action( 'woocommerce_before_calculate_totals', function() {
    if ( isset( $_POST['payment_method'] ) ) {
        WC()->session->set( 'chosen_payment_method', wc_clean( wp_unslash( $_POST['payment_method'] ) ) );
    }
});

/**
 * Add gateway-specific fee per product ID.
 */
add_action( 'woocommerce_cart_calculate_fees', 'ts_custom_product_gateway_fee_only' );
function ts_custom_product_gateway_fee_only( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( ! is_checkout() ) return;

    $chosen_gateway = WC()->session->get( 'chosen_payment_method' );
    if ( ! $chosen_gateway ) return;

    $rules = [
        [
            'product_id' => 2301,            // Replace with your product ID
            'gateway'    => 'ppcp-gateway',  // Payment method ID
            'amount'     => 10,
            'label'      => 'PayPal Fee for Product A',
        ],
    ];

    foreach ( $cart->get_cart() as $cart_item ) {
        foreach ( $rules as $rule ) {
            if ( (int) $cart_item['product_id'] === (int) $rule['product_id'] && $chosen_gateway === $rule['gateway'] ) {
                $fee_amount = $rule['amount'] * $cart_item['quantity'];
                $cart->add_fee( $rule['label'], $fee_amount, false );
            }
        }
    }
}

Output

When a customer adds the specific product defined in the code to their cart and selects the specified payment method at checkout, the additional fee is automatically applied.

How to Add Payment Gateway Fees for Specific Products in WooCommerce? - Tyche Softwares
How to Add Payment Gateway Fees for Specific Products in WooCommerce?

Sometimes, instead of charging a fee for a specific product, it’s more practical to apply it based on product type, especially for variable products like clothing with size options or custom-built items.

In such scenarios, you can adjust your checkout logic to Add Payment Gateway Fees for Variable Product Type in WooCommerce, ensuring the fee only appears when a variation is in the cart and a specific payment method is selected.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of


0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible.

Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

By using our site, you acknowledge that you have read and understood our Privacy Policy and Terms & Conditions.