I want to let users select products in a form.
To do that, I need to dynamically populate a form with published WooCommerce produts.
I found a solution to populate a select field with WooCommerce produts.
But the user should be able to select a quantity per product. Therefore I need to use a list field and populate the cells of the list field.
Here’s how the list field looks:
The SKU and the title fields should be populated dynamically.
And the quantity field is for the user to fill.
Here’s my code to populate a select field with WooCommerce product data:
add_filter( 'gform_pre_render_5', 'populate_posts' );
add_filter( 'gform_pre_validation_5', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_5', 'populate_posts' );
add_filter( 'gform_admin_pre_render_5', 'populate_posts' );
function populate_posts( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
$posts = get_posts( 'numberposts=-1&post_status=publish&post_type=product' );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a product';
$field->choices = $choices;
}
return $form;
}
Unfortunately I have no idea how to populate the cells of a list field dynamically.
I found an example how to populate a list field: https://docs.gravityforms.com/gform_field_value_parameter_name/#list-field
But I couldn’t figure out how to combine these two snippets :/
add_filter( 'gform_field_value_list', 'populate_list' );
function populate_list( $value ) {
$list_array = array(
array(
'Column 1' => 'row1col1',
'Column 2' => 'row1col2',
'Column 3' => 'row1col3',
),
array(
'Column 1' => 'row2col1',
'Column 2' => 'row2col2',
'Column 3' => 'row2col3'
),
);
return $list_array;
}
2
Answers
If anyone is interested, here's my working code.
With some big help from here: https://stackoverflow.com/a/61098114/1788961
Just modified your code a little to make first column as a select field and dynamically populate the product names to not have a long list being displayed.