Created
October 9, 2019 08:37
-
-
Save chrismademe/69a4b68123a9196bd2d81acbf3d023b1 to your computer and use it in GitHub Desktop.
WooCommerce Custom Checkout Fields
This file contains 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 | |
/** | |
* 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