Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #14896

    url: thefoldlondon.com

    theme: Abundance 1.4

    Hello Kriesi team,

    I am searching for a way to incorporate a “where did you hear about us?” radio form on the checkout page.

    Apart from the different options offered to the user to select, it would be ideal to also include a text box for the user to fill in when none of the offered options are right for him.

    plz see image here > http://screencast.com/t/cVEOOrRYF8

    of course this whole form would not be mandatory in order for the user to place the order.

    I did get in touch with Woo with the same question; the suggested code to be implemented on the functions.php was this:

    /**

    * 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 ) && $args ) ) $after = ‘<div class=”clear”></div>'; else $after = ”;

    $required = ( $args ) ? ‘ <abbr class=”required” title=”‘ . esc_attr__( ‘required’, ‘woocommerce’ ) . ‘”>*</abbr>’ : ”;

    switch ( $args ) {

    case “select” :

    $options = ”;

    if ( ! empty( $args ) )

    foreach ( $args 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 .” . “rn”;

    $field = ‘<p class=”form-row ‘ . implode( ‘ ‘, $args ) .'” id=”‘ . $key . ‘_field”>

    <label for=”‘ . $key . ‘” class=”‘ . implode( ‘ ‘, $args ) .'”>’ . $args. $required . ‘</label>

    ‘ . $options . ‘

    </p>’ . $after;

    break;

    }

    if ( $args ) return $field; else echo $field;

    }

    /**

    * Add the field to the checkout

    **/

    add_action(‘woocommerce_after_order_notes’, ‘hear_about_us_field’);

    function hear_about_us_field( $checkout ) {

    echo ‘<div id=”hear_about_us_field”><h3>’.__(‘How did you hear about us?’).'</h3>';

    woocommerce_form_field_radio( ‘hear_about_us’, array(

    ‘type’ => ‘select’,

    ‘class’ => array(‘here-about-us form-row-wide’),

    ‘label’ => __(”),

    ‘placeholder’ => __(”),

    ‘required’ => true,

    ‘options’ => array(

    ‘Ask Jeeves’ => ‘Ask Jeeves’,

    ‘BBC Website’ => ‘BBC Website’,

    ‘Bing’ => ‘Bing’,

    )

    ), $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)

    $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) update_post_meta( $order_id, ‘How did you hear about us?’, esc_attr($_POST));

    }

    sadly enough the above code did not work, instead it caused a complete site crash, with this error:

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/…/…/wp-content/themes/abundance/functions.php on line 279!

    note: btw there is no line 279 on theme’s function.php file if i am right!

    I could really count on some help with this from the team, you ve been great so far!

    Thank you in advance,

    Best,

    Bill

    #85366

    Hi,

    I tested your code and I didn’t get an error. There were some syntax mistakes. Also you may have used here/hear in various places. Try the code below. Your best bet is to ask whoever wrote this at woothemes to look at it, because I think there are some deeper issues here than just a missing . or missing ” or missing ‘ which is usually the reason for the T_CONSTANT_ENCAPSED_STRING error.

    /**
    * 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 . '' . "rn";
    $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_order_notes', 'hear_about_us_field' );
    function hear_about_us_field( $checkout ) {
    echo '<div id="hear_about_us_field"><h3>' . __( 'How did you hear about us?' ) . '</h3>';
    woocommerce_form_field_radio( 'hear_about_us', array(
    'type' => 'select',
    'class' => array(
    'here-about-us form-row-wide'
    ),
    'label' => __( '' ),
    'placeholder' => __( '' ),
    'required' => true,
    'options' => array(
    'Ask Jeeves' => 'Ask Jeeves',
    'BBC Website' => 'BBC Website',
    'Bing' => 'Bing'
    )
    ), $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' ] ) );
    }

    #85367

    Hey Nick thank you very much for your prompt reply. the code you attached previously worked great. the woo team updated their code with a similar to yours, but my thing still is that I want to add a custom special field so that I can have this > http://screencast.com/t/cVEOOrRYF8

    Please see the code I tried to put together with no real success so far. An update from you on that will be really helpful. Thank you in advance.

    /**
    * Outputs a radio 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 "radio" :

    $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 .'<br />' . "rn";

    $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;

    endforeach;

    endif;

    break;

    }

    if ( $args['return'] ) return $field; else echo $field;
    }

    /**
    * Add the field to the checkout
    **/
    add_action('woocommerce_after_order_notes', 'hear_about_us_field');

    function hear_about_us_field( $checkout )

    add_action('hear_about_us_field', 'my_custom_checkout_field');

    function my_custom_checkout_field( $checkout )

    {

    echo '<div id="hear_about_us_field"><h3>'.__('How did you hear about us?').'</h3>';

    woocommerce_form_field_radio( 'hear_about_us', array(
    'type' => 'radio',
    'class' => array('here-about-us form-row-wide'),
    'label' => __(''),
    'placeholder' => __(''),
    'required' => true,
    'options' => array(
    'Google' => 'Google',
    'Friend Recommendation' => 'Friend Recommendation',
    'Brochure/Lookbook' => 'Brochure/Lookbook',
    'Press' => 'Press',
    'Other' => 'Other',

    )
    ), $checkout->get_value( 'hear_about_us' ));

    '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';

    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-wide'),
    'label' => __('thhh'),
    'placeholder' => __('Enter something'),
    ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';
    }

    /**
    * Process the checkout
    **/
    add_action('woocommerce_checkout_process', 'hear_about_us_field_process');

    function hear_about_us_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.') );
    }

    add_action('hear_about_us_field_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']));
    }

    add_action('hear_about_us_field_update_order_meta', 'my_custom_checkout_field_update_order_meta');

    function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
    }

    #85368

    Hi,

    Add something like this to the code you pasted a, below the line that has … echo ‘</div>';

    echo '<div id="hear_about_us_textarea"><textarea name="order_here_textarea" class="input-text" id="order_here_textarea_id" placeholder="Please tells how you found us" cols="5" rows="2"></textarea></div>';

    Then open up class-wc-order.php, which is located inside the woo plugin, inside classes folder. Since you are basically duplicating the order_comments textarea (order notes), on that page and elsewhere in the plugin just duplicate the lines that concern order_comments and change the name to order_here_textarea.

    And for the radio buttons, you would need to do the same but this time duplicate payment_method, since thats also a radio button, and just rename the copies to hear_aboutus

    Thanks,

    Nick

    #85369

    Thank you very much for your help Nick, I finally found a way to sort it out!

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Add a "where did you hear about us?" radio form on Checkout page (Abundance 1.4)’ is closed to new replies.