【问题标题】:Add select field options with time intervals in WooCommerce checkout在 WooCommerce 结帐中添加具有时间间隔的选择字段选项
【发布时间】:2020-11-18 14:24:54
【问题描述】:

我使用 WordPress 和 WooCommerce 创建了一个餐厅送货服务网站。

基本上当客户 结账时,他/她可以选择他们想要送餐的时间。我希望选择框包含 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' ));

}

我想在过去几个小时内禁用/停用所有内容。在前端结帐页面上显示时。

我们将非常感谢您的帮助。

【问题讨论】:

    标签: php wordpress woocommerce checkout


    【解决方案1】:

    使用 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 );
    

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 2021-08-25
      • 2021-07-21
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      相关资源
      最近更新 更多