Skip to content

Instantly share code, notes, and snippets.

@mchrislay
Created January 24, 2013 02:05
Show Gist options
  • Save mchrislay/4617040 to your computer and use it in GitHub Desktop.
Save mchrislay/4617040 to your computer and use it in GitHub Desktop.
Add Select Radio Buttons + Text Field option to WooCommerce Checkout Page
<?php
/**
* Outputs a rasio button form field
*/
function woocommerce_form_field_radio( $key, $args, $value = '' ) {
global $woocommerce;
$defaults = array(
'type' => 'radio',
'label' => '',
'placeholder' => '',
'required' => false,
'class' => array( ),
'label_class' => array( ),
'return' => false,
'options' => array( )
);
$args = wp_parse_args( $args, $defaults );
if ( ( isset( $args[ 'clear' ] ) && $args[ 'clear' ] ) )
$after = '<div class="clear"></div>';
else
$after = '';
$required = ( $args[ 'required' ] ) ? ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>' : '';
switch ( $args[ 'type' ] ) {
case "select":
$options = '';
if ( !empty( $args[ 'options' ] ) )
foreach ( $args[ 'options' ] as $option_key => $option_text )
$options .= '<input type="radio" name="' . $key . '" id="' . $key . '" value="' . $option_key . '" ' . selected( $value, $option_key, false ) . 'class="select">' . $option_text . '' . "\r\n";
$field = '<p class="form-row ' . implode( ' ', $args[ 'class' ] ) . '" id="' . $key . '_field">
<label for="' . $key . '" class="' . implode( ' ', $args[ 'label_class' ] ) . '">' . $args[ 'label' ] . $required . '</label>
' . $options . '
</p>' . $after;
break;
} //$args[ 'type' ]
if ( $args[ 'return' ] )
return $field;
else
echo $field;
}
/**
* Add the field to the checkout
**/
add_action( 'woocommerce_after_checkout_billing_form', 'hear_about_us_field', 10 );
function hear_about_us_field( $checkout ) {
echo '<div id="hear_about_us_field" style="background: lightgoldenrodyellow;"><h3>' . __( 'How\'d you First find us?' ) . '</h3>';
woocommerce_form_field_radio( 'hear_about_us', array(
'type' => 'select',
'class' => array(
'here-about-us form-row-wide'
),
'label' => __( '' ),
'placeholder' => __( '' ),
'required' => false,
'options' => array(
'Google' => 'Google<br/>',
'Other Search' => 'Other Search Engine<br/>',
'Friend' => 'Friend<br/>',
'Friend/Facebook' => 'Facebook/Friend<br/>',
'Cristal' => 'CristalProStyler on YoutTube',
'Other' => 'Other<br/>'
)
), $checkout->get_value( 'hear_about_us' ) );
echo '</div>';
}
/**
* Process the checkout
**/
add_action( 'woocommerce_checkout_process', 'my_custom_checkout_field_process' );
function my_custom_checkout_field_process( ) {
global $woocommerce;
// Check if set, if its not set add an error.
if ( !$_POST[ 'hear_about_us' ] )
$woocommerce->add_error( __( 'Please enter something into this new shiny field.' ) );
}
/**
* Update the order meta with field value
**/
add_action( 'woocommerce_checkout_update_order_meta', 'hear_about_us_field_update_order_meta' );
function hear_about_us_field_update_order_meta( $order_id ) {
if ( $_POST[ 'hear_about_us' ] )
update_post_meta( $order_id, 'How did you hear about us?', esc_attr( $_POST[ 'hear_about_us' ] ) );
}
?>
@mchrislay
Copy link
Author

So the code about displays the Select buttons, but now I'm trying to add a text field just under the Select options, as a way for the customer to chose "Other" and then type in where they found the website with a little more detail

@josece
Copy link

josece commented Sep 27, 2013

Thank you for posting this!

@davidrhoden
Copy link

Thanks, this was useful! My next task is to show the radio button field only when certain products are in the checkout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment