WooCommerce结帐选择字段:在计算的选项数组之前插入键/值

时间:2017-09-26 16:34:58

标签: php wordpress woocommerce

我正在使用我的WordPress / Woocommerce网站。 如何添加常量值,该值始终是下拉列表中的第一个值,包括时间(小时:分钟)。顶部的这个恒定值应该尽快说明"。

以下是生成表单的代码:

public function time_select( $checkout ) { 
    echo '<div id="local-pickup-time-select"><h2>' . __( 'Delivery Time', $this->plugin_slug ) . '</h2>';

    woocommerce_form_field( 'local_pickup_time_select', array(
        'type'          => 'select',
        'class'         => array( 'local-pickup-time-select-field form-row-wide' ),
        'label'         => __( 'Delivery Time', $this->plugin_slug ),
        'required'      => true,
        'options'       => self::create_hour_options()
    ), $checkout->get_value( 'local_pickup_time_select' ));

    self::create_hour_options();

    echo '</div>';
}

public function pickup_time_select_translatable( $value ) {
    $value = preg_replace('/(\d)_(\d)/','$1:$2', $value);
    $value = explode('_', $value);
    $return = __( $value[0], $this->plugin_slug ). ' ' .$value[1];
    return $return;
}

public function create_hour_options() {
    // Make sure we have a time zone set
    $offset = get_option( 'gmt_offset' );
    $timezone_setting = get_option( 'timezone_string' );

    if ( $timezone_setting ) {
        date_default_timezone_set( get_option( 'timezone_string', 'America/New_York' ) );
    }
    else {
        $timezone = timezone_name_from_abbr( null, $offset * 3600, true );
        if( $timezone === false ) $timezone = timezone_name_from_abbr( null, $offset * 3600, false );
        date_default_timezone_set( $timezone );
    }

    // Get days closed textarea from settings, explode into an array
    $closing_days_raw = trim( get_option( 'local_pickup_hours_closings' ) );
    $closing_days = explode( "\n", $closing_days_raw );
    $closing_days = array_filter( $closing_days, 'trim' );

    // Get delay, interval, and number of days ahead settings
    $delay_minutes = get_option( 'local_pickup_delay_minutes', 60 );
    $interval = get_option( 'local_pickup_hours_interval', 30 );
    $num_days_allowed = get_option( 'local_pickup_days_ahead', 1 );

    // Setup time variables for calculations
    $today_name = strtolower( date( 'l' ) );
    $today_date = date( 'm/d/Y' );

    // Create an empty array for our dates
    $pickup_options = array();

    //Translateble days
    __( 'Monday', $this->plugin_slug );
    __( 'Tuesday', $this->plugin_slug );
    __( 'Wednesday', $this->plugin_slug );
    __( 'Thursday', $this->plugin_slug );
    __( 'Friday', $this->plugin_slug );
    __( 'Saturday', $this->plugin_slug );
    __( 'Sunday', $this->plugin_slug );

    // Add empty option
    $pickup_options[''] = __( 'Select time', $this->plugin_slug );

    // Loop through all days ahead and add the pickup time options to the array
    for ( $i = 0; $i < $num_days_allowed; $i++ ) {

        // Get the date of current iteration
        $current_day_name = date( 'l', strtotime( "+$i days" ) );
        $current_day_name_lower = strtolower( $current_day_name );

        // Get the day's opening and closing times
        $open_time = get_option( 'local_pickup_hours_' . $current_day_name_lower . '_start', '10:00' );
        $close_time = get_option( 'local_pickup_hours_' . $current_day_name_lower . '_end', '19:00' );

        // Today
        $tStart = strtotime( $open_time );
        $tEnd = strtotime( $close_time );
        $tNow = $tStart;
        $current_time = time();

        // Date format based on user settings
        $date_format = get_option('time_format');
        $date_format_key = preg_replace("/[^\w]+/", "_", $date_format);

        // If Closed today or today's pickup times are over, don't allow a pickup option
        if ( ( in_array( $today_date, $closing_days ) || ( $current_time >= $tEnd ) )  && $num_days_allowed == 1 ) {

            // Set drop down text to let user know store is closed
            $pickup_options['closed_today'] = __( 'Closed today, please check back tomorrow!', $this->plugin_slug );

            // Hide Order Review so user doesn't order anything today
            remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
            remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );

        }
        else {
            // Create array of time options to return to woocommerce_form_field

            // Today
            if ( $i == 0) {

                // Check if it's not too late for pickup
                if ( $current_time < $tEnd ) {

                    // Fix tNow if is pickup possible today
                    if ( $i == 0 ) {
                        $todayStart = $tStart;
                        $delayStart = strtotime("+$delay_minutes minutes", $current_time);
                        while ( $todayStart <= $delayStart ) {
                            $todayStart = strtotime("+$interval minutes", $todayStart);
                        }
                        $tNow = $todayStart;
                    }

                    while ( $tNow <= $tEnd ) {

                        $day_name = __( 'Today', $this->plugin_slug );

                        $option_key = $current_day_name . date( $date_format_key, $tNow );
                        $option_value = $day_name . ' ' . date( $date_format, $tNow );

                        $pickup_options[$option_key] = $option_value;

                        $tNow = strtotime( "+$interval minutes", $tNow );
                    }

                }

            // Other days
            } else {

                if ( !empty($open_time) && !empty($close_time )) {
                    while ( $tNow <= $tEnd ) {

                        $day_name = __( $current_day_name, $this->plugin_slug );

                        $option_key = $current_day_name . date( $date_format_key, $tNow );
                        $option_value = $day_name . ' ' . date( $date_format, $tNow );

                        $pickup_options[$option_key] = $option_value;

                        $tNow = strtotime( "+$interval minutes", $tNow );

                    }

                }
            }

        }

    } // end for loop

    if ( count($pickup_options) == 1) {
        // Set drop down text to let user know store is closed
        $pickup_options['closed_today'] = __( 'Closed today, please check back tomorrow!', $this->plugin_slug );

        // Hide Order Review so user doesn't order anything today
        remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
        remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
    }

    return $pickup_options;
}

最终结果将类似于下拉列表

  
      
  • 尽快
  •   
  • 星期二晚上7:30
  •   
  • 星期二晚上8:00
  •   
  • 星期二晚上8:30
  •   

1 个答案:

答案 0 :(得分:0)

有很多方法可以执行此操作,具体取决于“尽快”选项所需的键值是否必须是动态的。这是通过这种方式在array_merge()的第一个函数中完成的:

public function time_select( $checkout ) { 
    echo '<div id="local-pickup-time-select"><h2>' . __( 'Delivery Time', $this->plugin_slug ) . '</h2>';

    // The default empty value and your translatable sentence to be included
    $array_to_insert = array( 
        ''        => __( 'Select your delivery time', $this->plugin_slug ),
        'as_soon' => __( 'As soon as possible', $this->plugin_slug ) 
    );

    // Getting your calculated hours options array
    $options = self::create_hour_options();

    // Merging both arrays in one
    $options = array_merge( $array_to_insert, $options );

    woocommerce_form_field( 'local_pickup_time_select', array(
        'type'          => 'select',
        'class'         => array( 'local-pickup-time-select-field form-row-wide' ),
        'label'         => __( 'Delivery Time', $this->plugin_slug ),
        'required'      => true,
        'options'       => $options,
    ), $checkout->get_value( 'local_pickup_time_select' ));

    echo '</div>';
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

经过测试和工作。你会得到的:

enter image description here

  

我在你的代码中注意到你的//Translateble days(星期一到星期日)没有在单个变量或数组中设置......