Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gravity Wiz // Enable Decimal Values for Quantity Fields
<?php
/**
* Gravity Wiz // Gravity Forms // Accept Decimal Values for Quantity Fields
*
* Allows you to accept decimal values in Quantity fields, converting any Quantity field into a weight field.
*
* @version 1.2
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/enable-decimal-values-in-quantity-fields/
*/
class GW_Quantity_Decimal {
private static $_current_form;
function __construct( $form_id, $field_ids = array() ) {
if( ! is_array( $field_ids ) ) {
$field_ids = array( $field_ids );
}
$this->form_id = $form_id;
$this->field_ids = $field_ids;
add_action( 'init', array( $this, 'init' ) );
}
function init() {
// make sure Gravity Forms is loaded
if( ! class_exists( 'GFForms' ) ) {
return;
}
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 );
}
}
function allow_quantity_float( $result, $value, $form, $field ) {
if (
$this->is_enabled_field( $field ) &&
in_array( $field->type, array( 'product', 'quantity' ) ) &&
in_array( $field->validation_message, array( __( 'Please enter a valid quantity. Quantity cannot contain decimals.', 'gravityforms'), __( 'Please enter a valid quantity', 'gravityforms' ) ) ) )
{
$is_numeric = $field->type == 'product' ? GFCommon::is_numeric( rgpost( "input_{$field['id']}_3" ), 'decimal_dot' ) : GFCommon::is_numeric( rgpost( "input_{$field['id']}"), 'decimal_dot' );
if( $is_numeric ) {
$result['is_valid'] = true;
}
}
return $result;
}
function stash_current_form( $form ) {
self::$_current_form = $form;
return $form;
}
function modify_quantity_input_tag( $markup, $field, $value, $lead_id, $form_id ) {
$is_correct_form = $this->form_id == $form_id;
$is_correct_stashed_form = self::$_current_form && self::$_current_form['id'] == $form_id;
if( ! $is_correct_form || ! $is_correct_stashed_form || ! $this->is_enabled_field( $field ) ) {
return $markup;
}
$markup = $this->get_field_input( $field, $value, self::$_current_form );
$search = 'type=\'number\'';
$replace = $search . ' step=\'any\'';
$markup = str_replace( $search, $replace, $markup );
return $markup;
}
function get_field_input( $field, $value, $form ) {
remove_filter( 'gform_field_input', array( $this, 'modify_quantity_input_tag' ), 10, 5 );
$input = GFCommon::get_field_input( $field, $value, 0, $form['id'], $form );
add_filter( 'gform_field_input', array( $this, 'modify_quantity_input_tag' ), 10, 5 );
return $input;
}
function is_enabled_field( $field ) {
return is_array( $this->field_ids ) && ! empty( $this->field_ids ) ? in_array( $field['id'], $this->field_ids ) : true;
}
}
# accept quantity as decimal for any fields
new GW_Quantity_Decimal( 123 );
# accept quantity as decimal for a single field
// new GW_Quantity_Decimal( 123, 1 );
# accept quantity as decimal for a group of fields
// new GW_Quantity_Decimal( 123, array( 1, 2, 3 ) );
@certainlyakey

This comment has been minimized.

Copy link

certainlyakey commented Oct 4, 2017

This code has actually opened my eyes on how to add attributes to GF inputs correctly. Thanks!

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.