Hey folkens
Jeg har købt gravity forms til 2 sider jeg er ved at sætte op. Men jeg er løbet ind i et problem med at GF ikke kan regne med decimaler, så jeg bliver nød til at indsætte en kode i min Functions.php.. Jeg har prøvet et par gange men hvergang kan vil siden ikke åbne bagefter og her sidste gang lavede jeg kage i det og nu er der en grå bar over min header.. har brug for hjælp til at få den kode in i min functions.php og bagefter fjerne den grå bar der nu er dukket..
<?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.1
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/enable-decimal-values-in-quantity-fields/
* @copyright 2014 Gravity Wiz
*/
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_validation_' . $this->form_id, array( $this, 'allow_quantity_float' ) );
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( $validation_result ) {
$form = $validation_result['form'];
$has_validation_error = false;
foreach( $form['fields'] as &$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 )
$field['failed_validation'] = false;
}
if( $field['failed_validation'] )
$has_validation_error = true;
}
$validation_result['form'] = $form;
$validation_result['is_valid'] = ! $has_validation_error;
return $validation_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 a single field
new GW_Quantity_Decimal( 529, 1 );
# accept quantity as decimal for a group of fields
// new GW_Quantity_Decimal( 34, array( 1, 3 ) );
not a support question