如何在 Woo Commerce 订单编辑页面中显示结帐自定义字段值?

问题描述 投票:0回答:1

我正在使用 在 WooCommerce 结账中添加带有收集时间的自定义选择字段来创建收集下拉列表

我的问题是如何让我的收集时间显示在 woo 的后端

如果我选择晚上8点作为收集时间,后台的收集时间显示为数字7。女巫是第7个选项(晚上8点)

function collection_time( $checkout ) {
    // Display time, open and close time
    date_default_timezone_set('Europe/London');
    $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_values( $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_checkout_process', 'process_collection_time_field');
function process_collection_time_field() {
    if (isset($_POST['daypart']) && empty($_POST['daypart']) ) {
        wc_add_notice( __( 'Please select a collection time' ), 'error' );
    }
}


//* Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'select_checkout_field_update_order_meta');
function select_checkout_field_update_order_meta( $order_id ) {
if ($_POST['daypart']) update_post_meta( $order_id, 'daypart', esc_attr($_POST['daypart']));
}

//* Display field value on the order edition page

add_action( 'woocommerce_admin_order_data_after_billing_address', 'njengah_select_checkout_field_display_admin_order_meta', 10, 1 );
function njengah_select_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Collection Time').':</strong> ' . get_post_meta( $order->id, 'daypart', true ) . '</p>';
}

//* Add selection field value to emails
add_filter('woocommerce_email_order_meta_keys', 'njengah_select_order_meta_keys');
function njengah_select_order_meta_keys( $keys ) {
$keys['Daypart:'] = 'daypart';
return $keys;
}`
php wordpress woocommerce
1个回答
2
投票

您需要使用

array_merge()
。检查下面的代码。

array_values()
值会重置数组的键,这就是为什么你得到 7 而不是 8:00 PM。

更改以下行。

$options = array_values( $default + $remaining_times );

$options = array_merge( $default, $remaining_times );

已测试且有效。

© www.soinside.com 2019 - 2024. All rights reserved.