[英]How to make the “coupon amount” column sortable in WooCommerce
Departing from the following tutorial: " Columns in WooCommerce ", the intention is to make the coupon amount column sortable based on amount.从以下教程出发:“ WooCommerce 中的列”,目的是使优惠券金额列可根据金额排序。
Making it sortable seems to be successful with my already written code.使用我已经编写的代码使其可排序似乎是成功的。 Nevertheless, the rule to sort goes wrong.
然而,排序规则出错了。
I've tried several things before but the sort code is not applied after clicking,我之前尝试过几件事,但点击后没有应用排序代码,
Who would like to take a closer look?谁愿意仔细看看?
What I've used so far:到目前为止我用过的:
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;
}
The correct metakey is coupon_amount
instead of amount
正确的元键是
coupon_amount
而不是amount
So this should suffice所以这应该足够了
// 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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.