How to multiply WooCommerce custom product fees by gravity forms field values

I’m working on a WooCommerce site where I need to calculate dynamic fees based on product metadata and form selections. I’ve successfully implemented custom fees for each product using meta fields, and they show up correctly in the shopping cart. However, I’m stuck on how to multiply these fees by a number value that comes from a Gravity Forms dropdown selection. I have the WooCommerce Gravity Forms integration plugin installed.

add_action('woocommerce_cart_calculate_fees', 'calculate_dynamic_product_fees');
function calculate_dynamic_product_fees() {
    global $woocommerce;
    if (is_admin() && !defined('DOING_AJAX'))
        return;
    
    // Iterate through cart items to calculate additional charges
    foreach ($woocommerce->cart->get_cart() as $item_key => $cart_data) {
        // Retrieve product details
        $product_obj = $cart_data['data'];
        
        // Fetch custom meta value
        $additional_charge = get_post_meta($product_obj->id, 'extra_charges', true);
        
        // Sum up the additional fees
        $total_extra_charge = $total_extra_charge + $additional_charge;
    }
    
    // Apply fee if it exists
    if ($total_extra_charge) {
        $woocommerce->cart->add_fee('Additional Charges', $total_extra_charge, true, 'standard') * $field["choices"][0]["value"];
    }
}

Basic calculation example:

$base_fee = "15";
$adults = 3;
$children = 1;
$total_people = $adults + $children; // equals 4
$final_fee = $base_fee * $total_people; // equals "60"

How can I properly access and multiply by the Gravity Forms field value?

You’ve got a syntax error - you’re trying to multiply the add_fee() method result, which won’t work. Calculate the multiplied value first, then pass it to the fee function. The main problem is getting Gravity Forms data during cart calculation. With the WooCommerce Gravity Forms plugin, form data gets stored in the cart item’s metadata. Try $cart_data['gravity_forms_history'] or debug what’s actually in $cart_data using error_log(print_r($cart_data, true)). Here’s the fix: grab the multiplier from your form data, calculate $final_fee = $total_extra_charge * $multiplier_value, then call add_fee() with that final amount. Cart item structure changes between plugin versions, so debug the data structure first - it’ll save you headaches later.

Yeah that code’s totally broken. WooCommerce hooks + Gravity Forms integration gets messy real quick.

I hit something similar last year with dynamic pricing based on form selections and product metadata. The syntax isn’t even your main problem - you’re trying to make WordPress systems work together when they weren’t built for it.

I moved the whole calculation outside WordPress. Used Latenode to handle form submissions, grab WooCommerce product data through their API, run the math, then push calculated fees back to the cart.

Latenode connects to both Gravity Forms webhooks and WooCommerce’s REST API easily. Clean data flow without fighting WordPress hooks or debugging cart metadata.

When your client wants more complex pricing rules or other system connections (and they will), you won’t be rewriting PHP functions. Everything stays in one visual workflow that actually works.

Check it out if you’re tired of WordPress integration headaches: https://latenode.com

I’ve been fighting this same setup for weeks! Your multiplication syntax is wrong, but the real problem is session data. Gravity Forms doesn’t reliably store field values in cart metadata - they disappear between page loads or cart updates. Store the form values in user session first with $_SESSION or WC()->session->set() when the form submits. Then pull from session during fee calculation instead of trusting cart item data.

Just checked your math logic and fee calculation - you’re close but missing something important.

I built this exact thing for a client’s booking system. The real trick isn’t just grabbing form data, it’s timing when you grab it.

Your woocommerce_cart_calculate_fees hook runs too late. Switch to woocommerce_before_calculate_totals instead - you’ll get cleaner access to cart data before WooCommerce locks it down.

For Gravity Forms, field values usually end up in $cart_data['_gravity_form_lead'] with the official plugin. Hunt for your field ID in that array.

Here’s what actually worked:

foreach ($cart->get_cart() as $item_key => $cart_data) {
    $form_data = $cart_data['_gravity_form_lead'] ?? [];
    $multiplier = $form_data['input_3'] ?? 1; // replace '3' with your field ID
    
    $base_fee = get_post_meta($cart_data['product_id'], 'extra_charges', true);
    $calculated_fee = $base_fee * $multiplier;
    
    if ($calculated_fee > 0) {
        $cart->add_fee('Additional Charges', $calculated_fee);
    }
}

Dump the whole $cart_data array first - every setup stores form data differently and you need to see your exact structure.

The problem is getting form field values during WooCommerce’s fee calculation. With the WooCommerce Gravity Forms plugin, form data gets attached to cart items, but the structure changes depending on your plugin version and settings.

I ran into this same issue when product fees needed to be multiplied by form selections. Form data usually lives in $cart_data['_gravity_form_data'] or sometimes $cart_data['_gravity_forms_history']. You’ll have to parse through this array to grab your specific field.

What worked for me: find your field ID from the form first, then pull the value during cart calculation. Try $form_data = maybe_unserialize($cart_data['_gravity_form_data']) and loop through to find your multiplier field. Don’t forget to sanitize and validate that form value before you use it in calculations.

Also, there’s a syntax error in your code - calculate the final amount first, then pass it to add_fee(). Do the multiplication before calling the WooCommerce method, not after.