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.
Learn how to manage payment gateway fees for WooCommerce and apply extra charges for specific 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.
/**
* 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.

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.
