Skip to content

gw-quantity-as-decimal.php: Fixed an issue with Calculation Product field not editable with decimal values. #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions gravity-forms/gw-quantity-as-decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ function init() {
add_filter( 'gform_field_validation_' . $this->form_id, array( $this, 'allow_quantity_float' ), 10, 4 );
}

if ( GFFormsModel::is_html5_enabled() ) {
add_filter( 'gform_pre_render', array( $this, 'stash_current_form' ) );
add_filter( 'gform_field_input', array( $this, 'modify_quantity_input_tag' ), 10, 5 );
// For GF versions before 2.8 and HTML5 disabled, ignore the rest.
if ( version_compare( GFCommon::$version, '2.8', '<' ) && ! GFFormsModel::is_html5_enabled() ) {
return;
}

// For GF versions 2.8 and beyond, HTML5 is enabled by default.
// Also for GF versions prior to 2.8 having HTML5 manually enabled.
add_filter( 'gform_pre_render', array( $this, 'stash_current_form' ) );
add_filter( 'gform_field_input', array( $this, 'modify_quantity_input_tag' ), 10, 5 );

add_filter( 'gform_field_content', array( $this, 'fix_content' ), 10, 5 );
}

function allow_quantity_float( $result, $value, $form, $field ) {
Expand Down Expand Up @@ -87,6 +93,30 @@ function modify_quantity_input_tag( $markup, $field, $value, $lead_id, $form_id
return $markup;
}

function fix_content( $content, $field, $value, $lead_id, $form_id ) {
if ( ! $this->is_enabled_field( $field ) ) {
return $content;
}

// ensure the step is 'any' for any fields that have a decimal value.
return preg_replace_callback(
'/<input([^>]*class=[\'"]ginput_quantity[\'"][^>]*)>/i',
function ( $matches ) {
$inputTag = $matches[0];

// Check if the input has a decimal value, and if does not have 'any'.
if ( preg_match('/\bvalue=[\'"]([\d]+\.[\d]+)[\'"]/i', $inputTag, $valueMatch ) ) {
if ( ! preg_match('/\bstep=[\'"]any[\'"]/i', $inputTag ) ) {
$inputTag = preg_replace( '/<input/i', '<input step="any"', $inputTag, 1 );
}
}

return $inputTag;
},
$content
);
}

function get_field_input( $field, $value, $form ) {

remove_filter( 'gform_field_input', array( $this, 'modify_quantity_input_tag' ), 10, 5 );
Expand Down
Loading