将wordpress post_meta显示到其他表中

时间:2022-10-25 10:57:11

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

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

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

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

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: *.com but it doesnt work.

我从这里使用解决方案:*.com但它不起作用。

3 个解决方案

#1


2  

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

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

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

function MY_COLUMNS_FUNCTION($columns) {

}

#2


0  

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.

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

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

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

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

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

http://pastebin.com/RTYX1Bik

http://pastebin.com/RTYX1Bik

Hope this works for you.

希望这对你有用。

#3


-1  

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)

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

#1


2  

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

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

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

function MY_COLUMNS_FUNCTION($columns) {

}

#2


0  

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.

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

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

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

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

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

http://pastebin.com/RTYX1Bik

http://pastebin.com/RTYX1Bik

Hope this works for you.

希望这对你有用。

#3


-1  

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)

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