17站长网

17站长网 首页 编程 PHP编程 查看内容

在WooCommerce结账中添加带有收集时间的自定义选择字段

2023-1-19 16:57| 查看: 2744 |来源: 互联网

我已经在我的网站上为收款时间创建了一个自定义结账字段这是我当前的代码:add_action('woocommerce_before_order_notes','njengah_add_selec ...

我已经在我的网站上为收款时间创建了一个自定义结账字段

这是我当前的代码:

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

任何帮助都是最好的

使用根据指定类型检索当前时间的推荐答案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

  • 上一个值=下午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

  • 最后一个值=晚上8:00(停止时间)

本文最后更新于 2023-1-19 16:57,某些文章具有时效性,若有错误或已失效,请在网站留言或联系站长:17tui@17tui.com
·END·
站长网微信号:w17tui,关注站长、创业、关注互联网人 - 互联网创业者营销服务中心

免责声明:本站部分文章和图片均来自用户投稿和网络收集,旨在传播知识,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系我们及时修正或删除。谢谢!

17站长网微信二维码

始终以前瞻性的眼光聚焦站长、创业、互联网等领域,为您提供最新最全的互联网资讯,帮助站长转型升级,为互联网创业者提供更加优质的创业信息和品牌营销服务,与站长一起进步!让互联网创业者不再孤独!

扫一扫,关注站长网微信

大家都在看

    热门排行

      最近更新

        返回顶部