Skip to content

Instantly share code, notes, and snippets.

@prasadnevase
Last active April 23, 2016 14:53
Show Gist options
  • Save prasadnevase/d7e40c418f737e853489b12556586d50 to your computer and use it in GitHub Desktop.
Save prasadnevase/d7e40c418f737e853489b12556586d50 to your computer and use it in GitHub Desktop.
WooCommerce Custom Checkout Fields with Fields Added in Order Email
<?php
/* Add the field to the checkout */
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
woocommerce_form_field( 'p_trade', array(
'type' => 'select',
'class' => array('p-trade orm-row-wide'),
'label' => __('Trade Details'),
'placeholder' => __('Enter a number'),
'clear' => true,
'options' => array( 'trade-1' => 'Trade One', 'trade-2' => 'Trade Two', 'trade-3' => 'Trade Three', 'trade-4' => 'Trade Four' )
), $checkout->get_value( 'p_trade' ));
woocommerce_form_field( 'p_retail', array(
'type' => 'select',
'class' => array('p-retail orm-row-wide'),
'label' => __('Retail Details'),
'placeholder' => __('Enter a number'),
'clear' => true,
'options' => array('retail-1' => 'Retail One', 'retail-2' => 'Retail Two', 'retail-3' => 'Retail Three', 'retail-4' => 'Retail Four')
), $checkout->get_value( 'p_retail' ));
echo '</div>';
}
/* Update the order meta with field value */
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['p_trade']) update_post_meta( $order_id, 'p_trade', esc_attr($_POST['p_trade']));
if ($_POST['p_retail']) update_post_meta( $order_id, 'p_retail', esc_attr($_POST['p_retail']));
}
/* Add the fields to order email */
add_filter( 'woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys' );
function my_custom_checkout_field_order_meta_keys( $keys ) {
echo '<h3>Payment Option:</h3>';
$keys['Trade'] = 'p_trade';
$keys['Retail'] = 'p_retail';
return $keys;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment