我有一个关于wooccommerce和wordpress本身的问题。可能我的问题是伪的,但当前的代码不起作用。
我想要实现的是在wooccommerce订单子页面中显示我的自定义字段数据。我的自定义字段post_meta具有名称(元密钥):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 );
}
正在显示Issue-列,但没有任何值。为什么?
我使用了这里的解决方案:Stackoverflow.com,但它不起作用。
为过滤器添加优先级,使其可以根据需要工作。同时将此代码添加到functions.php
文件中。
add_filter('manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION', 11);
function MY_COLUMNS_FUNCTION($columns) {
}
所以你能做的就是让它在那里显示一个span。像下面的
<span id="metawrap"></span>
您可以使用jQuery和Ajax来更新值,从而调用Wordpress Action来获取元数据。
将以下代码粘贴到主题的functions.php中的pastebin链接中http://pastebin.com/LA4zB4TF
在主题的js文件中,在document.ready函数中,在pastebin链接中添加以下jQuery函数
http://pastebin.com/RTYX1Bik
希望这对你有用。
事实上,我已经找到了解决问题的方法。谢谢你在这件事上的支持。如果将来有人需要帮助,我在下面分享我的代码(重要的是,我决定添加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 );
}