【问题标题】:How to show checkout custom fields value in Woo Commerce order edit page?如何在 Woocommerce 订单编辑页面中显示结帐自定义字段值?
【发布时间】:2021-06-10 13:23:53
【问题描述】:

我正在使用Add a custom select field with collection times in WooCommerce checkout 创建一个收藏下拉菜单

我的问题是如何让我的收集时间显示在 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;
}`

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    您需要使用array_merge()。检查下面的代码。

    array_values() 值会重置数组的键,这就是您得到 7 而不是 8:00 PM 的原因。

    更改以下行。

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

    到

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

    经过测试并且有效。

    【讨论】:

    • 欢迎...很高兴为您提供帮助。
    猜你喜欢
    • 2018-10-01
    • 2019-04-14
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多