Create a gist now

Instantly share code, notes, and snippets.

Embed
[Customizing checkout fields using actions and filters] Add new shipping fields to WooCommerce
// 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['shipping']['shipping_phone'] = array(
'label' => __('Phone', 'woocommerce'),
'placeholder' => _x('Phone', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Phone From Checkout Form').':</strong> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}
@cabrailsford

This comment has been minimized.

Show comment
Hide comment
@cabrailsford

cabrailsford Mar 26, 2018

While this does add the field to display on the checkout form, it does not seem to save the submitted data to the database, despite the reassurance in this tutorial, unless there is something I blatantly missed.

While this does add the field to display on the checkout form, it does not seem to save the submitted data to the database, despite the reassurance in this tutorial, unless there is something I blatantly missed.

@McBishop42

This comment has been minimized.

Show comment
Hide comment
@McBishop42

McBishop42 Apr 8, 2018

same issue

same issue

@aefurrer

This comment has been minimized.

Show comment
Hide comment
@aefurrer

aefurrer May 17, 2018

Same Issue here, docs state after implementing the example in their docs, represented in this gist:

What do we do with the new field? Nothing. Because we defined the field in the checkout_fields array, the field is automatically processed and saved to the order post meta (in this case, _shipping_phone). If you want to add validation rules, see the checkout class where there are additional hooks you can use.

The fields created using this method are not automatically saved to the order post meta

Same Issue here, docs state after implementing the example in their docs, represented in this gist:

What do we do with the new field? Nothing. Because we defined the field in the checkout_fields array, the field is automatically processed and saved to the order post meta (in this case, _shipping_phone). If you want to add validation rules, see the checkout class where there are additional hooks you can use.

The fields created using this method are not automatically saved to the order post meta

@aefurrer

This comment has been minimized.

Show comment
Hide comment
@aefurrer

aefurrer May 17, 2018

As a follow up, here is what I was able to decipher, via this old Forum:
https://wordpress.org/support/topic/added-checkout-fields-not-saving-to-post_meta/

Firstly, it appears the gist on this page only works since the field being added is prefixed with shipping_ .

In my specific situation, I wanted to add a new billing field, which were not saving to DB when I wrote it like this:

$fields['billing']['hotel_name']

But changing it to this, worked for me:

$fields['billing']['billing_hotel_name']

So docs would be well served to be updated to state that the field key must be prefixed appropriately to work.

aefurrer commented May 17, 2018

As a follow up, here is what I was able to decipher, via this old Forum:
https://wordpress.org/support/topic/added-checkout-fields-not-saving-to-post_meta/

Firstly, it appears the gist on this page only works since the field being added is prefixed with shipping_ .

In my specific situation, I wanted to add a new billing field, which were not saving to DB when I wrote it like this:

$fields['billing']['hotel_name']

But changing it to this, worked for me:

$fields['billing']['billing_hotel_name']

So docs would be well served to be updated to state that the field key must be prefixed appropriately to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment