Last active
March 7, 2023 18:50
-
-
Save paaljoachim/ae511b704cfa97101faca1251c0418c0 to your computer and use it in GitHub Desktop.
WooCommerce: Adding multiple custom fields to the order notes area in the checkout page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 1. Adds a custom field. NB. I am using some Norwegian words in the below text. | |
* 2. Then adds a validate error message if person does not fill out the field. | |
* 3. Then adds the custom field to the order page. | |
https://businessbloomer.com/woocommerce-add-custom-checkout-field-php/ | |
https://businessbloomer.com/woocommerce-add-shipping-phone-checkout/ | |
https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-6 | |
*/ | |
add_action( 'woocommerce_before_order_notes', 'my_custom_checkout_field' ); | |
function my_custom_checkout_field( $checkout ) { | |
woocommerce_form_field( 'billing_year', array( | |
'type' => 'checkbox', | |
'required' => 'true', | |
'class' => array('year-class form-row-wide'), | |
'label' => __('I am over 18.'), | |
'placeholder' => __(''), | |
), $checkout->get_value( 'year' )); | |
woocommerce_form_field( 'forening', array( | |
'type' => 'text', | |
'required' => 'true', | |
'class' => array('forening-class form-row-wide'), | |
'label' => __('Which sportsclub/organization or class do you belong to?'), | |
), $checkout->get_value( 'forening' )); | |
woocommerce_form_field( 'antall_personer', array( | |
'type' => 'text', | |
'required' => 'true', | |
'class' => array('antall-personer-class form-row-wide'), | |
'label' => __('How many will sell these products?'), | |
'priority' => 28, | |
), $checkout->get_value( 'antall_personer' )); | |
woocommerce_form_field( 'kontaktperson', array( | |
'type' => 'select', | |
'required' => 'true', | |
'class' => array('kontaktperson-class form-row-wide'), | |
'label' => __('Contact from our company'), | |
'options' => array( // options for <select> or <input type="radio" /> | |
'' => 'Please select', // empty values means that field is not selected | |
'ingen_kontakt_person' => 'No contact person', // 'value'=>'Name' | |
'Regine' => 'Employee 1', | |
'mei_mei' => 'Employee 2' | |
) | |
), $checkout->get_value( 'kontaktperson' )); | |
} | |
/** | |
* 2. Process the checkout - We then need to validate the field. If someone does not fill out the field they will get an error message. | |
*/ | |
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); | |
function my_custom_checkout_field_process() { | |
// Check if set, if its not set add an error. | |
if ( ! $_POST['year'] ) | |
wc_add_notice( __( 'Fyll ut: Bekreft alder' ), 'error' ); | |
if ( ! $_POST['forening'] ) | |
wc_add_notice( __( 'Fyll ut: Hvilken idrettslag/ forening/ klasse?' ), 'error' ); | |
if ( ! $_POST['antall_personer'] ) | |
wc_add_notice( __( 'Fyll ut: Antall personer som skal selge?' ), 'error' ); | |
} | |
/** | |
* 3. Display field value on the order edit page. | |
*/ | |
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); | |
function my_custom_checkout_field_display_admin_order_meta($order){ | |
echo '<p><strong>'.__('Over 18').':</strong> ' . get_post_meta( $order->id, 'Over 18', true ) . '</p>'; | |
echo '<p><strong>'.__('Foreningen').':</strong> ' . get_post_meta( $order->id, 'Foreningen', true ) . '</p>'; | |
echo '<p><strong>'.__('Antall personer').':</strong> ' . get_post_meta( $order->id, 'Antall personer', true ) . '</p>'; | |
} |
Thank you for this. I have just tried woocommerce custom checkout fields plugin for this functionality and it is good so far.
Good to hear!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Glad to be of help!
BtwI am recreating my easywebdesigntutorials.com web site. It is taking a while to do so. In the mean time I am recreating older tutorials and adding new ones here: https://new.easywebdesigntutorials.com/
At the moment the new site is hidden from search engines, as I do not plan to keep the url, as it will be transferred over to the original tutorial site in a while. I still have a lot of original material to update and recreate at the new site before moving it over.
Here is a direct link to all the WooCommerce tutorials I have presently added:
https://easywebdesigntutorials.com/tutorial_categories/wordpress/woocommerce/