在 WooCommerce 结帐中添加带有收集时间的自定义 select 字段
Add a custom select field with collection times in WooCommerce checkout
我在我的网站上为取件时间创建了一个自定义结账字段(见附图)
这是我当前的代码:
add_action('woocommerce_before_order_notes', 'njengah_add_select_checkout_field');
function njengah_add_select_checkout_field( $checkout ) {
woocommerce_form_field( 'daypart', array(
'type' => 'select',
'class' => array( 'njengah-drop' ),
'label' => __( 'Collection Time' ),
'required' => true,
'options' => array(
'blank' => __( 'Select a collection time', 'njengah' ),
'5:00_PM' => __( '5:00 PM', 'njengah' ),
'5:30_PM' => __( '5:30 PM', 'njengah' ),
'6:00_PM' => __( '6:00 PM', 'njengah' ),
'6:30_PM' => __( '6:30 PM', 'njengah' ),
'7:00_PM' => __( '7:00 PM', 'njengah' ),
'7:30_PM' => __( '7:30 PM', 'njengah' ),
'8:00_PM' => __( '8:00 PM', 'njengah' )
)
), $checkout->get_value( 'daypart' ));
}
但是,目的是在时间过去后隐藏收集时间
- 如果下午 6 点隐藏:5:00 下午和 5:30 下午
任何帮助都会很棒
使用 WordPress current_time() 函数根据指定类型检索当前时间。
从那时起,您可以进一步自定义代码以满足您的需要,因此您将获得:
function action_woocommerce_before_order_notes( $checkout ) {
// Open and close time
$start_time = strtotime( '9:00 AM' );
$stop_time = strtotime( '1:00 PM' );
/* END SETTINGS */
// Current time
$current_time = current_time( 'timestamp' );
// Initialize
$remaining_times = array();
$required = true;
// Closed
if( $current_time > $stop_time || $current_time <= $start_time ) {
// Default value
$default[''] = __( 'Closed', 'woocommerce');
// False
$required = false;
} else {
// Default value
$default[''] = __( 'Select a collection time', 'woocommerce');
// Determine first value
$first_value = strtotime( date( 'g:i A', ceil( $current_time / 1800 ) * 1800 ) );
// Add a new option every 30 minutes
while( $first_value <= $stop_time && $first_value >= $start_time ) {
$value = date( 'g:i A', $first_value );
$remaining_times[$value] = $value;
// Add 30 minutes
$first_value = strtotime( '+30 minutes', $first_value );
}
}
// Options
$options = array_merge( $default, $remaining_times );
// Add field
woocommerce_form_field( 'daypart', array(
'type' => 'select',
'class' => array( 'njengah-drop' ),
'label' => __( 'Collection Time', 'woocommerce' ),
'required' => $required,
'options' => $options,
), $checkout->get_value( 'daypart' ));
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
例如
- 当前时间=9:14上午
- 第一个值 = 9:30 AM
- 最后一个值 = 1:00 下午(停止时间)
补充问题:假设开始时间是5:00下午,结束时间是8:00下午。我怎样才能让客户有机会从 12:00 下午开始订购,而第一个时段是 5:00 下午?
请改用以下代码:
function action_woocommerce_before_order_notes( $checkout ) {
// Display time, open and close time
$display_time = strtotime( '12:00 PM' );
$start_time = strtotime( '5:00 PM' );
$stop_time = strtotime( '8:00 PM' );
// END SETTINGS
// Current time
$current_time = current_time( 'timestamp' );
// Initialize
$remaining_times = array();
$required = true;
// Closed
if( $current_time > $stop_time || $current_time <= $display_time ) {
// Default value
$default[''] = __( 'Closed', 'woocommerce');
// False
$required = false;
} else {
// Default value
$default[''] = __( 'Select a collection time', 'woocommerce');
// Determine first value
$first_value = strtotime( date( 'g:i A', ceil( $current_time / 1800 ) * 1800 ) );
// First value is less than start time
if ( $first_value < $start_time ) {
$first_value = $start_time;
}
// Add a new option every 30 minutes
while( $first_value <= $stop_time && $first_value >= $start_time ) {
$value = date( 'g:i A', $first_value );
$remaining_times[$value] = $value;
// Add 30 minutes
$first_value = strtotime( '+30 minutes', $first_value );
}
}
// Options
$options = array_merge( $default, $remaining_times );
// Add field
woocommerce_form_field( 'daypart', array(
'type' => 'select',
'class' => array( 'njengah-drop' ),
'label' => __( 'Collection Time', 'woocommerce' ),
'required' => $required,
'options' => $options,
), $checkout->get_value( 'daypart' ));
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
例如
- 现在时间=12:05下午
- 第一个值 = 5:00 PM
- 最后一个值 = 8:00 下午(停止时间)
相关:
我在我的网站上为取件时间创建了一个自定义结账字段(见附图)
这是我当前的代码:
add_action('woocommerce_before_order_notes', 'njengah_add_select_checkout_field');
function njengah_add_select_checkout_field( $checkout ) {
woocommerce_form_field( 'daypart', array(
'type' => 'select',
'class' => array( 'njengah-drop' ),
'label' => __( 'Collection Time' ),
'required' => true,
'options' => array(
'blank' => __( 'Select a collection time', 'njengah' ),
'5:00_PM' => __( '5:00 PM', 'njengah' ),
'5:30_PM' => __( '5:30 PM', 'njengah' ),
'6:00_PM' => __( '6:00 PM', 'njengah' ),
'6:30_PM' => __( '6:30 PM', 'njengah' ),
'7:00_PM' => __( '7:00 PM', 'njengah' ),
'7:30_PM' => __( '7:30 PM', 'njengah' ),
'8:00_PM' => __( '8:00 PM', 'njengah' )
)
), $checkout->get_value( 'daypart' ));
}
但是,目的是在时间过去后隐藏收集时间
- 如果下午 6 点隐藏:5:00 下午和 5:30 下午
任何帮助都会很棒
使用 WordPress current_time() 函数根据指定类型检索当前时间。
从那时起,您可以进一步自定义代码以满足您的需要,因此您将获得:
function action_woocommerce_before_order_notes( $checkout ) {
// Open and close time
$start_time = strtotime( '9:00 AM' );
$stop_time = strtotime( '1:00 PM' );
/* END SETTINGS */
// Current time
$current_time = current_time( 'timestamp' );
// Initialize
$remaining_times = array();
$required = true;
// Closed
if( $current_time > $stop_time || $current_time <= $start_time ) {
// Default value
$default[''] = __( 'Closed', 'woocommerce');
// False
$required = false;
} else {
// Default value
$default[''] = __( 'Select a collection time', 'woocommerce');
// Determine first value
$first_value = strtotime( date( 'g:i A', ceil( $current_time / 1800 ) * 1800 ) );
// Add a new option every 30 minutes
while( $first_value <= $stop_time && $first_value >= $start_time ) {
$value = date( 'g:i A', $first_value );
$remaining_times[$value] = $value;
// Add 30 minutes
$first_value = strtotime( '+30 minutes', $first_value );
}
}
// Options
$options = array_merge( $default, $remaining_times );
// Add field
woocommerce_form_field( 'daypart', array(
'type' => 'select',
'class' => array( 'njengah-drop' ),
'label' => __( 'Collection Time', 'woocommerce' ),
'required' => $required,
'options' => $options,
), $checkout->get_value( 'daypart' ));
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
例如
- 当前时间=9:14上午
- 第一个值 = 9:30 AM
- 最后一个值 = 1:00 下午(停止时间)
补充问题:假设开始时间是5:00下午,结束时间是8:00下午。我怎样才能让客户有机会从 12:00 下午开始订购,而第一个时段是 5:00 下午?
请改用以下代码:
function action_woocommerce_before_order_notes( $checkout ) {
// Display time, open and close time
$display_time = strtotime( '12:00 PM' );
$start_time = strtotime( '5:00 PM' );
$stop_time = strtotime( '8:00 PM' );
// END SETTINGS
// Current time
$current_time = current_time( 'timestamp' );
// Initialize
$remaining_times = array();
$required = true;
// Closed
if( $current_time > $stop_time || $current_time <= $display_time ) {
// Default value
$default[''] = __( 'Closed', 'woocommerce');
// False
$required = false;
} else {
// Default value
$default[''] = __( 'Select a collection time', 'woocommerce');
// Determine first value
$first_value = strtotime( date( 'g:i A', ceil( $current_time / 1800 ) * 1800 ) );
// First value is less than start time
if ( $first_value < $start_time ) {
$first_value = $start_time;
}
// Add a new option every 30 minutes
while( $first_value <= $stop_time && $first_value >= $start_time ) {
$value = date( 'g:i A', $first_value );
$remaining_times[$value] = $value;
// Add 30 minutes
$first_value = strtotime( '+30 minutes', $first_value );
}
}
// Options
$options = array_merge( $default, $remaining_times );
// Add field
woocommerce_form_field( 'daypart', array(
'type' => 'select',
'class' => array( 'njengah-drop' ),
'label' => __( 'Collection Time', 'woocommerce' ),
'required' => $required,
'options' => $options,
), $checkout->get_value( 'daypart' ));
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
例如
- 现在时间=12:05下午
- 第一个值 = 5:00 PM
- 最后一个值 = 8:00 下午(停止时间)
相关: