I'm trying to create new fields (single products) for my form dynamically from a custom post type. That works quite well in the frontend. Now, one issue with this is that, once form is submitted, all the fields EXCEPT these dynamically added ones are in the submission. The other issue is, that also the total field (as for calculation needs) ignores the dynamically created ones.
I guess it has to do with the hooks. But every hook I tried, doesn't change a thing.
add_filter( 'gform_form_post_get_meta_1', 'populate_beers');
function populate_beers($form) {
$new_field_id = 0;
foreach( $form['fields'] as $field ) {
if( $field->id > $new_field_id ) {
$new_field_id = $field->id;
}
}
$new_field_id++;
$beers = get_posts( 'numberposts=-1&post_status=publish&post_type=bier' );
//var_dump($beers);
foreach ( $beers as $beer ) {
// Prepare field properties
$props = array(
'id' => $new_field_id,
'type' => 'singleproduct',
'label' => $beer->post_title,
'basePrice' => floatval( $beer->preis ),
'enableCalculation' => true
);
//var_dump($props);
// Create new gravity forms field and add it to the form object
$nf = GF_Fields::create( $props );
// Hack - insert into array at specific position
// Needed to display product fields before other fields
// in the form
array_splice( $form['fields'], 5, 0, array($nf) );
$new_field_id++;
}
return $form;
}
I've also tried these hooks already:
add_filter( 'gform_pre_process_1', 'populate_beers' );
add_filter( 'gform_pre_render_1', 'populate_beers' );
Here's the output of the logging regarding the saving problem for clarification:
2021-01-26 8:40:48.944197 - DEBUG --> GFFormDisplay::process_form(): Starting to process form (#1) submission.
2021-01-26 8:40:48.944871 - DEBUG --> GFFormDisplay::process_form(): Source page number: 1. Target page number: 0.
2021-01-26 8:40:48.945425 - DEBUG --> GFFormDisplay::process_form(): After validation. Is submission valid? Yes.
2021-01-26 8:40:48.945449 - DEBUG --> GFFormDisplay::process_form(): Submission is valid. Moving forward.
2021-01-26 8:40:48.945477 - DEBUG --> GFFormsModel::save_entry(): Saving entry.
2021-01-26 8:40:48.948657 - DEBUG --> GFFormsModel::save_entry(): Entry record created in the database. ID: 16.
2021-01-26 8:40:48.948826 - DEBUG --> GFFormsModel::save_entry(): Saving entry fields.
2021-01-26 8:40:48.948880 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Name(#1.2 - name).
2021-01-26 8:40:48.948913 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Name(#1.3 - name).
2021-01-26 8:40:48.948936 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Name(#1.4 - name).
2021-01-26 8:40:48.948961 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Name(#1.6 - name).
2021-01-26 8:40:48.948983 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Name(#1.8 - name).
2021-01-26 8:40:48.949018 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Anschrift(#2.1 - address).
2021-01-26 8:40:48.949042 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Anschrift(#2.2 - address).
2021-01-26 8:40:48.949067 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Anschrift(#2.3 - address).
2021-01-26 8:40:48.949090 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Anschrift(#2.4 - address).
2021-01-26 8:40:48.949114 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Anschrift(#2.5 - address).
2021-01-26 8:40:48.949136 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Anschrift(#2.6 - address).
2021-01-26 8:40:48.949172 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Telefon(#3 - phone).
2021-01-26 8:40:48.949207 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: E-Mail(#13 - email).
2021-01-26 8:40:48.949239 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Collaboration Pale Ale(#18 - product).
2021-01-26 8:40:48.949271 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: What is Love?(#17 - product).
2021-01-26 8:40:48.949301 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Exploring New Galaxies(#16 - product).
2021-01-26 8:40:48.949331 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Devil in Disguise(#15 - product).
2021-01-26 8:40:48.949362 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Gooseberry Gose(#14 - product).
2021-01-26 8:40:48.949409 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Versand(#7 - shipping).
2021-01-26 8:40:48.953788 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Summe(#5 - total).
2021-01-26 8:40:48.957503 - DEBUG --> GFFormsModel::save_entry(): Finished saving entry fields.
{all_fields} and {pricing_fields} didn't show those product fields, the totals and the shipping field.
You forgot to define the required inputType
in your properties.
The correct way to define a dynamic product in gravityforms is to declare it as a type => product
, define an inputType
like singleproduct
and don´t forget to add the inputs
array to the properties if you are using singleproduct
as inputType
.
Example:
$props = array(
'id' => $new_field_id,
'type' => 'product',
'inputType' => 'singleproduct',
'label' => $beer->post_title,
'basePrice' => floatval( $beer->preis ),
'enableCalculation' => true,
'inputs' => array(
array(
'id' => $new_field_id.'.1',
'label' => $beer->post_title,
'name' => 'param_product'
),
array(
'id' => $new_field_id.'.2',
'label' => 'Price',
'name' => 'param_price'
),
array(
'id' => $new_field_id.'.3',
'label' => 'Quantity',
'name' => 'param_qty'
),
),
);
Available inputTypes for a gravityforms product are:
Also try to add a dynamic totals calculation to your form like this:
$calculation = GF_Fields::create(array(
'id' => 1000,
'label' => 'Totals',
'type' => 'total',
));
array_push( $form['fields'], $calculation );
Your Gravityforms Hook should work:
add_filter( 'gform_form_post_get_meta_1', 'populate_beers' );