我有一个 ACF 中继器字段,可以创建具有一定数量可用位置的课程。这个复读器在每个单独的自定义帖子类型课程上。另一方面,我有一个 Gravityform,它有一个自动填充了这些 ACF 中继器的无线电场:
add_filter( 'gform_pre_render_3', 'populate_posts' );
add_filter( 'gform_pre_validation_3', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_3', 'populate_posts' );
add_filter( 'gform_admin_pre_render_3', 'populate_posts' );
function populate_posts( $form ) {
foreach ( $form['fields'] as &$field ) {
// Vient check tous les fields du formulaire
if ( $field->type != 'radio' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
// Choisit seulement les fields de type [radio] qui ont la class [populate-posts]
continue;
}
$date_type = get_field( 'date_type' );
$range_dates = get_field( 'date_range_repeater' );
$sinlge_dates = get_field( 'sinlge_dates_repeater' );
$choices = array();
if( $date_type === 'date_type_range' ){
foreach ( $range_dates as $range_date ) {
$dateformatstring = "j F Y";
$dateclean_start = strtotime( $range_date['date_starting'] );
$final_date_start = date_i18n($dateformatstring, $dateclean_start);
$dateclean_end = strtotime( $range_date['date_ending'] );
$final_date_end = date_i18n($dateformatstring, $dateclean_end);
$choices[] = array(
'text' => 'Du '.$final_date_start.' au '.$final_date_end.'<br />'.$range_date['availability'].' places disponibles',
'value' => 'Du '.$final_date_start.' au '.$final_date_end
);
}
}elseif( $date_type === 'date_type_single' ){
foreach ( $sinlge_dates as $sinlge_date ) {
$dateformatstring = "j F Y";
$dateclean_start = strtotime( $sinlge_date['date_starting'] );
$final_date_start = date_i18n($dateformatstring, $dateclean_start);
$dateclean_end = strtotime( $sinlge_date['date_ending'] );
$final_date_end = date_i18n($dateformatstring, $dateclean_end);
$choices[] = array(
'text' => 'Du '.$final_date_start.' au '.$final_date_end.'<br />'.$sinlge_date['availability'].' places disponibles',
'value' => 'Du '.$final_date_start.' au '.$final_date_end
);
}
}else{}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a Post';
$field->choices = $choices;
}
return $form;
}
我希望在每次提交后可用的位置数从 1 减少。我已经想出了如何使用这个 ACF 函数来管理它: https ://www.advancedcustomfields.com/resources/update_sub_field/ 这是我的代码:
add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {
//$radio_value = rgar( $entry, '11' );
$repeater = 'date_range_repeater';
$acf_repeater = get_field('date_range_repeater' ); // get all the rows
$row = 0;
$specific_row = $acf_repeater[$row];
$sub_field = 'availability';
$current_availability = $specific_row['availability'];
$availability_new = --$current_availability;
update_sub_field( array($repeater, ++$row, $sub_field), $availability_new );
//echo $radio_value;
}
问题是我必须手动选择必须使用哪一行来更新子字段。我如何才能检测到我提交的值在哪个 ACF 转发器行中?
谢谢 !