Wordpress 在表单字段中设置动态选项数组
因此,我有以下代码:Wordpress 在表单字段中设置动态选项数组,wordpress,woocommerce,Wordpress,Woocommerce,因此,我有以下代码: /** Add custom fields to user / checkout - Date + Venue */ add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' ); function my_custom_checkout_field( $checkout ) { if( have_rows('date_venue', 424) ): $x = 1; wh
/**
Add custom fields to user / checkout - Date + Venue
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
if( have_rows('date_venue', 424) ): $x = 1;
while ( have_rows('date_venue', 424) ) : the_row(); ?>
<?php $dates[] = get_sub_field('date').' - '.get_sub_field('session_time'); ?>
<?php $x++; endwhile;
else : endif;
echo '<div id="bv_custom_checkout_field"><h4>Select Course Date/Venue</h4>';
woocommerce_form_field( 'course_venue', array(
'type' => 'select',
'class' => array('my-class form-row-wide'),
'label' => __('Select Course Date / Venue'),
'placeholder' => __('Course Date/Venue'),
'options' => array(
$dates[0] => __( $dates[0], 'wps' ),
$dates[1] => __( $dates[1], 'wps' ),
$dates[2] => __( $dates[2], 'wps' )
),
),
get_user_meta( get_current_user_id(),'course_venue' , true ) ); echo '</div>';
}
/**
将自定义字段添加到用户/签出-日期+地点
*/
添加操作(“订单注释后的woocommerce”、“我的自定义”签出字段”);
函数my_custom_checkout_字段($checkout){
如果(有行(“日期/地点”,424)):$x=1;
while(have_row('date_vention',424)):the_row();?>
多亏了斯迈尔斯:
您可以使用array\u flip
将所有键设置为$dates中的值,但如果希望值通过转换函数传递,则可以使用foreach构建数组
$options_dates = array();
foreach( (array) $dates as $date ){
$options_dates[ $date ] = __( $date, 'wps' );
}
然后只需设置'options'=>$options\u日期,
您的代码中存在一些缺陷和不一致之处。您能解释一下您想要实现的目标吗?看起来您希望客户选择一个日期/时间,但目前您正在通过获取用户\元(获取当前用户\ id())为该字段提供您正在收集的值,当然,正确)
。这可能不是您想要的。您不能将变量传递给这样的转换函数。有关转换的详细信息。我也不明白在这种情况下为什么需要array\u flip()
。