Skip to content

Commit

Permalink
gpi-capture-resource-name.php: Added support for activating via dyn…
Browse files Browse the repository at this point in the history
…amic population parameter and populating resource on render.
  • Loading branch information
spivurno authored Nov 7, 2023
1 parent 1372794 commit a4fe7a3
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions gp-inventory/gpi-capture-resource-name.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,52 @@
* Gravity Perks // Inventory // Capture Resource Name (as Field Value)
* https://gravitywiz.com/documentation/gravity-forms-inventory/
*
* Instruction Video: https://www.loom.com/share/064577f9491a487d84e6bb594d3fd578
*
* If you intend to map different Resources to different fields throughout the life of your form, you may wish to capture
* the current Resource at the time of submission and save that value to a field. This snippet can help.
*

Check failure on line 8 in gp-inventory/gpi-capture-resource-name.php

View workflow job for this annotation

GitHub Actions / PHPCS

Whitespace found at end of line
* Instructions
*
* 1. Install the snippet.
* https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
*
* 2. Enable "Allow field to be populated dynamically" option under the "Advanced" field settings for the field
* in which you would like to capture the Resource name.
*
* 3. Set the parameter name to `gpi_capture_resource_1` replacing the "1" with the GPI-enabled field ID for which
* you would like to capture the current resource.
*/
// Update "123" to your form ID.
add_action( 'gform_after_submission_123', function( $entry, $form ) {
add_action( 'gform_field_value', function( $value, $field, $name ) {

if ( strpos( $name, 'gpi_capture_resource' ) !== false ) {
$resource = gpi_get_resource_by_parameter( $name, $field->formId );
$value = $resource->post_title;
}

// Update "4" to your Product field ID.
$product_field_id = 4;
return $value;
}, 10, 3 );

// Update "5" to a Hidden field ID that will capture the resource name.
$resource_field_id = 5;
add_action( 'gform_after_submission', function( $entry, $form ) {

$product_field = GFAPI::get_field( $form, $product_field_id );
$resource = get_post( $product_field->gpiResource );
foreach ( $form['fields'] as &$field ) {
if ( strpos( $field->inputName, 'gpi_capture_resource' ) === false ) {
continue;
}

GFAPI::update_entry_field( $entry['id'], $resource_field_id, $resource->post_title );
$resource = gpi_get_resource_by_parameter( $field->inputName, $field->formId );

GFAPI::update_entry_field( $entry['id'], $field->id, $resource->post_title );
}

}, 10, 2 );

if ( ! function_exists( 'gpi_get_resource_by_parameter' ) ) {
function gpi_get_resource_by_parameter( $parameter, $form_id ) {

$bits = explode( '_', $parameter );
$product_field_id = array_pop( $bits );

$product_field = GFAPI::get_field( GFAPI::get_form( $form_id ), $product_field_id );

return get_post( $product_field->gpiResource );
}
}

0 comments on commit a4fe7a3

Please sign in to comment.