Skip to content

Instantly share code, notes, and snippets.

@kenmasters
Last active August 30, 2017 13:00
Show Gist options
  • Save kenmasters/c3423455f86b4852e96a10277f0c454a to your computer and use it in GitHub Desktop.
Save kenmasters/c3423455f86b4852e96a10277f0c454a to your computer and use it in GitHub Desktop.
Creating unique order id.
Here are the detailed steps on how to add/create a unique order id for specific form.
An order id can be of any format, for demonstration purposes our format would be,
XXX-AAAYYMMDDHHMMSS - change XXX and AAA to your own preferences.
Step 1: Create new form.
Step 2: Add single field
Label: ORDER ID
Goto Advance Tab -> Visibility -> Administrative
Step 3: Add single field (This will be the $agent value)
Label: Agent
Step 4: Save the form, Your Done!
Please see also the screenshot in comment.
How it works?
# OrderID field will be populated dynamically before the form is saved to the database
Note: In the code above filter is executed for all submitted forms, but can also be specified
by adding the specific form id on the filter.
Example:
// Generate OrderID for FormID 12 only
add_action('gform_pre_submission_12', 'your_custom_callback');
function your_custom_callback( $form ) {
$_POST['input_10'] = 'I love Gravity Form';
}
Reference: https://www.gravityhelp.com/documentation/article/gform_pre_submission/
// Code Placement: current themes functions.php
/**
* Generate order id for each form [formid, orderfieldID, formname ]
* [21 383 BLUE] | [10 279 BS] | [22 430 T4] | [23 337 T1]
*/
add_action('gform_pre_submission', 'generate_order_id');
function generate_order_id ( $form ) {
$form_id = $form['id'];
$agent = strtoupper( substr( rgpost('input_1'), 0, 3) ); // return first 3 characters of the given input
$date_time = date('ymdHis', current_time('timestamp')); // returns YYMMDDHHMMSS format (eg.: 170901230000)
if ( $form_id == 21 ) {
$_POST['input_383'] = 'BLUE-' . $agent . $date_time;
} elseif ( $form_id == 10 ) {
$_POST['input_279'] = 'BS-' . $agent . $date_time;
} elseif ( $form_id == 22 ) {
$_POST['input_430'] = 'T4-' . $agent . $date_time;
} elseif ( $form_id == 23 ) {
$_POST['input_383'] = 'T1-' . $agent . $date_time;
} else {
# do nothing
}
}
@kenmasters
Copy link
Author

SETTING UP THE GRAVITY FORM:

Screenshot

generate order id setup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment