How to Add Custom Fields to WooCommerce Checkout Page?

image

You are well aware of the importance of a checkout page if you conduct business online. Due to certain needs, you might occasionally need to customize your eCommerce website. In this video, we’ll demonstrate how to add more fields and personalize the WooCommerce checkout page.  You can either make use of a WooCommerce Checkout Field Editor Plugin or achieve the task by coding. The choice depends on your preference.
Let’s define a checkout page first before moving on to the instruction.

What is the checkout page?

A checkout page is a term used in eCommerce to describe a page that a consumer sees throughout the detailed checkout process. It is typically the most significant page for both the buyer and the vendor. Customers must fill out this page with vital facts such as addresses, billing information, and payment method. No customer will be able to make a purchase if this page malfunctions in any way! We can therefore understand the significance of this page to the sellers.
You also get your own checkout page if you use WordPress to power your website and WooCommerce as your eCommerce solution. WooCommerce does not, however, provide you the option to customize your checkout page through settings since it is a free solution. So, how do you intend to accomplish that?

Adding an Extra WooCommerce Checkout Field

According to our criteria, you may occasionally only need to add WooCommerce custom fields to a checkout page, which can be an uphill task. You will be shown step-by-step how to do this in this tutorial. You’ll be prepared to add a custom field to the checkout page after reading this article. Let’s go on to the important part.

You can add custom fields to the checkout page using two entirely distinct methods, such as using –

  • custom coding (for coders)
  • plugin (for non-coders)

Adding Custom Checkout Fields Through Coding

This method can be used by those who are familiar with coding or who have a basic understanding of it. Using the code provided below, this method adds the custom field to the checkout page. The tasks listed below must be completed in order to begin coding.
First, we need to perform an action on our functions.php file. Copy this entire code to your theme’s function.php file.

/**
 
* Add a custom field to the checkout page
 
*/
 
add_action('woocommerce_after_order_notes', 'custom_checkout_field');
 
function custom_checkout_field($checkout)
 
{
 
echo '<div id="custom_checkout_field"><h2>' . __('New Heading') . '</h2>';
 
woocommerce_form_field('custom_field_name', array(
 
'type' => 'text',
 
'class' => array(
 
'my-field-class form-row-wide'
 
) ,
 
'label' => __('Custom Additional Field') ,
 
'placeholder' => __('New Custom Field') ,
 
) ,
 
$checkout->get_value('custom_field_name'));
 
echo '</div>';
 
}
 
//For data validation of the custom field you can use the code given below:
 
/**
* Checkout Process
*/
add_action('woocommerce_checkout_process', 'customised_checkout_field_process');
 
function customised_checkout_field_process(){
 
// Show an error message if the field is not set.
 
if (!$_POST['customised_field_name']) wc_add_notice(__('Please enter value!') , 'error');
}
 
Consequently, we've included a replacement field and a validation check to the checkout page! Great!
 
/*Let's check to see if the information that the client has supplied in the custom field is being stored or not.
 
This can be done by using the code below:*/
 
/**
* Update the value given in custom field
*/
 
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');
 
function custom_checkout_field_update_order_meta($order_id){
 
if (!empty($_POST['customised_field_name'])) {
 
update_post_meta($order_id, 'Some Field',sanitize_text_field($_POST['customised_field_name']));
 
}
}

Adding Checkout Fields Using a Plugin

We have now added custom fields to our WooCommerce web store with the aforementioned lines of code!

You can also utilize a plugin to add a new field to the checkout page if you are not a programmer. There are numerous plugins available that can be utilized for this. The one plugin that is the most efficient, feature-rich, and user-friendly is the WooCommerce Conditional Checkout Field Editor. So, let’s see how we can edit, add, or delete fields at the checkout page.

You must install and activate the following plugin in order to add an additional custom field to your WooCommerce checkout page.

There are three sections of Fields that you can edit with the WooCommerce checkout page editor:

  • Billing – Billing address and payment section of checkout (payment must be enabled)
  • Shipping – Shipping details section of checkout (shipping must be enabled)
  • Additional – Additional Information section that appears after the billing and shipping sections of checkout, next to order notes (order notes must be enabled)

How To Add Custom Fields to WooCommerce Checkout Page?

Click “Add Field” to create a new checkout field for billing, shipping, or additional sections.

After clicking on Add field button, now you can customize the field according to your preferences. You can do the following settings with each field.

  • Type (Select the type of field you required)
  • Field Name:(Set the field name)
  • Label (Set the Label of the Field to display on frontend)
  • Enable (Enable/Disable the field)
  • Placeholder (Specify placeholder )
  • Field Class (Choose the position or width of fields such as form row first/last, wide)
  • Price (Set price for this field )
  • Required(Make these fields mandatory for Customers)
  • Display for Specific Product or Category (Display field based on specific products or categories in cart)
  • Display only Specific User roles (Show this field to specific users only)
  • Add Conditions (Make the field dependent on another field. Relation between groups is “Or” and the relation between conditions is “And”)

In the “And” operation. all conditions must be true. In the “OR” condition, if any operation is true, the expression will be true.

Now click on save changes and update the checkout page with the extra custom checkout fields. You can place a test order to see the updated checkout page.