Hello there,
This particular forum is mostly for questions related to the free WooCommerce plugin and its included features and functionalities.
For feedback on any code that you’ve written, it will be better to get in touch with the web developer community at:
* https://wordpress.stackexchange.com/ or https://stackoverflow.com/
* https://www.facebook.com/groups/advanced.woocommerce
@eri32s98
Thanks for replying, it’s appreciated.
OK I will repost in those new forums.
Thanks very much.
@lordliverpool
The first function uses “Deliver Time” as the meta key, the second uses “delivery_time”. The keys should be the same.
Changing that field made no difference 🙁
Delivery Time still isn’t being populated in Order Edit
Here’s my code:
/*************************************************************************************/
/* Add Delivery Time */
/*********************/
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['billing']['delivery_time'] = array(
'type' => 'select',
'label' => __('Delivery Time', 'woocommerce'),
'placeholder' => __('Choose a Time', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['delivery_time']['options'] = array(
'option_1' => 'Morning: 7am-9am',
'option_2' => 'Evening: 7pm-9pm'
);
return $fields;
}
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['delivery_time'] )
wc_add_notice( __( 'Please choose a delivery time' ), 'error' );
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['delivery_time'] ) ) {
update_post_meta( $order_id, 'Delivery Time', sanitize_text_field( $_POST['delivery_time'] ) );
}
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Delivery Time').':</strong> ' . get_post_meta( $order->id, 'delivery_time', true ) . '</p>';
}
/*************************************************************************************/
I know this sounds obvious but do I need to create a new column for Delivery Time in a database table to insert the value?
No, because the order_id and the meta_key are enough to identify the relevant value. All sorts of odd bits of data get put in the postmeta table.
You still have different meta_keys in update_post_meta() and in the later get_post_meta().
Can’t remember why I did it, but on mine I have the update_post_meta() in the
‘woocommerce_new_order’ hook, not in the ‘woocommerce_checkout_update_order_meta’ hook.
I had phpMyAdmin open at the postmeta table to make sure the value was actually been added.
@lorro
Sorry for the slow reply! Xmas and New Year happened.
Anyway I found a solution here:
https://www.wpstud.io/add-custom-select-field-woocommerce-checkout-page/
If anyone else wants to add a custom field to the WooCommerce checkout then that tutorial pretty much covers it.
Cheers
I’m glad to hear you’ve solved it! Also thanks for providing a link to help others in future, that’s great.
I’ll close this now, all the best!