【问题标题】:Display wordpress post_meta into other table将 wordpress 帖子元显示到另一个表中
【发布时间】:2015-12-26 10:06:08
【问题描述】:

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

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

在谷歌中我找到了一些代码,只需将我的帖子元名称更改为这个:

   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,但它不起作用。

【问题讨论】:

  • 您给定的代码对我来说工作正常。您是否检查过 wccpf_authorvalue 是否已保存在数据库中,或者是否存在带有该 key 的元数据?
  • @Rohil_PHPBeginner - 在数据库中 > _postmeta 表我的记录带有元值,例如 ({"type":"text","label":"PLN dla autora","name":" pl ..). 但是也有表 woocommerce_order_itemmeta ,其中 -> 每个订单都有我输入的专用记录(元值是我需要的数据)。但我不知道如何显示它,特别是如果每​​个订单都有有自己的专线。
  • $data = get_post_meta( $post->ID ); 替换为$data = get_post_meta( $post->ID, 'wccpf_authorvalue', true );echo $data;。可以给我替换后的结果吗?
  • 嘿,还是什么都没有。空白值。

标签: php wordpress woocommerce meta


【解决方案1】:

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

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

function MY_COLUMNS_FUNCTION($columns) {

}

【讨论】:

  • 感谢 Gaurav,很遗憾这个改动不起作用
  • 您是否正在获取 MY_COLUMNS_VALUES_FUNCTION 的密钥。更新以下代码。如果发现任何问题,请告诉我。 code add_action('manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION',10, 2);功能 MY_COLUMNS_VALUES_FUNCTION($column){ 全球 $post; $data = get_post_meta($post->ID); //开始编辑,我将订单字段保存为自定义帖子元 //如果您也这样做,请遵循此代码 if ( $column == 'for-author-value' ) { echo (isset($data[ 'wccpf_authorvalue']) ? $data['wccpf_authorvalue'] : ''); } } code
  • 你应该 $data = get_post_meta( $post->ID ); print_r($data);
  • 建议的更改没有帮助 - 它已将 ORDERS 表中的所有数据更改为以下值: Array ( [_order_key] => Array ( [0] => wc_order_567e99cba6711 )
【解决方案2】:

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

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

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

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

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

http://pastebin.com/RTYX1Bik

希望这对你有用。

【讨论】:

  • 我可以编写如下代码: echo "Hello world!
    ";它会显示出来。但从特定的 postmeta dosnt 工作中获取值:(
  • 此代码不起作用。新的列值仍然是空白
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多