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

How to Add Payment Gateway Fees for Variable Product Type in WooCommerce?

Store owners often sell variable products, which come with their own unique challenges, especially when combined with specific payment methods or complex fulfillment requirements.

For instance, high-fee payment gateways can increase processing costs, while custom-built or made-to-order items can drive up fulfillment expenses.

To protect profitability without raising prices across the board, a smarter solution is to apply an extra fee to variable products, but only when certain payment gateways are selected at checkout.

Solution: Add Payment Gateway Fees for Variable Product Type in WooCommerce

This code adds an extra fee to variable products in the cart only when a specific payment method is chosen.

/**
 * Store chosen payment method when checkout updates.
 */
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 for variable products.
 */
add_action( 'woocommerce_cart_calculate_fees', 'ts_custom_product_type_gateway_fee' );
function ts_custom_product_type_gateway_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( ! is_checkout() ) return;

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

    // Define your settings:
    $gateway = 'ppcp-gateway'; // Payment method ID (example: PayPal)
    $amount  = 10;              // Fee per unit
    $label   = 'PayPal Fee for Variable Products';

    $total_fee = 0;

    foreach ( $cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];

        if ( $product->get_type() === 'variation' ) {
            $parent_product = wc_get_product( $product->get_parent_id() );
            if ( $parent_product && $parent_product->get_type() === 'variable' && $chosen_gateway === $gateway ) {
                $total_fee += $amount * $cart_item['quantity'];
            }
        }
    }

    if ( $total_fee > 0 ) {
        $cart->add_fee( $label, $total_fee, false );
    }
}

/**
 * Enqueue custom JS to update checkout when payment method changes.
 */
add_action( 'wp_footer', 'ts_add_update_checkout_script' );
function ts_add_update_checkout_script() {
    if ( is_checkout() && ! is_order_received_page() ) :
    ?>
    <script type="text/javascript">
    jQuery(function($){
        $('form.checkout').on('change', 'input[name="payment_method"]', function() {
            $('body').trigger('update_checkout');
        });
    });
    </script>
    <?php
    endif;
}

Output

If a customer adds any variable product to their cart and chooses the payment method defined in the code, an extra fee will be automatically added to the order total.

Add Payment Gateway Fees for Variable Product Type


Not all products carry the same overhead, especially items with higher fulfillment or payment processing costs. In such cases, adding Payment Gateway Fees for Specific Products in WooCommerce gives you more control over your checkout pricing strategy.

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.