Display wordpress post_meta into other table

I’ve got a question regarding woocommerce and wordpress itself. Probably my issue is dummy yet current code is not working.

What i wanted to achieve is to display my custom field data in the woocommerce orders subpage. My custom field post_meta has name (metakey): wccpf_authorvalue

Read More

In google i’ve found some code and just change my post meta name into this one:

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

The Issue – column is being displayed, yet without any value. Why is that?

I used solution from here: Stackoverflow.com but it doesnt work.

Related posts

3 comments

  1. Add priority to the filter so that it can work as you want. Also add this code to the functions.php file.

    add_filter('manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION', 11);    
    
    function MY_COLUMNS_FUNCTION($columns) {
    
    }
    
  2. So What you can do is make it to have a span to display there. like the following

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

    And you can update the value using jQuery and Ajax to call a WordPress Action to get metadata.

    Paste the following code in the pastebin link in functions.php of your theme
    http://pastebin.com/LA4zB4TF

    In the js file of your theme, inside the document.ready function add the following jQuery function in pastebin link

    http://pastebin.com/RTYX1Bik

    Hope this works for you.

  3. in fact i’ve figured out how to solve my issue. Thanks for your support in this matter. If somebody in the future will need a help, im sharing my code below (important, ive decided to add 2 columns)

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

Comments are closed.