gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:31:10 29 4
gpt4 key购买 nike

我正在使用 Add a custom select field with collection times in WooCommerce checkout创建集合下拉列表
我的问题是如何让我的收集时间出现在 woo 的后端
collection time
如果我选择晚上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;
}`

最佳答案

您需要使用 array_merge() .检查下面的代码。array_values() values 重置数组的键,这就是为什么你得到 7 而不是 8:00 PM。
改变这个下面的行。

$options = array_values( $default + $remaining_times );
$options = array_merge( $default, $remaining_times );
测试和工作。
enter image description here

关于wordpress - 如何在 Woo Commerce 订单编辑页面中显示结帐自定义字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67922074/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com