From 41078f1b226c9a06187ce9c2cfdbe1b8e48d782d Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Tue, 2 Jun 2015 16:44:49 -0400 Subject: [PATCH] Added custom fields to checkout --- functions.php | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/functions.php b/functions.php index bf97df3..b828436 100755 --- a/functions.php +++ b/functions.php @@ -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 '

' . __('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.'), + ), $checkout->get_value( 'tm_gift_message' )); + + echo '
'; + + echo '

' . __('FedEx Shipping') . '

'; + + 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 '
'; + +} + +// 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 '

'.__('Gift Message').':
' . get_post_meta( $order->id, 'Gift Message', true ) . '

'; + echo '

'.__('FedEx Shipping').':
' . get_post_meta( $order->id, 'FedEx Shipping', true ) . '

'; +} + + +?> -- 2.17.1