Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jan 22, 2015

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

This comment has been minimized.

Copy link
Owner Author

commented Jan 23, 2015

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

This comment has been minimized.

Copy link

commented Jan 26, 2015

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

@uprise10

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.