Skip to content

Instantly share code, notes, and snippets.

@Greg808
Last active December 18, 2019 11:26
Show Gist options
  • Save Greg808/52b5d5fb469731de005e7d5092e60040 to your computer and use it in GitHub Desktop.
Save Greg808/52b5d5fb469731de005e7d5092e60040 to your computer and use it in GitHub Desktop.
<?php
// Frontend: Display the custom billing fields (in checkout and my account)
add_filter( 'woocommerce_billing_fields' ,'add_custom_billing_fields', 20, 1 );
function add_custom_billing_fields( $fields ) {
$fields['billing_address_3'] = array(
'label' => __( 'Stiege', 'woocommerce' ),
'placeholder' => _x('Stiege', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
// Save the custom billing fields (once order is placed)
add_action( 'woocommerce_checkout_create_order', 'save_custom_billing_fields', 20, 2 );
function save_custom_billing_fields( $order, $data ) {
if ( isset( $_POST['billing_address_3'] ) && ! empty( $_POST['billing_address_3'] ) ) {
$order->update_meta_data('_billing_address_3', sanitize_text_field( $_POST['billing_address_3'] ) );
update_user_meta( $order->get_customer_id(), 'billing_address_3', sanitize_text_field( $_POST['billing_address_3'] ) );
}
}
// This part does not work
// Backend: Display editable custom billing fields
function order_admin_custom_fields( $fields ) {
$fields['address_3'] = array(
'label' => __( 'Stiege', 'woocommerce' ),
'show' => true,
'wrapper_class' => 'form-field-wide',
'style' => '',
);
return $fields;
}
add_filter( 'woocommerce_admin_billing_fields' , 'order_admin_custom_fields' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment