Added gift message to checkout, order email, and order detail in admin.
authorChuck Scott <cscott@gaslightmedia.com>
Mon, 8 Aug 2016 15:08:40 +0000 (11:08 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Mon, 8 Aug 2016 15:08:40 +0000 (11:08 -0400)
Added code to bottom of functions.php in theme.

functions.php

index 4a8db60..aef0bab 100644 (file)
@@ -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 '<div id="tm_gift_message"><h3>' . __('Gift Message') . '</h3>';
+
+    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 '</div>';
+
+}
+
+// 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 );
+