如何使 "coupon amount" 列在 WooCommerce 中可排序
How to make the "coupon amount" column sortable in WooCommerce
与以下教程不同:“Columns in WooCommerce”,目的是使 优惠券金额 列可根据金额排序。
使用我已经编写的代码使其可排序似乎是成功的。然而,排序规则出错了。
我之前尝试过几种方法,但点击后排序代码没有应用,
谁愿意仔细看看?
目前我用过的:
add_filter('manage_edit-shop_coupon_sortable_columns', 'misha_sortable');
function misha_sortable( $sortable_columns ){
$sortable_columns['amount'] = 'amount';
return $sortable_columns;
}
add_action( 'pre_get_posts', 'misha_filter' );
function misha_filter( $query ) {
// if it is not admin area, exit the filter immediately
if ( ! is_admin() ) return;
if( empty( $_GET['orderby'] ) || empty( $_GET['order'] ) ) return;
if( $_GET['orderby'] == 'amount' ) {
$query->set('meta_key', 'amount' );
$query->set('orderby', 'meta_value'); // or meta_value_num
$query->set('order', $_GET['order'] );
}
return $query;
}
正确的元键是 coupon_amount
而不是 amount
所以这应该足够了
// Make column sortable
function filter_manage_edit_shop_coupon_sortable_columns( $columns ) {
$columns['amount'] = 'amount';
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' ) {
$query->set( 'meta_key', 'coupon_amount' );
$query->set( 'orderby', 'meta_value_num' );
}
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );
与以下教程不同:“Columns in WooCommerce”,目的是使 优惠券金额 列可根据金额排序。
使用我已经编写的代码使其可排序似乎是成功的。然而,排序规则出错了。
我之前尝试过几种方法,但点击后排序代码没有应用,
谁愿意仔细看看?
目前我用过的:
add_filter('manage_edit-shop_coupon_sortable_columns', 'misha_sortable');
function misha_sortable( $sortable_columns ){
$sortable_columns['amount'] = 'amount';
return $sortable_columns;
}
add_action( 'pre_get_posts', 'misha_filter' );
function misha_filter( $query ) {
// if it is not admin area, exit the filter immediately
if ( ! is_admin() ) return;
if( empty( $_GET['orderby'] ) || empty( $_GET['order'] ) ) return;
if( $_GET['orderby'] == 'amount' ) {
$query->set('meta_key', 'amount' );
$query->set('orderby', 'meta_value'); // or meta_value_num
$query->set('order', $_GET['order'] );
}
return $query;
}
正确的元键是 coupon_amount
而不是 amount
所以这应该足够了
// Make column sortable
function filter_manage_edit_shop_coupon_sortable_columns( $columns ) {
$columns['amount'] = 'amount';
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' ) {
$query->set( 'meta_key', 'coupon_amount' );
$query->set( 'orderby', 'meta_value_num' );
}
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );