在“代码”中添加排序WooCommerce 优惠券列表中的列

发布于 2025-01-13 08:13:35 字数 1262 浏览 0 评论 0原文

我正在尝试按字母顺序和数字顺序对优惠券代码管理列进行排序。

我有一些优惠券仅由数字组成,其他优惠券仅由字母组成。

我想显示:

  • 按数字顺序显示所有数字优惠券代码,然后
  • 按字母顺序显示所有字母优惠券代码

基于如何制作“优惠券金额”列可在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不…忘初心 2025-01-20 08:13:35

$query->set( 'meta_key', '..' ) 不起作用,因为它与元键无关。相反,您必须使用自定义 SQL 查询,并且可以使用 $query->set( 'post__in', .. )

所以您会得到:

// Make column sortable
function filter_manage_edit_shop_coupon_sortable_columns( $columns ) {
    $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 ) {
    global $pagenow, $post_type, $wpdb;

    // Only for coupons
    if ( $query->is_admin && $pagenow === 'edit.php' && $post_type === 'shop_coupon' && $query->get( 'orderby' ) == 'code' ) {
        // ASC OR DESC
        $asc_desc = $query->get( 'order' );

        // Get all coupon IDs
        $coupon_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'shop_coupon' ORDER BY post_name $asc_desc" );

        // Set
        $query->set( 'post__in', $coupon_ids );
        $query->set( 'orderby', 'post__in' );
    }
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );

相关:如何在 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:

// Make column sortable
function filter_manage_edit_shop_coupon_sortable_columns( $columns ) {
    $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 ) {
    global $pagenow, $post_type, $wpdb;

    // Only for coupons
    if ( $query->is_admin && $pagenow === 'edit.php' && $post_type === 'shop_coupon' && $query->get( 'orderby' ) == 'code' ) {
        // ASC OR DESC
        $asc_desc = $query->get( 'order' );

        // Get all coupon IDs
        $coupon_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'shop_coupon' ORDER BY post_name $asc_desc" );

        // Set
        $query->set( 'post__in', $coupon_ids );
        $query->set( 'orderby', 'post__in' );
    }
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );

Related: How to make the "coupon amount" column sortable in WooCommerce

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文