Skip to content

Instantly share code, notes, and snippets.

@chrismademe
Created October 9, 2019 08:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismademe/69a4b68123a9196bd2d81acbf3d023b1 to your computer and use it in GitHub Desktop.
Save chrismademe/69a4b68123a9196bd2d81acbf3d023b1 to your computer and use it in GitHub Desktop.
WooCommerce Custom Checkout Fields
<?php
/**
* Add Fields to Order
*
* @param $fields (array) WooCommerce Checkout Fields
*/
add_action( 'woocommerce_checkout_before_customer_details', function( $fields ) {
// Define new Field
woocommerce_form_field( 'delivery_date', array(
'type' => 'text',
'class' => array('nuxley-order-field'),
'label' => __('Selected Delivery or Collection Date'),
'placeholder' => __('Please select a delivery or collection date.'),
'required' => true
) );
} );
/**
* Save Custom Fields
*
* Save the input of custom order fields
*
* @param $order_id (int) WooCommerce Order ID
*/
add_action( 'woocommerce_checkout_update_order_meta', function( $order_id ) {
// Check field is not empty
if ( !empty( $_POST['delivery_date'] ) ) {
update_post_meta( $order_id, 'Delivery Date', sanitize_text_field( $_POST['delivery_date'] ) );
}
} );
/**
* Show custom fields on the Admin Page
*
* @param $order (object) WooCommerce Order Object
*/
add_action( 'woocommerce_admin_order_data_after_shipping_address', function( $order ) {
$value = get_post_meta( $order->get_id(), 'Delivery Date', true );
printf('<p><strong>%s: </strong><br><input type="text" value="%s"></p>', 'Delivery Date', $value);
}, 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment