How to modify product label on form submit

The following code can be used to automatically change a product label when a form is submitted.

In this example we have a book prize form –

  • field id 5 is the title of the book as provided by the form user
  • the ‘product’ is the prize and cost for entering the book prize

The form user will see they are registering for the book prize – but in the confirmation, payment gateway and invoice they will see the product along with the title of the book. For example “Prize nomination – My Awesome Book”

If you’re not sure where to place this code I highly recommend you read How to create a WordPress plugin for your custom functions.

Note: the form id and field id will need to be changed to match your form.

If you need this to apply to existing entries see: How to clear product info table cache

add_filter( 'gform_product_info', 'custom_product_info', 10, 3 );
function custom_product_info( $product_info, $form, $entry ) {
    $book_title = rgar( $entry, 5 );
    if ( 43 == $form['id'] && ! empty( $book_title ) ) {
        foreach ( $product_info['products'] as $key => &$product ) {
            $field = GFFormsModel::get_field( $form, $key );
            if ( is_object( $field ) ) {
                $product['name'] .= ' - ' . $book_title;
            }
        }
    }

    return $product_info;
}

For more information on using the gform_product_info filter see: https://www.gravityhelp.com/documentation/article/gform_product_info/

Was this article helpful?

Related Articles