微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何在WordPress / WooCommerce 3中的注释表单中添加自定义字段

Example

我正在尝试在产品评论添加“电话”字段(WooComerce 3).
*对于未注册用户(访客).
电话号码只能由管理员管理面板中查看.

*电话领域需要“必填”.

我试试这段代码,但这不起作用:

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);
    }
*/

//在管理列表表中列出

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

使用wordpress 5.1和WooCommerce 3.5.5测试好

enter image description here

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐