在“代码”中添加排序WooCommerce 优惠券列表中的列
我正在尝试按字母顺序和数字顺序对优惠券代码管理列进行排序。
我有一些优惠券仅由数字组成,其他优惠券仅由字母组成。
我想显示:
- 按数字顺序显示所有数字优惠券代码,然后
- 按字母顺序显示所有字母优惠券代码
基于如何制作“优惠券金额”列可在 WooCommerce 应答代码中排序,这是我的尝试:
function filter_manage_edit_shop_coupon_sortable_columns( $columns ) {
// $columns['amount'] = 'amount';
$columns['coupon_code'] = 'code';
return $columns;
}
add_filter( 'manage_edit-shop_coupon_sortable_columns', 'filter_manage_edit_shop_coupon_sortable_columns', 10, 1 );
// Fires after the query variable object is created, but before the actual query is run.
function action_pre_get_posts( $query ) {
// If it is not admin area, exit the filter immediately
if( ! is_admin() ) return;
// Get orderby
$orderby = $query->get( 'orderby' );
// Set query
// if( $orderby == 'amount' ) {
if( $orderby == 'coupon' ) {
// $query->set( 'meta_key', 'coupon_amount' );
$query->set( 'meta_key', 'coupon_code' );
$query->set( 'orderby', 'meta_value_num' );
}
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );
虽然优惠券列可排序,但实际排序未按预期进行。有什么建议吗?
I am trying to alphabetically and numerically sort the coupon code admin column.
I have some coupons that is made up with only numbers and others that is only letters.
I would like to display:
- All the numbers coupon codes in numerical order and then
- All the letter coupon codes in alphabetical order
Based on How to make the "coupon amount" column sortable in WooCommerce answer code, this is my attempt:
function filter_manage_edit_shop_coupon_sortable_columns( $columns ) {
// $columns['amount'] = 'amount';
$columns['coupon_code'] = 'code';
return $columns;
}
add_filter( 'manage_edit-shop_coupon_sortable_columns', 'filter_manage_edit_shop_coupon_sortable_columns', 10, 1 );
// Fires after the query variable object is created, but before the actual query is run.
function action_pre_get_posts( $query ) {
// If it is not admin area, exit the filter immediately
if( ! is_admin() ) return;
// Get orderby
$orderby = $query->get( 'orderby' );
// Set query
// if( $orderby == 'amount' ) {
if( $orderby == 'coupon' ) {
// $query->set( 'meta_key', 'coupon_amount' );
$query->set( 'meta_key', 'coupon_code' );
$query->set( 'orderby', 'meta_value_num' );
}
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );
While the coupon column is sortable, the actual sorting is not working as expected. Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$query->set( 'meta_key', '..' )
不起作用,因为它与元键无关。相反,您必须使用自定义 SQL 查询,并且可以使用$query->set( 'post__in', .. )
所以您会得到:
相关:如何在 WooCommerce 中使“优惠券金额”列可排序
$query->set( 'meta_key', '..' )
won't work because it's not about a metakey. Instead you have to use a custom SQL query and you can use$query->set( 'post__in', .. )
So you get:
Related: How to make the "coupon amount" column sortable in WooCommerce