gform_product_total

Description

This function fires before updating the Total value for forms with pricing fields. It allows users to implement custom logic to manipulate the Total value.

Note: This filter only applies to the form front-end JavaScript and will only change the display of the Total. You must also implement the gform_product_info filter to ensure the total is displayed correctly on the entry pages and sent properly to third-party services via add-ons such as the PayPal Add-On.

Usage

gform.addFilter( 'gform_product_total', function(total, formId){
    // do something with the total
    return total;
 } );

Parameters

  • total float
    The total to be filtered.
  • formId integer
    The ID of the form in use.

Examples

Add a value to the total

This example adds $50 to the total price if the quantity field is greater than 100.

gform.addFilter('gform_product_total', function(total, formId) {
    // Only apply logic to form ID 165
    if (formId !== 165) {
        return total;
    }

    const quantityInput = document.querySelector(".ginput_quantity");
    if (quantityInput && parseInt(quantityInput.value, 10) > 100) {
        total += 50;
    }

    return total;
});

Prevent totals less than zero

// Prevent the total from being less than zero
gform.addFilter('gform_product_total', function(total, formId) {
    return total < 0 ? 0 : total;
});

Placement

Your code snippet can be placed in an HTML field on your form or in a theme custom JavaScript file.

See also the JavaScript/jQuery section in this article: Where Do I Put This Code?

Source Code

This filter is located in js/gravityforms.js