Home > database >  Add custom field in Wordpress and WooCommerce and show on admin panel
Add custom field in Wordpress and WooCommerce and show on admin panel

Time:02-06

I want to add a phone number field in both Posts and Woocommerce. But I want just to show the phone number in the admin panel, not the front end. I used this link from @mujuonly to do so. Everything is Ok. I mean the phone number field is added to the comment form in posts and WooCommerce. The phone number is shown in the comment list of posts in the admin panel, The only problem is that the phone number is not shown in the WooCommerce comment list in the backend. Can anyone help me? This is the code

// Add phone number field

    function add_review_phone_field_on_comment_form() {
        echo '<p ><label for="phone">' . __( 'Phone', 'text-domain' ) . '</label><span >*</span><input  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 >' . $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;
}

CodePudding user response:

add_filter('woocommerce_product_reviews_table_columns', 'woocommerce_product_reviews_table_columns');

function woocommerce_product_reviews_table_columns($my_cols) {

    $my_cols['phone'] = __('Phone', 'woocommerce');

    return $my_cols;
}

add_filter('woocommerce_product_reviews_table_column_phone_content', 'woocommerce_product_reviews_table_column_phone_content', 10, 2);

function woocommerce_product_reviews_table_column_phone_content($output, $item) {

    $phone = get_comment_meta($item->comment_ID, 'phone', true);

    return $phone;
}

Please try these two hooks for the WooCommerce reviews table additional columns

  • Related