Home > OS >  Make e-mail field from single product page review form not required
Make e-mail field from single product page review form not required

Time:02-14

I would like to either remove or disable requirement of the woocommerce single product review e-mail field, but keep the name field.

If you uncheck the option "users have to enter name and email to comment" both of fields get removed. Making it easy to leave a review, but are likely not to be trusted by other users since there is no name next to it. If you make e-mail required, users are not likely to leave a review at all.

So far I tried:

  1. Edit single-product-reviews.php by removing the "required" attribute and get_option from the fields:
                        'author' => array(
                            'label'    => __( 'Name', 'woocommerce' ),
                            'type'     => 'text',
                            'value'    => $commenter['comment_author'],
                            'required' => '',
                        ),
                        'email' => array(
                            'label'    => __( 'Email', 'woocommerce' ),
                            'type'     => 'email',
                            'value'    => $commenter['comment_author_email'],
                            'required' => '',
                        ),
                    );
  1. Unset the e-mail field via filter (works but field is still required):
add_filter( 'comment_form_fields', 'remove_email_field', 9 );
function remove_email_field( $fields ){
    unset ($fields['email']);
    return $fields;
}

  1. Make email field not required via filter (not working, breaks review form):
add_filter( 'comment_form_fields', 'email_field_not_required');
function email_field_not_required( $fields ){
    $fields['email']['required'] = false;
    return $fields;
}

The end result should look something like this (no e-mail field but required name):

enter image description here

Can somebody please point me in the right direction?

CodePudding user response:

First, you have to hide email fields from a comment form rather than remove it.

function hide_email_field_on_comment(){

    if( !is_admin() && is_product() ){
        ?>
        <style type="text/css">
            .comment-form-email{
                display: none;
            }
        </style>
        <?php
    }

}
add_action( 'wp_head', 'hide_email_field_on_comment', 10, 1 );

enter image description here

Then you can modify single-product-reviews.php and pass random email to value. so it will not give the required field error for email.

$fields = array(
    'author' => array(
        'label'    => __( 'Name', 'woocommerce' ),
        'type'     => 'text',
        'value'    => $commenter['comment_author'],
        'required' => $name_email_required,
    ),
    'email'  => array(
        'label'    => __( 'Email', 'woocommerce' ),
        'type'     => 'email',
        'value'    => '[email protected]', // pass random email.
        'required' => $name_email_required,
    ),
);

Now you have to remove email from comment when a comment is inserted. for that, you can use the preprocess_comment filter hook.

function remove_dummy_email( $commentdata ){
    unset( $commentdata['comment_author_email'] );
    return $commentdata;
}
add_filter( 'preprocess_comment', 'remove_dummy_email', 10, 1 );

enter image description here

  • Related