Skip to content

Instantly share code, notes, and snippets.

@woogists
Created March 11, 2018 15:11
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save woogists/7241afdfa6f21d561ce85f7247a0f282 to your computer and use it in GitHub Desktop.
Save woogists/7241afdfa6f21d561ce85f7247a0f282 to your computer and use it in GitHub Desktop.
[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>';
}
@UVLabs
Copy link

UVLabs commented May 29, 2021

For anyone wondering, you need to actually save the meta value or else you won't see it on the order page. See this answer: https://wordpress.stackexchange.com/a/376228/76433

add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_fields_update_order_meta' );

function custom_checkout_fields_update_order_meta( $order_id ) {
    update_post_meta( $order_id, 'billing_colonia', sanitize_text_field( $_POST['billing_colonia'] ) );
    update_post_meta( $order_id, 'shipping_colonia', sanitize_text_field( $_POST['shipping_colonia'] ) );
}

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