From 3aae73ab3a405375de6153f84d34ee4c30191693 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Mon, 8 Aug 2016 11:08:40 -0400 Subject: [PATCH] Added gift message to checkout, order email, and order detail in admin. Added code to bottom of functions.php in theme. --- functions.php | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/functions.php b/functions.php index 4a8db60..aef0bab 100644 --- a/functions.php +++ b/functions.php @@ -18,3 +18,61 @@ require get_stylesheet_directory() . '/inc/init.php'; * of this theme is performed. Instead, add your customisations to a plugin such as * https://github.com/woothemes/theme-customisations */ + + +// Add custom fields to checkout + +add_filter( 'woocommerce_after_order_notes' , 'tm_added_checkout_fields' ); + +// Add custom fields to checkout + +add_filter( 'woocommerce_after_order_notes' , 'tm_added_checkout_fields' ); + +function tm_added_checkout_fields( $checkout ) { + + echo '

' . __('Gift Message') . '

'; + + woocommerce_form_field( 'tm_gift_message', array( + 'type' => 'textarea', + 'class' => array('my-field-class form-row-wide'), + 'label' => __(''), + 'placeholder' => __('Enter any desired gift message here.'), + 'maxlength' => 75, + ), $checkout->get_value( 'tm_gift_message' )); + + echo '
'; + +} + +// Get custom field data and store it in post meta data + +add_action( 'woocommerce_checkout_update_order_meta', 'tm_added_checkout_fields_order_meta' ); + +function tm_added_checkout_fields_order_meta( $order_id ) { + + if ( ! empty( $_POST['tm_gift_message'] ) ) { + update_post_meta( $order_id, 'Gift Message', sanitize_text_field( $_POST['tm_gift_message'] ) ); + } + +} + +// Add custom fields to order E-Mail messages +add_filter('woocommerce_email_order_meta_keys', 'tm_added_checkout_fields_email'); +function tm_added_checkout_fields_email( $keys ) { + $keys[] = 'Gift Message'; + return $keys; +} + +// Add custom fields to order printouts +function add_custom_fields_to_order_printout( $fields, $order ) { + $new_fields = array(); + if( get_post_meta( $order->id, 'Gift Message', true ) ) { + $new_fields['Gift Message'] = array( + 'label' => 'Gift Message', + 'value' => get_post_meta( $order->id, 'Gift Message', true ) + ); + } + return array_merge( $fields, $new_fields ); +} +add_filter( 'wcdn_order_info_fields', 'add_custom_fields_to_order_printout', 10, 2 ); + -- 2.17.1