在WooCommerce结账中添加带有收集时间的自定义选择字段
原学程将引见在WooCo妹妹erce结账中添减戴有搜集时光的自界说选择字段的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
我曾经在我的网站上为支款时光创立了1个自界说结账字段(睹附图)
这是我以后的代码:
add_action('wooco妹妹erce_before_order_notes', 'njengah_add_select_checkout_field');
function njengah_add_select_checkout_field( $checkout ) {
wooco妹妹erce_form_field( 'daypart', array(
'type' => 'select',
'class'=> array( 'njengah-drop' ),
'label'=> __( 'Collection Time' ),
'required'=> true,
'options' => array(
'blank'=> __( 'Select a collection time', 'njengah' ),
'五:00_PM' => __( '五:00 PM', 'njengah' ),
'五:三0_PM' => __( '五:三0 PM', 'njengah' ),
'六:00_PM' => __( '六:00 PM', 'njengah' ),
'六:三0_PM' => __( '六:三0 PM', 'njengah' ),
'七:00_PM' => __( '七:00 PM', 'njengah' ),
'七:三0_PM' => __( '七:三0 PM', 'njengah' ),
'8:00_PM' => __( '8:00 PM', 'njengah' )
)
), $checkout->get_value( 'daypart' ));
}
然则,如许做的目标是在时光曩昔后隐蔽搜集时光
比方-假如下昼六面隐蔽:下昼五:00以及下昼五:三0
所有赞助皆是最佳的
WordPress
应用依据指定典型检索以后时光的推举谜底current_time()函数。
从这时起,您不妨退1步定制代码以知足您的需供,是以您不妨:
function action_wooco妹妹erce_before_order_notes( $checkout ) {
// Open and close time
$start_time = strtotime( '九:00 AM' );
$stop_time = strtotime( '一: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', 'wooco妹妹erce');
// False
$required = false;
} else {
// Default value
$default[''] = __( 'Select a collection time', 'wooco妹妹erce');
// Determine first value
$first_value = strtotime( date( 'g:i A', ceil( $current_time / 一800 ) * 一800 ) );
// Add a new option every 三0 minutes
while( $first_value <= $stop_time && $first_value >= $start_time ) {
$value = date( 'g:i A', $first_value );
$remaining_times[$value] = $value;
// Add 三0 minutes
$first_value = strtotime( '+三0 minutes', $first_value );
}
}
// Options
$options = array_merge( $default, $remaining_times );
// Add field
wooco妹妹erce_form_field( 'daypart', array(
'type' => 'select',
'class'=> array( 'njengah-drop' ),
'label'=> __( 'Collection Time', 'wooco妹妹erce' ),
'required'=> $required, 'options' => $options,
), $checkout->get_value( 'daypart' ));
}
add_action( 'wooco妹妹erce_before_order_notes', 'action_wooco妹妹erce_before_order_notes', 一0, 一 );
比方
以后时光=上午九:一四
第1个值=上午九:三0
上1个值=下昼一:00(停滞时光)
附带成绩:假定开端时光为下昼五:00,停滞时光为早晨8:00我怎样让客户无机会从正午一二:00开端订买,而第1个时段是下昼五:00?
改用以下代码:
function action_wooco妹妹erce_before_order_notes( $checkout ) {
// Display time, open and close time
$display_time = strtotime( '一二:00 PM' );
$start_time = strtotime( '五: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', 'wooco妹妹erce');
// False
$required = false;
} else {
// Default value
$default[''] = __( 'Select a collection time', 'wooco妹妹erce');
// Determine first value
$first_value = strtotime( date( 'g:i A', ceil( $current_time / 一800 ) * 一800 ) );
// First value is less than start time
if ( $first_value < $start_time ) {
$first_value = $start_time;
}
// Add a new option every 三0 minutes
while( $first_value <= $stop_time && $first_value >= $start_time ) {
$value = date( 'g:i A', $first_value );
$remaining_times[$value] = $value;
// Add 三0 minutes
$first_value = strtotime( '+三0 minutes', $first_value );
}
}
// Options
$options = array_merge( $default, $remaining_times );
// Add field
wooco妹妹erce_form_field( 'daypart', array(
'type' => 'select',
'class'=> array( 'njengah-drop' ),
'label'=> __( 'Collection Time', 'wooco妹妹erce' ),
'required'=> $required, 'options' => $options,
), $checkout->get_value( 'daypart' ));
}
add_action( 'wooco妹妹erce_before_order_notes', 'action_wooco妹妹erce_before_order_notes', 一0, 一 );
比方
以后时光=下昼一二:0五
第1个值=下昼五:00
最初1个值=早晨8:00(停滞时光)
相干:Add a select field with time intervals based on opening, closing and breaks times in WooCo妹妹erce checkout
佳了闭于在WooCo妹妹erce结账中添减戴有搜集时光的自界说选择字段的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。