Add custom field to checkout form
-
Hello. I would like to add a field in the checkout form to ask a pick up location. It’s very important to add this for our business. What is the best option to do it ?
Thanks.
Viewing 6 replies - 1 through 6 (of 6 total)
-
Hi,
i just wanna ask if you found a solution.Greetings
No, unfortunately.
I’m looking for a solution as well. I only have the billing infomation fields.
For anyone looking how to add a custom field to the ckeckout form, here is how i added a Comments textarea.
You field will also be added to the admin view of the order and the email notices.
Just paste this in your functions.php
/** * Add custom field to booking object post metabox * * @param object CMB2 object */ function uau_cmb2_order_customer_fields($cmb) { $cmb->add_field(array( 'name' => __('Comments', 'ba-book-everything'), 'id' => 'order-comments', 'type' => 'textarea', )); return; } add_action('babe_cmb2_order_customer_fields', 'uau_cmb2_order_customer_fields', 10); function add_customs_order_fields($output, $args) { $comments = !empty($args['meta']['order-comments']) ? $args['meta']['order-comments'] : ''; $output .= '<h3>' . __('Comments', 'ba-book-everything') . '</h3>'; $output .= '<div class="address_fields_group input_group"> <div class="checkout-form-block"> <div class="checkout_form_input_field ' . (!empty($comments) ? 'checkout_form_input_field_content' : '') . '"> <label class="checkout_form_input_label">' . __('Comments', 'ba-book-everything') . '</label> <textarea class="checkout_input_field" name="order-comments" id="order-comments" value="' . esc_attr($comments) . '"></textarea> </div> </div> </div>'; return $output; } add_filter('babe_checkout_after_contact_fields', 'add_customs_order_fields', 20, 2); function sanitize_customs_order_fields($output, $arr) { $output['order-comments'] = isset($arr['order-comments']) ? sanitize_text_field($arr['order-comments']) : ''; return $output; } add_filter('babe_sanitize_checkout_vars', 'sanitize_customs_order_fields', 20, 2); /** * Add checkout field titles */ function uau_babe_checkout_field_label($field_title, $field_name) { if ($field_name === 'order-comments') { $field_title = __('Comments', 'ba-book-everything'); } return $field_title; } add_filter('babe_checkout_field_label', 'uau_babe_checkout_field_label', 10, 2);
That’s it.
Thanks for that.
Hello, you can try this in the functions.php, made 4 extra fields:
add_filter('babe_checkout_args', 'customtheme_babe_checkout_args', 10, 2); /** * Add checkout fields */ function customtheme_babe_checkout_args( $args_meta, $args ) { $args_meta['new_field'] = isset($args['meta']['new_field']) ? $args['meta']['new_field'] : ''; $args_meta['second_field'] = isset($args['meta']['second_field']) ? $args['meta']['second_field'] : ''; $args_meta['third_field'] = isset($args['meta']['third_field']) ? $args['meta']['third_field'] : ''; $args_meta['fourth_field'] = isset($args['meta']['fourth_field']) ? $args['meta']['fourth_field'] : ''; return $args_meta; } //////// add_filter('babe_checkout_field_label', 'customtheme_babe_checkout_field_label', 10, 2); /** * Add checkout field title */ function customtheme_babe_checkout_field_label( $field_title, $field_name ) { if ($field_name === 'new_field'){ $field_title = __('Hotel', 'textdomain'); } if ($field_name === 'second_field'){ $field_title = __('Second Field', 'textdomain'); } if ($field_name === 'third_field'){ $field_title = __('Third Field', 'textdomain'); } if ($field_name === 'fourth_field'){ $field_title = __('Fourth Field', 'textdomain'); } return $field_title; } //////// add_filter('babe_checkout_field_required', 'customtheme_babe_checkout_field_required', 10, 2); /** * Required tag for checkout field */ function customtheme_babe_checkout_field_required($required_tag, $field_name){ if ($field_name === 'new_field' || $field_name === 'second_field' || $field_name === 'third_field' || $field_name === 'fourth_field'){ $required_tag = 'required="required"'; } return $required_tag; } add_filter('babe_sanitize_checkout_vars', 'customtheme_sanitize_checkout_vars', 10, 2); /** * Add fields to sanitize checkout vars method */ function customtheme_sanitize_checkout_vars( $output, $arr ) { $output['new_field'] = isset($arr['new_field']) ? sanitize_text_field($arr['new_field']) : ''; $output['second_field'] = isset($arr['second_field']) ? sanitize_text_field($arr['second_field']) : ''; $output['third_field'] = isset($arr['third_field']) ? sanitize_text_field($arr['third_field']) : ''; $output['fourth_field'] = isset($arr['fourth_field']) ? sanitize_text_field($arr['fourth_field']) : ''; return $output; }
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Add custom field to checkout form’ is closed to new replies.