In WooCommerce, I can get a field hooked into my WooCommerce cart page & showing up OK using the following in my theme's (Storefront) functions PHP:
<?
// ADD Custom Fields to Checkout Page
/**
* Add the field to the checkout
**/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
date_default_timezone_set('America/Los_Angeles');
$mydateoptions = array('' => __('Select PickupDate', 'woocommerce' ));
echo '<div id="my_custom_checkout_field"><h3>'.__('Delivery Info').'</h3>';
woocommerce_form_field( 'order_pickup_date', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'id' => 'datepicker',
'required' => true,
'label' => __('Delivery Date'),
'placeholder' => __('Select Date'),
'options' => $mydateoptions
),$checkout->get_value( 'order_pickup_date' ));
echo '</div>';
}
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error.
if (!$_POST['order_pickup_date'])
wc_add_notice( '<strong>PickupDate</strong> ' . __( 'is a required field.', 'woocommerce' ), 'error' );
}
/**
* Update the order meta with field value
**/
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 ($_POST['order_pickup_date']) update_post_meta( $order_id, 'PickupDate', esc_attr($_POST['order_pickup_date']));
}
?>
However, the datepicker widget is not initiated when you click into the field. I know the following code is required in the header for JQuery to work:
<script>
$( function() {
$( "#datepicker" ).datepicker(); } );
</script>
This did not work for me, so I thought to put this function into the checkout.js file in Storefront theme, but the added field didn't have calendar widget functionality.
There's a lot of .js in the them do I need to start a new one for includes?
First you will need to:
Enqueu main jquery-ui datepicker script
Change your custom script starting it with jQuery(function($){ instead of $(function(){ …
So to enable datepicker for your custom text field your code will be:
// Register main datepicker jQuery plugin script
add_action( 'wp_enqueue_scripts', 'enabling_date_picker' );
function enabling_date_picker() {
// Only on front-end and checkout page
if( is_admin() || ! is_checkout() ) return;
// Load the datepicker jQuery-ui plugin script
wp_enqueue_script( 'jquery-ui-datepicker' );
}
// Call datepicker functionality in your custom text field
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field', 10, 1);
function my_custom_checkout_field( $checkout ) {
date_default_timezone_set('America/Los_Angeles');
$mydateoptions = array('' => __('Select PickupDate', 'woocommerce' ));
echo '<div id="my_custom_checkout_field">
<h3>'.__('Delivery Info').'</h3>';
// YOUR SCRIPT HERE BELOW
echo '
<script>
jQuery(function($){
$("#datepicker").datepicker();
});
</script>';
woocommerce_form_field( 'order_pickup_date', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'id' => 'datepicker',
'required' => true,
'label' => __('Delivery Date'),
'placeholder' => __('Select Date'),
'options' => $mydateoptions
),$checkout->get_value( 'order_pickup_date' ));
echo '</div>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works. On storefront theme you will get that:
You may need to style it…
Just an update that this is no longer necessary. With Woocommerce you can simply change the style to 'date' and it deals with the rest for you - yahoo!
Related
Im looking to find some php code that will allow me to extract information (Name, address etc) from the checkout fields and add it to the order meta data.
Looking for this to be a simple as possible
Ive previously found this code which allows a custom box to be added to the checkout page, and I sort of understand how it works, however I want to capture their name when they type it into the billing first name box. I can seem to grasp how to capture this data and put it into the order meta data, Ive tried shortening the code and editing it a few times but I dont seem to be winning
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_phone'] = array(
'label' => __('Phone', 'woocommerce'),
'placeholder' => _x('Phone', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Phone From Checkout Form').':</strong> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
/**
* Process the checkout
*/
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['my_field_name'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
}
/**
* Update the order meta with field value
*/
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['my_field_name'] ) ) {
update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
}
}
I like this and it does work but just not in the way i need it to work. thanks for any and all help
There are some mistakes and missing things in your code… Try the following replacement code instead:
// Add shipping phone (in checkout and My account edit shipping address) and save field value
add_action( 'woocommerce_shipping_fields', 'add_shipping_phone_field' );
function add_shipping_phone_field( $fields ) {
$fields['shipping_phone'] = array(
'label' => __('Phone', 'woocommerce'),
'placeholder' => _x('Phone', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
// Display shipping phone value on the order edit pages under shipping section
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'display_shipping_phone_in_admin_orders' );
function display_shipping_phone_in_admin_orders( $order ){
$phone_value = $order->get_meta('_shipping_phone');
if ( ! empty($phone_value) ) {
echo '<p><strong>'.__('Shipping phone').':</strong> ' . $phone_value . '</p>';
}
}
// Add a custom checkout field
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
woocommerce_form_field( 'my_field_slug', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('My custom field'),
'placeholder' => __('Enter something… '),
'required' => true,
), $checkout->get_value( 'my_field_slug' ) );
echo '</div>';
}
// Validate required checkout fields
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 ( isset($_POST['my_field_slug']) && empty($_POST['my_field_slug']) ) {
wc_add_notice( __( '"My custom field" is a required field.' ), 'error' );
}
}
// Add custom checkout field value as custom order meta data
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order ) {
if ( isset($_POST['my_field_slug']) && ! empty($_POST['my_field_slug']) ) {
$order->update_meta_data( 'My Field', sanitize_text_field( $_POST['my_field_slug'] ) );
}
}
// Display "My field" value on the order edit pages under billing section
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_my_custom_checkout_field_in_admin_orders', 10, 1 );
function display_my_custom_checkout_field_in_admin_orders($order){
$my_field_value = $order->get_meta('My Field');
if ( ! empty($my_field_value) ) {
echo '<p><strong>'.__('My field').':</strong> ' . $my_field_value . '</p>';
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Now If you need to extract some data from Woocommerce existing checkout fields and combine it in a custom way to save it as custom order meta data, try to be more explicit:
What fields?
How you want to combine it?
What is the custom field slug to be used to save that data combination?
I need to add a checkbox with the option to select all the checkboxes below it on the checkout page in woocommerce. How could I do this using php or javascript ?? Or if there is another better option, I will be happy.
So far, I have used the "code snippets" plugin to add checkboxes. But there's no way to add the feature I need ... at least I think so. :)
Thank you in advance for everything ... I have used this code on the plugin...
add_action( 'woocommerce_review_order_before_submit', 'bt_add_checkout_checkbox', 10 );
/**
* Add WooCommerce Checkbox checkout
*/
function bt_add_checkout_checkbox() {
woocommerce_form_field( 'checkout-checkbox', array( // CSS ID
'type' => 'checkbox',
'class' => array('form-row mycheckbox'), // CSS Class
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true, // Mandatory or Optional
'label' => 'Ok I agree with Checkout Checkbox Page', // Label and Link
));
}
add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
/**
* Alert if checkbox not checked
*/
function bt_add_checkout_checkbox_warning() {
if ( ! (int) isset( $_POST['checkout-checkbox'] ) ) {
wc_add_notice( __( 'Please acknowledge the Checkbox' ), 'error' );
}
}
Can I use it also for select all checkbox??
Just add a js file with your function bt_add_checkout_checkbox_warning(), or like this in functions.php file with child theme.
function my_hook() {
if ( is_page(your page id here) ) { // use is_page or is_post to identify page
wp_enqueue_script('custom', get_stylesheet_directory_uri().'/js/custom.js');
}
}
add_action( 'wp_enqueue_scripts', 'my_hook' );
create another checkbox id #checkAll, for example, so you can toggle like this in your custom.js file
$("#checkAll").click(function () {
$('.input-checkbox').prop('checked', this.checked);
});
jsfiddle
I have a checkout field and I want to add date ranges instead of picking a single date. Is it possible to use the snippets from jqueryUI to style it and make it a range of dates? I use this function in Woocommerce checkout page on Wordpress.
Below is my current code in the functions.php where I've added the date.
// Register main datepicker jQuery plugin script
add_action( 'wp_enqueue_scripts', 'enabling_date_picker' );
function enabling_date_picker() {
// Only on front-end and checkout page
if( is_admin() || ! is_checkout() ) return;
// Load the datepicker jQuery-ui plugin script
wp_enqueue_script( 'jquery-ui-datepicker' );
}
// Call datepicker functionality in your custom text field
add_action('woocommerce_before_order_notes', 'my_custom_checkout_field', 10, 1);
function my_custom_checkout_field( $checkout ) {
date_default_timezone_set('America/Los_Angeles');
$mydateoptions = array('' => __('Select PickupDate', 'woocommerce' ));
echo '<div id="my_custom_checkout_field">
<h3>'.__('Check In Date').'</h3>';
echo '
<script>
jQuery(function($){
$("#datepicker").datepicker();
});
</script>';
woocommerce_form_field( 'order_checkin_date', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'id' => 'datepicker',
'required' => true,
'label' => __('Check-in Date'),
'placeholder' => __('Select Date'),
'options' => $mydateoptions
),$checkout->get_value( 'order_checkin_date' ));
echo '</div>';
}
This information is available in the jQueryUI documentation. Here's an example of restricting the user to only allow a date 20 days in the past and 1 month + 20 days into the future. Replace your current $("#datepicker").datepicker(); declaration with the following
<script>
jQuery( function() {
$( "#datepicker" ).datepicker({ minDate: -20, maxDate: "+1M +10D" });
} );
</script>
To enable a date range with jQuery-ui Datepicker, there is 2 ways:
With 2 imput text fields that will display a datepicker each with "From" and "To" dates range.
A unique inline date-picker with 2 hidden fields for "from" and "to" dates range, like in this answer.
Here Below based on jQuery-ui datepicker range official documentation, you will be able to set a date range in checkout:
// Register main datepicker jQuery plugin script
add_action( 'wp_enqueue_scripts', 'enabling_date_picker' );
function enabling_date_picker() {
// Only on front-end and checkout page
if( is_admin() || ! is_checkout() ) return;
// Load the datepicker jQuery-ui plugin script
wp_enqueue_script( 'jquery-ui-datepicker' );
}
// Call datepicker functionality in your custom text field
add_action('woocommerce_before_order_notes', 'my_custom_checkout_field', 10, 1);
function my_custom_checkout_field( $checkout ) {
date_default_timezone_set('America/Los_Angeles');
?>
<div id="my_custom_checkout_field">
<h3><?php _e('Check In Date'); ?><abbr class="required" title="required">*</abbr></h3>
<?php
woocommerce_form_field( 'order_in_date', array(
'type' => 'text',
'class' => array('form-row-first'),
'id' => 'from',
'required' => true,
'placeholder' => __('Select Date in'),
),$checkout->get_value( 'order_in_date' ));
woocommerce_form_field( 'order_out_date', array(
'type' => 'text',
'class' => array('form-row-last'),
'id' => 'to',
'required' => true,
'placeholder' => __('Select Date out'),
'clear' => true
),$checkout->get_value( 'order_out_date' ));
?>
</div>
<script>
jQuery(function($){
var dateFormat = "yy/mm/dd",
from = $( "#from" ).datepicker().on( "change", function() {
to.datepicker( "option", "minDate", getDate( this ) );
}),
to = $( "#to" ).datepicker().on( "change", function() {
from.datepicker( "option", "maxDate", getDate( this ) );
});
function getDate( element ) {
var date;
try {
date = $.datepicker.parseDate( dateFormat, element.value );
} catch( error ) {
date = null;
}
return date;
}
});
</script>
<?php
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I have a checkbox which if checked opens a drop down field (requirements). If you try and send the Woocommerce order it conditionally checks if the field has content, if it doesn't it returns an error.
This all works accept when the requirements field does have information input it still treats it as not having content and returns the error.
As this is the basic way that forms should work I don't understand why I'm getting such a result. Here's the code:
/**
* Add a message field to the WC checkout
*/
add_action( 'woocommerce_before_checkout_form', 'custom_checkout_field' );
function custom_checkout_field( $checkout ) {
echo '<div id="message"><h3>' . __( '<i class="fas fa-envelope"></i> Message' ) . '</h3><p style="margin: 0 0 8px;">Would you like to leave a message?</p>';
woocommerce_form_field( 'checkbox', array(
'type' => 'checkbox',
'class' => array( 'msg-checkbox' ),
'label' => __( 'Yes' ),
), $checkout->get_value( 'checkbox' ) );
woocommerce_form_field( 'requirements', array(
'type' => 'text',
'class' => array('msg'),
'label' => __('Please input your message.'),
'placeholder' => __(''),
), $checkout->get_value( 'requirements' ));
echo '</div>';
}
/**
* Process checkout with additional custom field
*/
add_action('woocommerce_checkout_process', 'custom_checkout_field_process');
function custom_checkout_field_process() {
// Check if set, if not add an error.
if(isset($_POST['checkbox']) && ( empty($_POST['requirements'])));
wc_add_notice( __( 'Please let us know what you would like to do' ), 'error' );
}
I hope that makes sense. basically it works to a point but the validation for the input field doesn't work when it has content.
Thanks.
Update: Code improvements and added a show/hide functionality with jQuery
The only problem comes from the hook that you are using for your custom checkout fields. It output those fields outside the checkout form, so the values are never submitted and then always empty.
Instead you will use woocommerce_checkout_before_customer_details action hook for your first hooked function that will output those fields inside the checkout form this time:
// Output custom checkout fields
add_action( 'woocommerce_checkout_before_customer_details', 'custom_checkout_field_before_billing' );
function custom_checkout_field_before_billing() {
$domain = 'woocommerce';
// Hide the message text field on load
?>
<style>p#requirements_field{display:none;}</style>
<div id="message">
<h3><i class="fa fa-envelope"></i><?php _e( 'Message', 'woocommerce' ); ?></h3>
<?php
woocommerce_form_field( 'checkbox_msg', array(
'type' => 'checkbox',
'class' => array( 'msg-checkbox' ),
'label' => __( 'Would you like to leave a message?', 'woocommerce' ),
), WC()->checkout->get_value( 'cb_msg' ));
woocommerce_form_field( 'requirements', array(
'type' => 'text',
'class' => array('msg t_msg'),
'label' => __('Please input your message.'),
'placeholder' => __(''),
), WC()->checkout->get_value( 'requirements' ));
echo '</div>';
// jQuery: show hide the text requirement field
?><script>
jQuery(document).ready(function($) {
var a = '#requirements_field';
$('input#checkbox_msg').change( function(){
if( $(this).is(':checked') )
$(a).show();
else
$(a).hide();
});
});
</script><?php
}
// Process checkout with additional custom field
add_action('woocommerce_after_checkout_validation', 'custom_checkout_field_validation_process', 20, 2 );
function custom_checkout_field_validation_process( $data, $errors ) {
// Check if set, if not add an error.
if( isset($_POST['checkbox_msg']) && empty($_POST['requirements']) )
$errors->add( 'requirements', __( "Please let us know what you would like to do filling up the message field.", "woocommerce" ) );
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.
I'm adding WooCommerce custom checkout fields in a Storefront child theme functions.php file.
They have a "required" attribute.
The aim is to have these fields appear at the top of the page, before the billing fields.
when clicking the submit button to proceed to payment, I'm getting the required custom field validation error ('please fill in your name') and can't continue with payment, even though filling the field with valid data.
Any clue how to fix this or where to start debugging ?
here is the code in functions.php:
add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' );
function my_custom_checkout_fields( $checkout ) {
echo '<div id="my_custom_checkout_field" class="col4-set"><h2>' . __('name') . '</h2>';
woocommerce_form_field( 'developer_name', array(
'type' => 'text',
'class' => array('developer_name-class form-row form-row-first'),
'label' => __('name'),
'placeholder' => __('fill in your name'),
'required' => true,
), $checkout->get_value( 'developer_name' ));
echo '</div>';
}
/**
* Process the checkout
*/
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['developer_name'] )
wc_add_notice( __( 'please fill in your name' ), 'error' );
}
I tried the following but none of them helped:
1. changing:
if ( ! $_POST['developer_name'] )
to
if ( empty( $_POST['developer_name']) )
2. changing the trigger from:
add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' );
to
add_action( 'woocommerce_after_checkout_form', 'my_custom_checkout_fields' );
3. updating to latest Woocomerce 3.0.5 version
I'm running Wordpress 4.7.4
additional related active plugins:
Uni CPO - WooCommerce Options and Price Calculation Formulas
As you can read in woocommerce_before_checkout_form hook, it's before checkout form (so outside the checkout form). For this reason this custom field can't work in this hook.
You can use instead woocommerce_checkout_update_order_meta action hook, making some little changes in your code as there is no $checkout argument available in it.
This will display the field "at the top of the page, before the billing fields"…
So your complete code should be now:
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_checkout_before_customer_details', 'my_custom_checkout_fields' );
function my_custom_checkout_fields() {
echo '<div id="my_custom_checkout_field" class="col4-set"><h2>' . __('name') . '</h2>';
woocommerce_form_field( 'developer_name', array(
'type' => 'text',
'class' => array('developer_name-class form-row form-row-first'),
'label' => __('name'),
'placeholder' => __('fill in your name'),
'required' => true,
), WC()->checkout->get_value( 'developer_name' ));
echo '</div>';
}
/**
* Process the checkout
*/
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['developer_name'] )
wc_add_notice( __( 'Please fill in your name.' ), 'error' );
}
// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['developer_name'] ) ) {
update_post_meta( $order_id, 'Developer name', sanitize_text_field( $_POST['developer_name'] ) );
}
}
// Display the custom-field in orders view
add_action( 'woocommerce_order_details_after_customer_details', 'display_my_custom_field_in_orde_details', 10, 1 );
function display_my_custom_field_in_orde_details( $order ) {
$developer_name = get_post_meta( $order->get_id(), 'Developer name', true );
if ( ! empty( $developer_name ) ):
?>
<table class="woocommerce-table woocommerce-table--customer-details shop_table customer_details">
<tbody><tr>
<th>Developer name:</th>
<td><?php echo $developer_name; ?></td>
</tr></tbody>
</table>
<?php
endif;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for WooCommerce version 3.0+
You try this code. Tested Ok
add_action( 'woocommerce_billing_fields', 'my_custom_checkout_fields' );
function my_custom_checkout_fields( $fields ) {
$fields['billing_developer_name'] = array(
'label' => __('Developer name', 'woocommerce'),
'placeholder' => _x('Developer name', 'placeholder', 'woocommerce'),
'required' => TRUE,
'clear' => false,
'type' => 'text',
'class' => array('my-css')
);
return $fields;
}
You can arrange it with this snippet
add_filter("woocommerce_checkout_fields", "order_fields");
function order_fields($fields) {
$order = array(
"billing_developer_name",
"billing_first_name",
"billing_last_name",
"billing_company",
"billing_address_1",
"billing_address_2",
"billing_postcode",
"billing_country",
"billing_email",
"billing_phone"
);
foreach($order as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
See screenshot
**/* If You Have Created Your Custom Field at the checkout page */**
add_action( 'woocommerce_after_checkout_validation', 'shipping_time_optionss', 9999, 2);
function shipping_time_optionss( $fields, $errors ){
// if any validation errors
if ( empty( $_POST['woo_shipping_time'] ) ) {
$errors->add( 'woocommerce_password_error', __( 'Please Select Shipping Time Option.' ) );
} `enter code here`
}