Skip to content

Instantly share code, notes, and snippets.

@richardW8k
Last active November 10, 2017 03:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardW8k/9bb37e4f955c4d8c68e0 to your computer and use it in GitHub Desktop.
Save richardW8k/9bb37e4f955c4d8c68e0 to your computer and use it in GitHub Desktop.
Enables use of the total field with conditional logic. [hide] this field if [total][less than][0.1] or [show] this field if [total][greater than][0]. Because the total field is always the last field to be saved you can't use it when configuring conditional logic on other fields. Displaying other fields based on the total would prevent those fiel…
<?php
class RW_GF_Total_Field_Logic {
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
function init() {
if ( ! property_exists( 'GFForms', 'version' ) || ! version_compare( GFForms::$version, '1.9', '>=' ) ) {
return;
}
add_filter( 'gform_field_content', array( $this, 'maybe_add_logic_event' ), 10, 2 );
add_filter( 'gform_admin_pre_render', array( $this, 'enable_total_in_conditional_logic' ) );
}
function maybe_add_logic_event( $content, $field ) {
if ( $field->type != 'total' ) {
return $content;
}
$logic_event = $field->get_conditional_logic_event( 'change' );
return ! empty( $logic_event ) ? str_replace( "gform_hidden'", "gform_hidden' value='0' {$logic_event}", $content ) : $content;
}
function enable_total_in_conditional_logic( $form ) {
if ( GFCommon::is_entry_detail() ) {
return $form;
}
?>
<script type='text/javascript'>
gform.addFilter('gform_is_conditional_logic_field', function (isConditionalLogicField, field) {
return field.type == 'total' ? true : isConditionalLogicField;
});
gform.addFilter('gform_conditional_logic_operators', function (operators, objectType, fieldId) {
targetField = GetFieldById(fieldId);
if (targetField && targetField['type'] == 'total') {
operators = {'>': 'greaterThan', '<': 'lessThan'};
}
return operators;
});
</script>
<?php
return $form;
}
}
new RW_GF_Total_Field_Logic();
@mikebrenner
Copy link

I'm noticing that this alters the Pricing » Option field type and removes the ability to have checkboxes. Specifically, it replaces an Option's default field types (Drop Down, Checkboxes, Radio Buttons) to new ones (Single Product, Drop Down, Radio Buttons, User Defined Price, Hidden, Calculation).

Any idea how to circumvent this but still use this function to show the total field?

@richardW8k
Copy link
Author

Are you running the latest version of the snippet, the function for the gform_conditional_logic_operators hook was updated to resolve this issue in December.

@mikebrenner
Copy link

Thanks for the nudge to update, Richard. That worked.

@uprise10
Copy link

uprise10 commented May 4, 2015

It appears that when using this solution and using the "Form total greater than" conditional logic the payment details are not save with the lead entry.

Screenshot of entry without any conditional logic, or even conditional logic, but not the "Form total" logic: http://upload.uprise.nl/XRIb
Screenshot of entry with "Form total greater than 0": http://upload.uprise.nl/17e9f

And it isn't just that it doesn't display the payment details, but they're not even stored in the database (http://upload.uprise.nl/417a).

When doing some digging it appears that the "validation" method (for "gform_validation" filter), called on line 60 in includes/addon/class-gf-payment-addon.php, isn't executed. Why, I really can't figure out, but it seems something's going wrong there :)

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