Added custom fields to checkout
authorChuck Scott <cscott@gaslightmedia.com>
Tue, 2 Jun 2015 20:44:49 +0000 (16:44 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Tue, 2 Jun 2015 20:44:49 +0000 (16:44 -0400)
functions.php

index bf97df3..b828436 100755 (executable)
@@ -128,4 +128,65 @@ add_filter( 'woocommerce_enqueue_styles', '__return_false' );
 
 add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 48;' ), 20 );
 
-?>
\ No newline at end of file
+
+/**
+ * WooCommerce Checout Field Additions
+ */
+
+// 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.'),
+        ), $checkout->get_value( 'tm_gift_message' ));
+    echo '</div>';
+
+    echo '<div id="tm_fedex_shipping"><h3>' . __('FedEx Shipping') . '</h3>';
+    woocommerce_form_field( 'tm_fedex_shipping', array(
+        'type'          => 'textarea',
+        'class'         => array('my-field-class form-row-wide'),
+        'label'         => __(''),
+        'placeholder'   => __('If you wish to charge shipping to your FedEx account, please provide the information here.'),
+        ), $checkout->get_value( 'tm_fedex_shipping' ));
+    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'] ) );
+    }
+
+    if ( ! empty( $_POST['tm_fedex_shipping'] ) ) {
+        update_post_meta( $order_id, 'FedEx Shipping', sanitize_text_field( $_POST['tm_fedex_shipping'] ) );
+    }
+
+}
+
+// Display custom fields in admin order data
+
+add_action( 'woocommerce_admin_order_data_after_billing_address', 'tm_added_checkout_fields_admin_order_meta', 10, 1 );
+
+function tm_added_checkout_fields_admin_order_meta($order){
+    echo '<p><strong>'.__('Gift Message').':</strong><br> ' . get_post_meta( $order->id, 'Gift Message', true ) . '</p>';
+    echo '<p><strong>'.__('FedEx Shipping').':</strong><br> ' . get_post_meta( $order->id, 'FedEx Shipping', true ) . '</p>';
+}
+
+
+?>