Add a sortable custom column in Woocommerce Admin Orders list(在 Woocommerce Admin Orders 列表中添加可排序的自定义列)
问题描述
我在 WooCommerce 的订单"部分添加了一个自定义列,用于运输邮政编码.列及其值正确显示.
I've added a custom column to the "Orders" section of WooCommerce for the shipping zip code. The column and its values appear correctly.
我无法弄清楚如何使该字段的排序起作用(单击列标题).我可以找到使用钩子manage_edit-shop_order_sortable_columns"提到的其他代码示例,但这似乎不适用于该领域.
What I cannot figure out is how to make the sorting of this field work (clicking on the column header). Other code examples I could find mention using the hook "manage_edit-shop_order_sortable_columns", but this doesn't seem to be working for this field.
注意:我已经看到了关于此的其他 StackOverflow 问题,但似乎没有一个可以进行排序.
Note: I've seen the other StackOverflow issues about this, but none seem to have sorting working.
/**
* ADD ZIP CODE TO WOOCOMMERCE ORDERS LIST
*/
// Add column (working)
add_filter( 'manage_edit-shop_order_columns', 'custom_woo_columns_function' );
function custom_woo_columns_function( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns[ 'order_actions' ] );
// all of your columns will be added before the actions column
$new_columns['zipcode'] = 'Zip Code';
//stop editing
$new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
return $new_columns;
}
// Change order of columns (working)
add_action( 'manage_shop_order_posts_custom_column', 'custom_woo_admin_value', 2 );
function custom_woo_admin_value( $column ) {
global $post;
$zip_value = get_post_meta($post->ID, '_shipping_postcode', true);
if ( $column == 'zipcode' ) {
echo ( isset( $zip_value ) ? $zip_value : '' );
}
}
// Sort by custom column (NOT WORKING)
add_filter( "manage_edit-shop_order_sortable_columns", 'custom_woo_admin_sort' );
function custom_woo_admin_sort( $columns )
{
$custom = array(
'zipcode' => '_shipping_postcode',
);
return wp_parse_args( $custom, $columns );
}
推荐答案
我想通了.将把它留在这里供其他任何尝试添加排序邮政编码列的人使用.只需添加此附加操作.
I figured it out. Will leave this up here for anyone else trying to add a sorted zip code column. Just add this additional action.
// Make sorting by custom column work properly
add_action('pre_get_posts', 'custom_zipcode_orderby');
function custom_zipcode_orderby($query)
{
if (!is_admin()) return;
$orderby = $query->get('orderby');
if ('_shipping_postcode' == $orderby) {
$query->set('meta_key', '_shipping_postcode');
$query->set('orderby', 'meta_value_num');
}
}
这篇关于在 Woocommerce Admin Orders 列表中添加可排序的自定义列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Woocommerce Admin Orders 列表中添加可排序的自定义
基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01