重力形式 - 动态创建单个产品字段
Gravity forms – create single product field dynamically
我正在尝试通过自定义 post 类型为我的表单动态创建新字段(单个产品)。这在前端工作得很好。现在,一个问题是,一旦提交了表单,除了这些动态添加的字段之外的所有字段都在提交中。另一个问题是,总计字段(根据计算需要)也忽略了动态创建的。
我想这与挂钩有关。但是我尝试的每一个钩子都没有改变任何东西。
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;
}
我也已经尝试过这些钩子了:
add_filter( 'gform_pre_process_1', 'populate_beers' );
add_filter( 'gform_pre_render_1', 'populate_beers' );
这里是关于保存问题的日志输出以供澄清:
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} 和 {pricing_fields} 没有显示那些产品字段、总计和运输字段。
您忘记在属性中定义所需的 inputType
。
在 gravityforms 中定义动态产品的正确方法是将其声明为 type => product
,定义 inputType
,如 singleproduct
,不要忘记添加 [=如果您将 singleproduct
用作 inputType
.
,则将 17=] 数组添加到属性中
示例:
$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'
),
),
);
重力形式产品的可用输入类型为:
- 单一产品
- select
- 收音机
- 价格
- 隐藏产品
- 计算
也尝试像这样向您的表单添加动态总计计算:
$calculation = GF_Fields::create(array(
'id' => 1000,
'label' => 'Totals',
'type' => 'total',
));
array_push( $form['fields'], $calculation );
您的 Gravityforms Hook 应该可以工作:
add_filter( 'gform_form_post_get_meta_1', 'populate_beers' );
我正在尝试通过自定义 post 类型为我的表单动态创建新字段(单个产品)。这在前端工作得很好。现在,一个问题是,一旦提交了表单,除了这些动态添加的字段之外的所有字段都在提交中。另一个问题是,总计字段(根据计算需要)也忽略了动态创建的。
我想这与挂钩有关。但是我尝试的每一个钩子都没有改变任何东西。
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;
}
我也已经尝试过这些钩子了:
add_filter( 'gform_pre_process_1', 'populate_beers' );
add_filter( 'gform_pre_render_1', 'populate_beers' );
这里是关于保存问题的日志输出以供澄清:
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} 和 {pricing_fields} 没有显示那些产品字段、总计和运输字段。
您忘记在属性中定义所需的 inputType
。
在 gravityforms 中定义动态产品的正确方法是将其声明为 type => product
,定义 inputType
,如 singleproduct
,不要忘记添加 [=如果您将 singleproduct
用作 inputType
.
示例:
$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'
),
),
);
重力形式产品的可用输入类型为:
- 单一产品
- select
- 收音机
- 价格
- 隐藏产品
- 计算
也尝试像这样向您的表单添加动态总计计算:
$calculation = GF_Fields::create(array(
'id' => 1000,
'label' => 'Totals',
'type' => 'total',
));
array_push( $form['fields'], $calculation );
您的 Gravityforms Hook 应该可以工作:
add_filter( 'gform_form_post_get_meta_1', 'populate_beers' );