Skip to content

Instantly share code, notes, and snippets.

@paaljoachim
Last active September 12, 2019 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paaljoachim/86c5bb6f9e4fe55d439bffc4dfa5991b to your computer and use it in GitHub Desktop.
Save paaljoachim/86c5bb6f9e4fe55d439bffc4dfa5991b to your computer and use it in GitHub Desktop.
WooCommerce Checkout: Add multiple custom fields to billing area - customer case
// Initial inspiration: https://businessbloomer.com/woocommerce-add-shipping-phone-checkout/
// My Custom Fields
// Can be added to billing, shipping or order area. For the account page use the word account.
add_filter( 'woocommerce_checkout_fields', 'custom_fields_checkout' );
function custom_fields_checkout( $fields ) {
$fields['billing']['billing_idrettslag'] = array(
'label' => 'Hvilken idrettslag/ forening/ klasse?',
'type' => 'text',
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 28,
);
$fields['billing']['billing_antall'] = array(
'label' => 'Antall personer som skal selge',
'type' => 'text',
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 29,
);
$fields['billing']['billing_kontakt'] = array(
'label' => 'Kontaktperson hos Celias',
'type' => 'select',
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 75,
'options' => array( // options for <select> or <input type="radio" />
'' => 'Velg', // empty values means that field is not selected
// 'value'=>'Name'
'Ingen kontakt person' => 'Ingen kontakt person',
'Regine' => 'Regine',
'Mei Mei' => 'Mei Mei'
)
);
$fields['order']['billing_nyhetsbrev'] = array(
'label' => 'Jeg vil motta nyhetsbrev fra Celias.no',
'type' => 'select',
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 29,
'options' => array( // options for <select> or <input type="radio" />
'' => 'Velg', // empty values means that field is not selected
// 'value'=>'Name'
'Ja' => 'Ja',
'Nei' => 'Nei',
)
);
// Added below the order notes area.
$fields['order']['billing_alder'] = array(
'label' => 'Jeg er over 18.',
'type' => 'checkbox',
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 28,
);
return $fields;
}
// https://stackoverflow.com/questions/12958193/show-custom-field-on-order-in-woocommerce
// Custom fields that were added to the Order area will here be shown in the Billing area inside the WP backend WooCommerce -> Order and "Order Details" page.
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['billing']['billing_alder'])) {
update_post_meta($order_id, 'Jeg er over 18', esc_attr($_POST['billing']['billing_alder2']));
}
if (!empty($_POST['billing']['billing_nyhetsbrev'])) {
update_post_meta($order_id, 'Jeg vil motta nyhetsbrev fra Celias.no?', esc_attr($_POST['billing']['billing_nyhetsbrev2']));
}
}
// Display custom fields in the backend Order details screen.
add_action( 'woocommerce_admin_order_data_after_billing_address', 'customfields_billing_checkbox_checkout_display' );
function customfields_billing_checkbox_checkout_display( $order ){
echo '<p><b>Hvilken idrettslag/ forening/ klasse?</b> ' . get_post_meta( $order->get_id(), '_billing_idrettslag', true ) . '</p>';
echo '<p><b>Antall personer som skal selge:</b> ' . get_post_meta( $order->get_id(), '_billing_antall', true ) . '</p>';
echo '<p><b>Kontaktperson hos Celias?</b> ' . get_post_meta( $order->get_id(), '_billing_kontakt', true ) . '</p>';
echo '<p><b>Nyhetsbrev:</b> ' . get_post_meta( $order->get_id(), '_billing_nyhetsbrev', true ) . '</p>';
echo '<p><b>Jeg er over 18:</b> ' . get_post_meta( $order->get_id(), '_billing_alder', true ) . '</p>';
}
// Display custom fields in the admin and customer e-mails.
/* -------- E-mail custom fields ------ */
add_action( 'woocommerce_email_header', 'ts_email_header', 10, 2 );
function ts_email_header( $email_heading, $email ) {
echo "<p> Thanks for shopping with us. Here is your invoice </p>";
}
add_action( 'woocommerce_email_order_details', 'ts_email_order_details', 10, 4 );
function ts_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
echo '<p>Hey '.$order->get_billing_first_name().', We hope you had fun shopping with us.';
}
add_action( 'woocommerce_email_before_order_table', 'ts_email_before_order_table', 10, 4 );
function ts_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
echo "We hope to see you soon";
}
add_action( 'woocommerce_email_after_order_table', 'ts_email_after_order_table', 10, 4 );
function ts_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
echo '<h2>"Additional information"</h2>';
echo '<p><strong>'.__('Hvilken idrettslag/ forening/ klasse?').':</strong> <br/>' . get_post_meta( $order->get_id(), '_billing_idrettslag', true ) . '</p>';
echo '<p><strong>'.__('Antall personer som skal selge').':</strong> <br/>' . get_post_meta( $order->get_id(), '_billing_antall', true ) . '</p>';
echo '<p><strong>'.__('Kontaktperson hos Celias').':</strong> <br/>' . get_post_meta( $order->get_id(), '_billing_kontakt', true ) . '</p>';
echo '<p><strong>'.__('Jeg vil motta nyhetsbrev fra Celias.no').':</strong> <br/>' . get_post_meta( $order->get_id(), '_billing_nyhetsbrev', true ) . '</p>';
echo '<p><strong>'.__('Jeg er over 18.').':</strong> <br/>' . get_post_meta( $order->get_id(), '_billing_alder', true ) . '</p>';
}
add_action( 'woocommerce_email_customer_details', 'ts_email_customer_details', 10 , 4);
function ts_email_customer_details($order, $sent_to_admin, $plain_text, $email){
echo '<p>Your order is sent to the following address</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment