精品免费在线观看-精品欧美-精品欧美成人bd高清在线观看-精品欧美高清不卡在线-精品欧美日韩一区二区

17站長網

17站長網 首頁 編程 PHP編程 查看內容

在WooCommerce結賬中添加帶有收集時間的自定義選擇字段

2023-1-19 16:57| 查看: 6846 |來源: 互聯網

我已經在我的網站上為收款時間創建了一個自定義結賬字段這是我當前的代碼: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,某些文章具有時效性,若有錯誤或已失效,請在網站留言或聯系站長:[email protected]
·END·
站長網微信號:w17tui,關注站長、創業、關注互聯網人 - 互聯網創業者營銷服務中心

免責聲明:本站部分文章和圖片均來自用戶投稿和網絡收集,旨在傳播知識,文章和圖片版權歸原作者及原出處所有,僅供學習與參考,請勿用于商業用途,如果損害了您的權利,請聯系我們及時修正或刪除。謝謝!

17站長網微信二維碼

始終以前瞻性的眼光聚焦站長、創業、互聯網等領域,為您提供最新最全的互聯網資訊,幫助站長轉型升級,為互聯網創業者提供更加優質的創業信息和品牌營銷服務,與站長一起進步!讓互聯網創業者不再孤獨!

掃一掃,關注站長網微信

大家都在看

    熱門排行

      最近更新

        返回頂部
        主站蜘蛛池模板: 国产成人一区二区三区视频免费蜜 | 精品视频在线免费 | 无码一区二区三区视频 | 国产美女网址 | 国产亚洲综合视频 | 国产特级| 欧美人成在线观看网站高清 | 国产成人精品日本亚洲麻豆 | 国产黄色免费网站 | 免费人成又黄又爽的视频强 | 国产成人亚洲综合 | 蝌蚪视频91 | 欧美日韩中文在线视频 | 国产在线视精品麻豆 | 日韩美女一级毛片a | 免费在线观看成人 | 日本黄色大片免费观看 | 高清一区二区亚洲欧美日韩 | 中文字幕欧美成人免费 | 丁香六月婷婷激情 | 亚洲色啦啦狠狠网站 | 91精品天美精东蜜桃传媒免费 | 免费看一级淫片成人 | 日本精品在线观看视频 | 国产高清毛片 | 欧洲美女a视频一级毛片 | 国产色片在线观看 | 深夜爽爽福利gif在线观看 | 成年在线观看视频免费看 | 日韩精品免费 | 欧美精品v日韩精品v国产精品 | 黄色福利在线观看 | 成年美女黄的视频网站 | 亚洲国产婷婷综合在线精品 | 国产超级乱淫片中文 | 最刺激黄a大片老师 | 日韩精品你懂的在线播放 | 国产精品久久久久久免费 | 国产a v高清一区二区三区 | 国产欧美日韩一区二区三区在线 | 女性一级全黄生活片在线播放 |