简体   繁体   English

如何在 WordPress/WooCommerce 3+ 中向评论表单添加自定义字段

[英]How to add custom fields to comments form in WordPress/WooCommerce 3+

例子

I'm trying to add a "Phone" field in the product comments (WooComerce 3+).我正在尝试在产品评论中添加一个“电话”字段(WooComerce 3+)。 *For unregistered users too (guests). *对于未注册的用户(客人)。 Phone number should only be seen by the administrator in the admin panel.电话号码只能由管理员在管理面板中看到。

*Phone field need make " Required ". *电话字段需要设为“必填”。

I tried this code, but this not work:我试过这段代码,但这不起作用:

function true_phone_number_field( $fields ) {
$fields['phone'] = '<p class="comment-form-phone"><label for="phone">Phone</label> <input id="phone" name="phone" type="text" value="" size="30" /></p>';
}
add_filter( 'comment_form_default_fields', 'true_phone_number_field');
    // Add phone number field

    function add_review_phone_field_on_comment_form() {
        echo '<p class="comment-form-phone uk-margin-top"><label for="phone">' . __( 'Phone', 'text-domain' ) . '</label><span class="required">*</span><input class="uk-input uk-width-large uk-display-block" type="text" name="phone" id="phone"/></p>';
    }
    add_action( 'comment_form_logged_in_after', 'add_review_phone_field_on_comment_form' );
    add_action( 'comment_form_after_fields', 'add_review_phone_field_on_comment_form' );


    // Save phone number
    add_action( 'comment_post', 'save_comment_review_phone_field' );
    function save_comment_review_phone_field( $comment_id ){
        if( isset( $_POST['phone'] ) )
          update_comment_meta( $comment_id, 'phone', esc_attr( $_POST['phone'] ) );
    }

    function print_review_phone( $id ) {
        $val = get_comment_meta( $id, "phone", true );
        $title = $val ? '<strong class="review-phone">' . $val . '</strong>' : '';
        return $title;
    }

    // Print phone number - remove if not needed to show in front end
/*
    add_action('woocommerce_review_before_comment_meta', 'get_comment_phone' );
    function get_comment_phone($comment){
        echo print_review_phone($comment->comment_ID);
    }
*/

// List in admin list table // 管理员列表中的列表

add_filter('manage_edit-comments_columns', 'my_add_comments_columns');

function my_add_comments_columns($my_cols) {

    $temp_columns = array(
        'phone' => 'Phone'
    );
    $my_cols = array_slice($my_cols, 0, 3, true) + $temp_columns + array_slice($my_cols, 3, NULL, true);

    return $my_cols;
}

add_action('manage_comments_custom_column', 'my_add_comment_columns_content', 10, 2);

function my_add_comment_columns_content($column, $comment_ID) {
    global $comment;
    switch ($column) :

        case 'phone' : {

                echo get_comment_meta($comment_ID, 'phone', true);
                break;
            }
    endswitch;
}

Tested OK with WordPress 5.1 and WooCommerce 3.5.5使用WordPress 5.1WooCommerce 3.5.5测试正常

在此处输入图片说明

在此处输入图片说明

Your code should produce an input field, but it might appear not to, because the comment_form_default_fields filter you use is meant for default comment fields, which are hidden if you are logged in. The phone field should appear after you log out and look at the product comments.您的代码应该会生成一个输入字段,但它可能看起来不会,因为您使用的comment_form_default_fields过滤器用于默认评论字段,如果您登录,这些字段就会隐藏。电话字段应该在您注销后出现并查看产品评论。

In addition, you did not provide any logic for saving the value of the input field to the database.此外,您没有提供任何将输入字段的值保存到数据库的逻辑。 I think this article can be helpful if you want to implement this yourself.如果您想自己实现这一点,我认为这篇文章可能会有所帮助。

However, as you did tag your question with advanced-custom-fields , you might want to skip coding and let the Advanced Custom Fields plugin handle adding the input field and saving the phone number to the database.但是,由于您确实使用advanced-custom-fields标记了您的问题,您可能希望跳过编码并让Advanced Custom Fields 插件处理添加输入字段并将电话号码保存到数据库中。 To do so, simply download and activate the plugin, go to the Custom Fields menu, add a new field group, and create the phone input field.为此,只需下载并激活插件,转到自定义字段菜单,添加新字段组,然后创建电话输入字段。 Make sure to take a look at the Location meta box and create a rule for showing the field group only if Comment is equal to Product :确保查看 Location 元框并创建一个规则,仅当Comment is equal to Product时才显示字段组:

ACF 位置元框

This will automatically add the fields in your field group to the comment fields of products.这将自动将您的字段组中的字段添加到产品的评论字段中。

You have to return the variable named 'fields' at the end of the function.您必须在函数末尾返回名为“fields”的变量。

function true_phone_number_field( $fields ) {
$fields['phone'] = '<p class="comment-form-phone"><label for="phone">Phone</label> <input id="phone" name="phone" type="text" value="" size="30" /></p>';

 return $fields;
}
add_filter( 'comment_form_default_fields', 'true_phone_number_field');

i tried mujuonly's solution and it works fine.我尝试了 mujuonly 的解决方案,效果很好。 what i need is to replace phone by a age checkbox where user will selct his age, like this我需要的是用年龄复选框替换手机,用户将在其中选择他的年龄,就像这样

AGE :年龄 :
15-20 years 15-20岁
20-40 years 20-40岁
more than 40 years超过 40 年

any help !任何帮助! thx谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM