【发布时间】: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