Skip to content

Add a Free Product to a Gravity Forms Order with a Coupon Code

Want to give away a free bonus item when someone uses a coupon code in Gravity Forms? Here is a complete code snippet using the gform_product_info filter.

Chris Eggleston 3 min read

The Problem

You’re running a promotion and want to give away a free bonus item when someone enters a coupon code. Think “Use code FREEGIFT to get a free sticker with your order.”

The Gravity Forms Coupons Add-On handles discounts well, but it only takes money off. It can’t add a product to the order, so the free item never shows up in the entry, the receipt or your fulfillment list. Your team has no way to know the bonus was promised.

The Solution

Use the gform_product_info filter to add a $0 line item whenever your coupon code was applied.

Step 1: Add a Hidden Product Field for the Free Item

  1. Open your form in the form editor.
  2. Add a Product field from Pricing Fields.
  3. Set the Product Name to the free item (for example, “Free Sticker”) and the Price to 0.00.
  4. Under Advanced → Visibility, choose Hidden.
  5. Note the field ID. You’ll need it in the code.

Step 2: Add the Coupon Field

  1. Add a Coupon field from Pricing Fields.
  2. Create your coupon under Forms → Coupons. Set the discount however you like. If the code should only unlock the gift, use a flat amount of 0.00.

Step 3: Add the Code

Add this with a code snippet plugin like WPCodeBox or a custom plugin:

/**
 * Add a free product to the order when a specific coupon code is used.
 *
 * Title: Add Free Product via Coupon Code
 * Author: Gravity Ranger
 * Version: 1.1
 */
add_filter( 'gform_product_info', 'gr_add_free_product_via_coupon', 10, 3 );
function gr_add_free_product_via_coupon( $product_info, $form, $entry ) {
	// ---- CONFIGURE THESE VALUES ----
	$form_id            = 5;           // Your form ID.
	$target_coupon_code = 'FREEGIFT';  // Your coupon code (not case-sensitive).
	$free_product_id    = 7;           // The ID of your hidden Product field.
	$free_product_name  = 'Free Sticker';
	// ---------------------------------

	if ( (int) $form['id'] !== $form_id ) {
		return $product_info;
	}

	// The Coupons Add-On saves the applied codes in the Coupon field, separated by commas.
	$codes = array();
	foreach ( $form['fields'] as $field ) {
		if ( 'coupon' === $field->type ) {
			$codes = array_merge( $codes, array_map( 'trim', explode( ',', (string) rgar( $entry, (string) $field->id ) ) ) );
		}
	}

	if ( ! in_array( strtolower( $target_coupon_code ), array_map( 'strtolower', $codes ), true ) ) {
		return $product_info;
	}

	// Add the free item as a $0 line item.
	$product_info['products'][ $free_product_id ] = array(
		'name'     => $free_product_name,
		'price'    => '0.00',
		'quantity' => 1,
		'options'  => array(),
	);

	return $product_info;
}

Configuration Notes

  • 5: your form ID.
  • FREEGIFT: your coupon code. Upper or lower case doesn’t matter.
  • 7: the ID of the hidden Product field from Step 1.
  • Free Sticker: the name of your free item, as it should appear on the order.

Why This Works

Gravity Forms builds the order’s product list with gform_product_info whenever it needs it: for the payment, the order summary on the entry, and the {all_fields} and {products} merge tags in notifications. Adding the item there means it shows up everywhere, at a price of $0.00, so the total doesn’t change.

Limitations & Caveats

  • It isn’t shown on the form before submission. The free item appears in the entry, receipt and emails, not in the live total on the form. If people need to see it before submitting, use a product field with conditional logic instead, or a perk like Gravity Wiz’s GP eCommerce Fields.
  • The hidden Product field must exist in the form, so the line item matches a real field.
  • Payment gateways may leave $0 items off their own receipts. The Gravity Forms entry and notifications will still show it.

Need More Help?

If you still need help, use the chat or leave a comment below and we’ll get back to you.

Comments

Ask a question or leave a comment

Your email is never shown. For help with a specific form, share what you expected and what happened instead.