admin管理员组文章数量:1323723
So I have the following code:
/**
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>';
}
As you can see I have added $dates[] as an array which could range from 2-X options, which will depend on the product ID.
As an example, I have included the options manually, i.e. dates[0]
, dates[1]
etc...
How would I go about looping this and including it within the options
array?
It works fine as it is, but it's not dynamic.
Any help is much appreciated!
So I have the following code:
/**
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>';
}
As you can see I have added $dates[] as an array which could range from 2-X options, which will depend on the product ID.
As an example, I have included the options manually, i.e. dates[0]
, dates[1]
etc...
How would I go about looping this and including it within the options
array?
It works fine as it is, but it's not dynamic.
Any help is much appreciated!
Share Improve this question asked Sep 14, 2020 at 16:00 nsilvansilva 2274 silver badges20 bronze badges1 Answer
Reset to default 1You can use array_flip
to set all the keys to the values from $dates, but if you want the value to be passed through translation function you can just build the array using foreach
$options_dates = array();
foreach( (array) $dates as $date ){
$options_dates[ $date ] = __( $date, 'wps' );
}
Then just set 'options' => $options_dates,
本文标签: woocommerce offtopicSet dynamic Options array in woocommerceformfield
版权声明:本文标题:woocommerce offtopic - Set dynamic Options array in woocommerce_form_field 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742120327a2421664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论