Forked from mchrislay/select-text-woo-checkoutt.php
Created
October 26, 2018 17:26
-
-
Save fitzmode/e7578a17e85295ac4393964db75cb5e6 to your computer and use it in GitHub Desktop.
Add Select Radio Buttons + Text Field option to WooCommerce Checkout Page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ] ) ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment