Last active
July 21, 2022 19:16
-
-
Save nadeem-khan/adaa4e7efab373535294 to your computer and use it in GitHub Desktop.
Create Custom Fields in Woocommerce Checkout Page and Display them in Orders Details Page in Admin Area
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
<?php | |
/** | |
* Plugin Name: Flowershop - WooCommerceCustomOverrides | |
* Plugin URI: http://chillopedia.com | |
* Description: Overrides WooCommerce Checkout Form Fields | |
* Version: 1.0 | |
* Author: Nadeem Khan | |
* Author URI: http://facebook.com/nadeem.khan | |
* License: GPL | |
*/ | |
//Save this as a php file in a folder (with any name) under wp-content/plugins/ and then activate the plugin to both add custom fields and reorder the fields in woocommerce checkout form: | |
// Hook in | |
add_filter('woocommerce_checkout_fields', 'custom_override_shipping_checkout_fields'); | |
// Our hooked in function - $fields is passed via the filter! | |
function custom_override_shipping_checkout_fields($fields) { | |
$fields['shipping']['shipping_location_type'] = array( | |
'type' => 'select', | |
'label' => __('Location Type', 'woocommerce'), | |
'clear' => false, | |
'options' => array( | |
'home' => __('Home', 'woocommerce'), | |
'apartment' => __('Apartment', 'woocommerce'), | |
'business' => __('Business', 'woocommerce'), | |
'hospital' => __('Hospital', 'woocommerce'), | |
'funeral-home' => __('Funeral Home', 'woocommerce'), | |
'church' => __('Church', 'woocommerce'), | |
'other' => __('Other', 'woocommerce') | |
), | |
'placeholder' => _x('Location Type', 'placeholder', 'woocommerce'), | |
'required' => true | |
); | |
$fields['shipping']['shipping_email'] = array( | |
'type' => 'text', | |
'label' => __('Email Address', 'woocommerce'), | |
'clear' => false, | |
'placeholder' => _x('Enter Email', 'placeholder', 'woocommerce'), | |
'required' => true | |
); | |
$fields['shipping']['shipping_phone'] = array( | |
'type' => 'text', | |
'label' => __('Phone', 'woocommerce'), | |
'clear' => false, | |
'placeholder' => _x('Enter Phone Number', 'placeholder', 'woocommerce'), | |
'required' => true | |
); | |
return $fields; | |
} | |
// Hook in | |
add_filter('woocommerce_checkout_fields', 'custom_override_billing_checkout_fields'); | |
// Our hooked in function - $fields is passed via the filter! | |
function custom_override_billing_checkout_fields($fields) { | |
$fields['billing']['billing_location_type'] = array( | |
'type' => 'select', | |
'label' => __('Location Type', 'woocommerce'), | |
'clear' => false, | |
'options' => array( | |
'home' => __('Home', 'woocommerce'), | |
'apartment' => __('Apartment', 'woocommerce'), | |
'business' => __('Business', 'woocommerce'), | |
'hospital' => __('Hospital', 'woocommerce'), | |
'funeral-home' => __('Funeral Home', 'woocommerce'), | |
'church' => __('Church', 'woocommerce'), | |
'other' => __('Other', 'woocommerce') | |
), | |
'placeholder' => _x('Location Type', 'placeholder', 'woocommerce'), | |
'required' => true | |
); | |
return $fields; | |
} | |
add_filter("woocommerce_checkout_fields", "order_shipping_fields"); | |
function order_shipping_fields($fields) { | |
$order = array( | |
"shipping_country", | |
"shipping_first_name", | |
"shipping_last_name", | |
"shipping_location_type", | |
"shipping_address_1", | |
"shipping_address_2", | |
"shipping_city", | |
"shipping_state", | |
"shipping_postcode", | |
"shipping_email", | |
"shipping_phone" | |
); | |
foreach ($order as $field) { | |
$ordered_fields[$field] = $fields["shipping"][$field]; | |
} | |
$fields["shipping"] = $ordered_fields; | |
unset($fields['order']['order_comments']); | |
return $fields; | |
} | |
add_filter("woocommerce_checkout_fields", "order_billing_fields"); | |
function order_billing_fields($fields) { | |
$order = array( | |
"billing_country", | |
"billing_first_name", | |
"billing_last_name", | |
"billing_location_type", | |
"billing_address_1", | |
"billing_address_2", | |
"billing_city", | |
"billing_state", | |
"billing_postcode", | |
"billing_email", | |
"billing_phone" | |
); | |
foreach ($order as $field) { | |
$ordered_fields[$field] = $fields["billing"][$field]; | |
} | |
$fields["billing"] = $ordered_fields; | |
return $fields; | |
} | |
/** | |
* Add the field to the checkout | |
*/ | |
add_action('woocommerce_after_order_notes', 'personal_message_checkout_field'); | |
function personal_message_checkout_field($checkout) { | |
echo '<h3 class="entry-title">' . __('WRITE A PERSONAL MESSAGE') . '</h3>'; | |
echo '<div class="entry-wrapper">'; | |
woocommerce_form_field('card_type', array( | |
'type' => 'select', | |
'class' => array('form-row-wide'), | |
'label' => __('Select the card type'), | |
'clear' => false, | |
'options' => array( | |
'anniversary' => __('Anniversary', 'woocommerce'), | |
'birthday' => __('Birthday', 'woocommerce'), | |
'business-gift' => __('Business Gift', 'woocommerce'), | |
'congratulations' => __('Congratulations', 'woocommerce'), | |
'funeral' => __('Funeral', 'woocommerce'), | |
'get-well' => __('Get Well', 'woocommerce'), | |
'grandparents-day' => __('Grandparents Day', 'woocommerce'), | |
'housewarming' => __('Housewarming', 'woocommerce'), | |
'imsorry' => __('I\'m Sorry', 'woocommerce'), | |
'just-because' => __('Just Because', 'woocommerce'), | |
'love-and-romance' => __('Love & Romance', 'woocommerce'), | |
'new-baby' => __('New Baby', 'woocommerce'), | |
'retirement' => __('Retirement', 'woocommerce'), | |
'sympathy' => __('Sympathy', 'woocommerce'), | |
'thank-you' => __('Thank You', 'woocommerce'), | |
'thinking-of-you' => __('Thinking of You', 'woocommerce'), | |
'other' => __('Other', 'woocommerce') | |
), | |
'required' => true, | |
'placeholder' => __('Select card type'), | |
), $checkout->get_value('card_type')); | |
woocommerce_form_field('your_message', array( | |
'type' => 'textarea', | |
'class' => array('form-row form-row-wide'), | |
'label' => __('Your Message'), | |
'placeholder' => __('Enter your message (must be less than 200 characters)'), | |
'required' => true | |
), substr($checkout->get_value('your_message'), 200)); | |
echo '</div>'; | |
} | |
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); | |
function my_custom_checkout_field_process() { | |
if ($_POST['shipping']['shipping_location_type'] == "select") { | |
wc_add_notice(__('Please tell us the Location Type we are delivering to.'), 'error'); | |
} | |
if (!$_POST['card_type']) { | |
wc_add_notice(__('Please tell us the type of the Card that we have to deliver.'), 'error'); | |
} | |
if (!$_POST['your_message']) { | |
wc_add_notice(__('Please tell us the Message that we have to enter in your card.'), 'error'); | |
} | |
} | |
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['shipping']['shipping_location_type'])) { | |
update_post_meta($order_id, 'Location Type', esc_attr($_POST['shipping']['shipping_location_type'])); | |
} | |
if (!empty($_POST['card_type'])) { | |
update_post_meta($order_id, 'Card Type', sanitize_text_field($_POST['card_type'])); | |
} | |
if (!empty($_POST['your_message'])) { | |
update_post_meta($order_id, 'Your Message', sanitize_text_field($_POST['your_message'])); | |
} | |
} | |
add_action('woocommerce_admin_order_data_after_shipping_address', 'my_custom_fields_display_admin_order_meta', 10, 1); | |
function my_custom_fields_display_admin_order_meta($order) { | |
echo '<p><strong>' . __('Email') . ':</strong><br> ' . get_post_meta($order->id, '_shipping_email', true) . '</p>'; | |
echo '<p><strong>' . __('Phone') . ':</strong><br> ' . get_post_meta($order->id, '_shipping_phone', true) . '</p>'; | |
echo '<p><strong>' . __('Location Type') . ':</strong><br> ' . get_post_meta($order->id, '_shipping_location_type', true) . '</p>'; | |
echo "<h4>Personal Message</h4>"; | |
echo '<p><strong>' . __('Card Type') . ':</strong><br> ' . get_post_meta($order->id, 'Card Type', true) . '</p>'; | |
echo '<p><strong>' . __('Your Message') . ':</strong><br> ' . get_post_meta($order->id, 'Your Message', true) . '</p>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how many file I need to create and where I can move these?