将 wordpress post_meta 显示到其他 table

Display wordpress post_meta into other table

我有一个关于 woocommerce 和 wordpress 本身的问题。可能我的问题是虚拟的,但当前代码无法正常工作。

我想要实现的是在 woocommerce 订单子页面中显示我的自定义字段数据。我的自定义字段 post_meta 的名称 (metakey):wccpf_authorvalue

在 google 中,我找到了一些代码并将我的 post 元名称更改为这个:

   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 you column(s)
    //all of your columns will be added before the actions column
    $new_columns['for-author-value'] = 'Dla autora';
    //stop editing

    $new_columns['order_actions'] = $columns['order_actions'];
    return $new_columns;
}


add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION($column){
    global $post;
    $data = get_post_meta( $post->ID );

    //start editing, I was saving my fields for the orders as custom post meta
    //if you did the same, follow this code
    if ( $column == 'for-author-value' ) {    
        echo (isset($data['wccpf_authorvalue']) ? $data['wccpf_authorvalue'] : '');
    }

}


add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
function MY_COLUMNS_SORT_FUNCTION( $columns ) {
    $custom = array(
        //start editing

        'for-author-value'    => 'wccpf_authorvalue'

        //stop editing
    );
    return wp_parse_args( $custom, $columns );
}

正在显示问题 - 列,但没有任何值。这是为什么?

我使用了这里的解决方案:whosebug.com 但它不起作用。

为过滤器添加优先级,以便它可以按您的需要工作。还将此代码添加到 functions.php 文件。

add_filter('manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION', 11);    

function MY_COLUMNS_FUNCTION($columns) {

}

所以你可以做的是让它有一个显示在那里的跨度。像下面这样

<span id="metawrap"></span>

并且您可以使用 jQuery 和 Ajax 更新值以调用 Wordpress 操作以获取元数据。

将以下代码粘贴到您主题的 functions.php 中的 pastebin link http://pastebin.com/LA4zB4TF

在你的主题的 js 文件中,在 document.ready 函数中添加以下 jQuery 函数 in pastebin link

http://pastebin.com/RTYX1Bik

希望这对你有用。

事实上,我已经找到解决问题的方法了。感谢您对此事的支持。如果将来有人需要帮助,我会在下面分享我的代码(重要,我决定添加 2 列)

 add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION', 10 );
function MY_COLUMNS_FUNCTION( $columns ) {
    $new_columns = ( is_array( $columns ) ) ? $columns : array();
    unset( $new_columns['order_actions'] );
    //edit this for you column(s)
    //all of your columns will be added before the actions column
    $new_columns['product_name'] = 'Product';
    $new_columns['authors_income'] = 'Author';

    $new_columns['order_actions'] = $columns['order_actions'];

    return $new_columns;
}

add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION( $column ) {
    global $post;
    $order = new WC_Order( $post->ID );
    $items = $order->get_items();

    //start editing, I was saving my fields for the orders as custom post meta
    //if you did the same, follow this code
    if ( $column == 'authors_income' ) {
        foreach ( $items as $item ) {

            echo $item['your field meta key'];
            echo '.00USD';

        }
    }

    if ( $column == 'product_name' ) {
        foreach ( $items as $item ) {

            echo $item['your field meta key'];

        }
    }

}


add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
function MY_COLUMNS_SORT_FUNCTION( $columns ) {
    $custom = array(
        //start editing

        'authors_income'    => 'your field meta key',
        'product_name'    => 'your field meta key'

        //stop editing
    );
    return wp_parse_args( $custom, $columns );
}