Skip to content

Instantly share code, notes, and snippets.

@davidwolfpaw
Created September 8, 2016 16:05
Show Gist options
  • Save davidwolfpaw/1013242d18bf58caf10a12d6eb5cff0c to your computer and use it in GitHub Desktop.
Save davidwolfpaw/1013242d18bf58caf10a12d6eb5cff0c to your computer and use it in GitHub Desktop.
Gravity Forms / Sprout Invoices Pro Integration
/**
* Hook into all Gravity Form submissions, check to see if the form id
* matches the one we want to generate an invoice from, and process the
* submission if appropriate.
*
* @param arrat $entry Array of the GF entry
* @return null
*/
function maybe_process_gravity_form_and_create_invoice( $entry = array(), $form = array() ) {
// Change the form ID below
$form_id = 4;
// Only a specific form do this process, if not than stop
if ( $form_id === $entry['form_id'] ) {
return;
}
/**
* The below values need match field id for your custom form
* if your form does not include any information than leave 0
* unless it's a required field.
*/
$subject_field_id = 0;
$name_field_id = 9; // Advanced "name" field
$address_field_id = 14; // Advanced "address" field
$phone_field_id = 11;
$website_field_id = 12;
/**
* These values are required, otherwise a client and user cannot
* be created from the submission
*/
$client_company_name_field_id = 13; // REQUIRED
$email_field_id = 10; // REQUIRED
/**
* Creates variables from submission
*/
$client_name = ( $client_company_name_field_id ) ? $entry[ $client_company_name_field_id ] : '' ;
$email = ( $email_field_id ) ? $entry[ $email_field_id ] : '' ;
$subject = ( $subject_field_id ) ? $entry[ $subject_field_id ] : '' ;
$website = ( $website_field_id ) ? $entry[ $website_field_id ] : '' ;
$contact_phone = ( $phone_field_id ) ? $entry[ $phone_field_id ] : '' ;
$full_name = ( $name_field_id ) ? $entry[ $name_field_id . '.3' ] . ' ' . $entry[ $name_field_id . '.6' ] : '' ;
$contact_street = ( $address_field_id ) ? $entry[ $address_field_id . '.1' ] . ' ' . $entry[ $address_field_id . '.2' ] : '' ;
$contact_city = ( $address_field_id ) ? $entry[ $address_field_id . '.3' ] : '' ;
$contact_zone = ( $address_field_id ) ? $entry[ $address_field_id . '.4' ] : '' ;
$contact_postal_code = ( $address_field_id ) ? $entry[ $address_field_id . '.5' ] : '' ;
$contact_country = ( $address_field_id ) ? $entry[ $address_field_id . '.6' ] : '' ;
/**
* Build args to pass to SI_Invoice::create_invoice
*/
$invoice_args = array(
'subject' => 'FixUpFox Website Maintenance',
'status' => 'Pending',
'submission' => 'Pending',
'fields' => $entry,
'form' => $form,
'history_link' => sprintf( '<a href="%s">#%s</a>', add_query_arg( array( 'id' => $entry['form_id'], 'lid' => $entry['id'] ), admin_url( 'admin.php?page=gf_entries&view=entry' ) ), $entry['id'] ),
'is_recurring_invoice' => 1,
);
/**
* Creates the invoice from the arguments
*/
$invoice_id = SI_Invoice::create_invoice( $invoice_args );
$invoice = SI_Invoice::get_instance( $invoice_id );
/**
* DIFFICULT PART
* This is probably the part that may give you the most grief,
* everything above was pretty straightforward and you
* only needed to change some parameter ids to match your form field ids.
* This portion requires that you create the line items
* based on the submissions.
*
* Just remember this is an example for you to use.
* The original GF form this is based on is available upon request.
*/
// line items are an array.
$line_items = array();
// Check to see which options were selected for service
// $entry['16'] is service level
// $entry['17'] is renewal cycle
if ( $entry[16] === 'Professional' ) {
$rate = 199;
} elseif ( $entry[16] === 'Personal' ) {
$rate = 49;
}
// Calculate cycle with rate. Verifying rate as well to ensure no hidden fields interfere
if ( $entry[16] === 'Professional' && $entry[17] === 'annually' ) {
$quantity = 12;
$discount = 33;
$percent = 0.33;
$description = 'Professional service at an annual renewal discount';
} elseif ( $entry[16] === 'Professional' && $entry[17] === 'quarterly' ) {
$quantity = 3;
$discount = 20;
$percent = 0.20;
$description = 'Professional service at a quarterly renewal discount';
} elseif ( $entry[16] === 'Professional' && $entry[17] === 'monthly' ) {
$quantity = 1;
$discount = 0;
$percent = 0.00;
$description = 'Professional service at no monthly renewal discount';
} elseif ( $entry[16] === 'Personal' && $entry[17] === 'annually' ) {
$quantity = 12;
$discount = 33;
$percent = 0.33;
$description = 'Personal service at an annual renewal discount';
} elseif ( $entry[16] === 'Personal' && $entry[17] === 'quarterly' ) {
$quantity = 3;
$discount = 20;
$percent = 0.20;
$description = 'Personal service at a quarterly renewal discount';
} elseif ( $entry[16] === 'Personal' && $entry[17] === 'monthly' ) {
$quantity = 1;
$discount = 0;
$percent = 0.00;
$description = 'Personal service at no monthly renewal discount';
}
$total = $rate * $quantity * (1 - $percent);
// if ( is_numeric( $entry['choice_4_5_0'] ) ) {
$line_items[] = array(
'type' => 'service', // type of line item
'rate' => $rate, // rate per hour
'qty' => $quantity,
'tax' => $discount,
'total' => $total,
'desc' => $description,
);
// }
// /**
// * That's it for creating the line items array. Now it can
// * be set.
// */
$invoice->set_line_items( $line_items );
/**
* Attempt to create a user before creating a client.
*/
$user_id = get_current_user_id();
if ( ! $user_id ) {
if ( isset( $args['email'] ) && '' !== $args['email'] ) {
// check to see if the user exists by email
$user = get_user_by( 'email', $args['email'] );
if ( $user ) {
$user_id = $user->ID;
}
}
}
// Create a user for the submission if an email is provided.
if ( ! $user_id ) {
// email is critical
if ( isset( $args['email'] ) && '' !== $args['email'] ) {
$user_args = array(
'user_login' => esc_attr__( $email ),
'display_name' => isset( $client_name ) ? esc_attr__( $client_name ) : esc_attr__( $email ),
'user_email' => esc_attr__( $email ),
'first_name' => si_split_full_name( esc_attr__( $full_name ), 'first' ),
'last_name' => si_split_full_name( esc_attr__( $full_name ), 'last' ),
'user_url' => $website,
);
$user_id = SI_Clients::create_user( $user_args );
}
}
// Make up the args in creating a client
$args = array(
'company_name' => $client_name,
'website' => $website,
'phone' => $contact_phone,
'address' => array(
'street' => $contact_street,
'city' => $contact_city,
'zone' => $contact_zone,
'postal_code' => $contact_postal_code,
'country' => $contact_country,
),
'user_id' => $user_id,
);
$client_id = SI_Client::new_client( $args );
/**
* After a client is created assign it to the invoice
*/
$invoice->set_client_id( $client_id );
// Finally associate the invoice with the form submission
add_post_meta( $invoice_id, 'gf_form_id', $entry['id'] );
}
function maybe_redirect_to_invoice_after_submission( $confirmation, $form, $entry, $ajax ) {
// Find the invoice that have been associated with this entry.
$invoice_ids = SI_Post_Type::find_by_meta( SI_Invoice::POST_TYPE, array( 'gf_form_id' => $entry['id'] ) );
if ( ! empty( $invoice_ids ) ) {
$invoice_id = $invoice_ids[0];
}
// var_dump($invoice_ids);
// Modify the confirmation array and add the redirection to the new invoice
if ( get_post_type( $invoice_id ) === SI_Invoice::POST_TYPE ) {
$invoice_url = get_permalink( $invoice_id );
if ( headers_sent() || $ajax ) {
$confirmation = GFFormDisplay::get_js_redirect_confirmation( $invoice_url, $ajax );
} else {
$confirmation = array( 'redirect' => $invoice_url );
}
}
return $confirmation;
// echo '<pre>';
// var_dump($entry);
// echo '</pre>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment