在 WooCommerce 结帐中添加带有时间间隔的 select 字段选项
Add select field options with time intervals in WooCommerce checkout
我使用 WordPress 和 WooCommerce 创建了一个餐厅送货服务网站。
基本上当客户是
结账时,he/she 可以选择他们想要送餐的时间。我希望 select 框包含 15 分钟的时间间隔
从当前时间到收盘时间。
餐厅在 11:30 到 22:00 期间开放供外卖。第一个选项我
想要的是“尽快”,然后下一个选项是一个小时后(四舍五入到最接近的 15 分钟),然后每次都是 15 分钟。
所以基本上是这样的:
(假设当前时间是13:55)
尽快
15:00
15:15
15:30
15:45
...以此类推直到关闭时间 (23:00)
这是我到目前为止尝试过的方法,但它只显示了整个时间范围。我应该怎么做才能禁用过去的时间?
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
woocommerce_form_field( 'delivery_time', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => ( '<span class="gew-lz">Desired Delivery Time</span>'),
'options' => array(
'sofort' => 'As soon as possible',
'12:00' => '12:00',
'12:30' => '12:30',
'13:00' => '13:00',
'13:30' => '13:30',
'14:00' => '14:00',
'17:30' => '17:30',
'18:00' => '18:00',
'18:30' => '18:30',
'19:00' => '19:00',
'19:30' => '19:30',
'20:00' => '20:00',
'20:30' => '20:30',
'21:00' => '21:00',
'21:30' => '21:30',
'22:00' => '22:00',
)
),
$checkout->get_value( 'delivery_time' ));
}
我想 disable/deactive 所有过去的时间。在前端结帐页面上显示时。
非常感谢您的帮助。
使用 WordPress current_time() 函数根据指定类型检索当前时间。
从那时起,您可以进一步自定义代码以满足您的需求,因此您将获得:
function action_woocommerce_before_order_notes( $checkout ) {
// Open and close time
$open_time = strtotime('11:30');
$close_time = strtotime('22:00');
// Current time
$current_time = current_time( 'timestamp' );
// Closed
if( $current_time > $close_time || $current_time <= $open_time ) {
// Default value
$options['closed'] = __( 'Closed', 'woocommerce');
} else {
// Default value
$options[''] = __( 'As soon as possible', 'woocommerce');
// As soon as possible
$asa_possible = strtotime( '+1 hour', $current_time );
// Round to next 15 minutes (15 * 60 seconds)
$asa_possible = ceil( $asa_possible / ( 15 * 60 ) ) * ( 15 * 60);
// Add a new option every 15 minutes
while( $asa_possible <= $close_time && $asa_possible >= $open_time ) {
$value = date( 'H:i', $asa_possible );
$options[$value] = $value;
// Add 15 minutes
$asa_possible = strtotime( '+15 minutes', $asa_possible );
}
}
// Add field
woocommerce_form_field( 'delivery_time', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => __('Desired delivery time', 'woocommerce' ),
'options' => $options,
), $checkout->get_value( 'delivery_time' ));
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
我使用 WordPress 和 WooCommerce 创建了一个餐厅送货服务网站。
基本上当客户是 结账时,he/she 可以选择他们想要送餐的时间。我希望 select 框包含 15 分钟的时间间隔 从当前时间到收盘时间。
餐厅在 11:30 到 22:00 期间开放供外卖。第一个选项我 想要的是“尽快”,然后下一个选项是一个小时后(四舍五入到最接近的 15 分钟),然后每次都是 15 分钟。 所以基本上是这样的:
(假设当前时间是13:55)
尽快 15:00 15:15 15:30 15:45 ...以此类推直到关闭时间 (23:00)
这是我到目前为止尝试过的方法,但它只显示了整个时间范围。我应该怎么做才能禁用过去的时间?
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
woocommerce_form_field( 'delivery_time', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => ( '<span class="gew-lz">Desired Delivery Time</span>'),
'options' => array(
'sofort' => 'As soon as possible',
'12:00' => '12:00',
'12:30' => '12:30',
'13:00' => '13:00',
'13:30' => '13:30',
'14:00' => '14:00',
'17:30' => '17:30',
'18:00' => '18:00',
'18:30' => '18:30',
'19:00' => '19:00',
'19:30' => '19:30',
'20:00' => '20:00',
'20:30' => '20:30',
'21:00' => '21:00',
'21:30' => '21:30',
'22:00' => '22:00',
)
),
$checkout->get_value( 'delivery_time' ));
}
我想 disable/deactive 所有过去的时间。在前端结帐页面上显示时。
非常感谢您的帮助。
使用 WordPress current_time() 函数根据指定类型检索当前时间。
从那时起,您可以进一步自定义代码以满足您的需求,因此您将获得:
function action_woocommerce_before_order_notes( $checkout ) {
// Open and close time
$open_time = strtotime('11:30');
$close_time = strtotime('22:00');
// Current time
$current_time = current_time( 'timestamp' );
// Closed
if( $current_time > $close_time || $current_time <= $open_time ) {
// Default value
$options['closed'] = __( 'Closed', 'woocommerce');
} else {
// Default value
$options[''] = __( 'As soon as possible', 'woocommerce');
// As soon as possible
$asa_possible = strtotime( '+1 hour', $current_time );
// Round to next 15 minutes (15 * 60 seconds)
$asa_possible = ceil( $asa_possible / ( 15 * 60 ) ) * ( 15 * 60);
// Add a new option every 15 minutes
while( $asa_possible <= $close_time && $asa_possible >= $open_time ) {
$value = date( 'H:i', $asa_possible );
$options[$value] = $value;
// Add 15 minutes
$asa_possible = strtotime( '+15 minutes', $asa_possible );
}
}
// Add field
woocommerce_form_field( 'delivery_time', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => __('Desired delivery time', 'woocommerce' ),
'options' => $options,
), $checkout->get_value( 'delivery_time' ));
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );