Reputation: 35
I'm using Add a custom select field with collection times in WooCommerce checkout to create a collection dropdown
my question is how do I get my collection time to show up in the backend of woo
If I select 8 PM as the collection time, the collection time in the backend shows as number 7. Witch is the 7th option (8 PM)
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;
}`
Upvotes: 2
Views: 82
Reputation: 11282
You need to use array_merge()
. check below code.
array_values()
values reset the keys of the array that's why you get 7 instead of 8:00 PM.
change this below line.
$options = array_values( $default + $remaining_times );
to
$options = array_merge( $default, $remaining_times );
Tested and works.
Upvotes: 2