将wordpress post_meta显示到其他表

时间:2015-12-26 10:06:08

标签: php wordpress woocommerce meta

我对woocommerce和wordpress本身有疑问。可能我的问题是假的,但目前的代码不起作用。

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

在google中,我发现了一些代码,只是将我的帖子元名称更改为这个:

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

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

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

2 个答案:

答案 0 :(得分:0)

所以你可以做的是让它有一个展示在那里的跨度。如下

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

您可以使用jQuery和Ajax更新值,以调用Wordpress Action来获取元数据。

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

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

http://pastebin.com/RTYX1Bik

希望这适合你。

答案 1 :(得分:-1)

事实上,我已经找到了解决问题的方法。感谢您对此事的支持。如果将来某人需要帮助,我将在下面分享我的代码(重要的是,我决定添加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 );
}