Home / eCommerce / AffiliateWP – Create Referral from Gravity Forms Dropdown Field
Duplicate Snippet

Embed Snippet on Your Site

AffiliateWP – Create Referral from Gravity Forms Dropdown Field

Creates an AffiliateWP referral when a Gravity Forms form is submitted, assigning credit to the affiliate whose name is selected in a dropdown field. Configure the form ID, dropdown field ID, and the label-to-affiliate-ID mapping array at the top of the snippet. Requires AffiliateWP and Gravity Forms to be active.

Code Preview
php
<?php
<?php
// -----------------------------------------------------------------------
// CONFIGURATION -- edit these values before activating the snippet.
// -----------------------------------------------------------------------
// The ID of the Gravity Forms form this snippet applies to.
// Find it in Forms > All Forms (the number in the ID column).
define( 'AFFWP_GF_PRACTITIONER_FORM_ID', 5 );
// The Field ID of the dropdown field that holds the practitioner name.
// Find it by opening the form in the GF editor and clicking the dropdown
// field -- the field ID appears in the right-hand panel under "Field ID".
define( 'AFFWP_GF_PRACTITIONER_FIELD_ID', 12 );
// Map each dropdown option label to its AffiliateWP affiliate ID.
// The key must match the dropdown choice label exactly (case-sensitive).
// Find affiliate IDs in AffiliateWP > Affiliates (the ID column).
define( 'AFFWP_GF_PRACTITIONER_MAP', serialize( array(
	'Jane Smith'  => 42,
	'Dr. Johnson' => 87,
	'Mike Torres' => 103,
) ) );
// -----------------------------------------------------------------------
// SNIPPET -- do not edit below this line.
// -----------------------------------------------------------------------
add_action( 'gform_after_submission', 'affwp_gf_create_referral_from_dropdown', 20, 2 );
function affwp_gf_create_referral_from_dropdown( $entry, $form ) {
	if ( absint( $form['id'] ) !== AFFWP_GF_PRACTITIONER_FORM_ID ) {
		return;
	}
	if ( ! function_exists( 'affwp_add_referral' ) ) {
		return;
	}
	$field_id       = AFFWP_GF_PRACTITIONER_FIELD_ID;
	$selected_label = isset( $entry[ $field_id ] ) ? sanitize_text_field( $entry[ $field_id ] ) : '';
	if ( empty( $selected_label ) ) {
		return;
	}
	$affiliate_map = unserialize( AFFWP_GF_PRACTITIONER_MAP );
	if ( ! isset( $affiliate_map[ $selected_label ] ) ) {
		return;
	}
	$affiliate_id = absint( $affiliate_map[ $selected_label ] );
	if ( empty( $affiliate_id ) ) {
		return;
	}
	if ( 'active' !== affwp_get_affiliate_status( $affiliate_id ) ) {
		return;
	}
	$entry_id = absint( $entry['id'] );
	$existing_native = affwp_get_referral_by( 'reference', $entry_id, 'gravityforms' );
	$existing_manual = affwp_get_referral_by( 'reference', $entry_id, 'gravityforms_manual' );
	if ( ! is_wp_error( $existing_native ) || ! is_wp_error( $existing_manual ) ) {
		return;
	}
	$description = isset( $form['title'] ) ? sanitize_text_field( $form['title'] ) : '';
	$referral_id = affwp_add_referral( array(
		'affiliate_id' => $affiliate_id,
		'reference'    => $entry_id,
		'description'  => $description,
		'context'      => 'gravityforms_manual',
		'status'       => 'pending',
	) );
	if ( empty( $referral_id ) ) {
		error_log( sprintf(
			'AffiliateWP: Failed to create referral for entry #%d, affiliate #%d.',
			$entry_id,
			$affiliate_id
		) );
	}
}

Comments

Add a Comment