我正在收集元密钥数据,并希望在WordPress仪表板中显示它

问题描述 投票:0回答:1

我找到了它,并根据需要对其进行了修改,并将其放在我的functions.php中:

add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
    $new_columns = ( is_array( $columns ) ) ? $columns : array();
    unset( $new_columns[ 'order_actions' ] );

    //edit this for your column(s)
    //all of your columns will be added before the actions column
    $new_columns['dname'] = 'Dogs Name';
    $new_columns['additional_allergies'] = 'Allergies';

    //stop editing
    $new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
    return $new_columns;
}

并且在我的商店结帐中,我收集了这两个元密钥:“ dname”和“ additional_allergies”

wordpress woocommerce hook-woocommerce orders meta-key
1个回答
0
投票

假设使用的meta_keys = dnameadditional_allergies。如果不是这种情况,您将看到“ Meta key is error”消息,那么这就是在数据库中寻找正确的meta_key的问题。

// Add a Header
function custom_shop_order_column( $columns ) {
    // Add new columns
    $columns['dogs_name'] = 'Dogs Name';
    $columns['additional_allergies'] = 'Allergies';

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 10, 1 );

//  Populate the Column
function custom_shop_order_list_column_content( $column, $post_id ) {
    // Compare
    if ( $column == 'dogs_name' ) {
        $dogs_name = get_post_meta( $post_id, 'dname', true);

        if ( $dogs_name ) {
            echo $dogs_name;
        } else {
            echo 'Meta key is wrong';
        }
    }

    if ( $column == 'additional_allergies' ) {
        $allergies = get_post_meta( $post_id, 'additional_allergies', true);

        if ( $allergies ) {
            echo $allergies;
        } else {
            echo 'Meta key is wrong';
        }       
    }   
}
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );
© www.soinside.com 2019 - 2024. All rights reserved.