按SKU数值(1.2.3.4.5.6 ........)对WooCommerce产品进行排序 [英] Sort WooCommerce Products by SKU Numeric (1.2.3.4.5.6........)

查看:67
本文介绍了按SKU数值(1.2.3.4.5.6 ........)对WooCommerce产品进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在管理产品列表上的WooCommerce版本4.2.2中,当我尝试按SKU(列)对产品进行排序时,结果是:1 10 100 101 102 103 104 ...
相反,我希望它具有自然的顺序,例如:1 2 3 4 5 ... 9 10 11 ... 99 ... 100101 ...

In WooCommerce Version 4.2.2 on admin products list, when I try to sort products by SKU (column), the result is: 1 10 100 101 102 103 104 ...
InsteadI would like it to be in natural order like: 1 2 3 4 5 ... 9 10 11 ... 99 ... 100 101 ...

当我使用WooCommerce产品搜索(2.21.0版)时,也会发生同样的事情.

The same thing happens when I use WooCommerce Product Search (Version 2.21.0).

试图了解正在发生的事情,我注意到,如果我在管理面板中搜索产品,它会返回太多结果,并且并非所有产品都发生这种情况,有些工作可以完成,并且只带给我1件产品,而另一些却带来了更多.

Trying to understand what is happening I noticed that if I do a search in the admin panel for products it returns too many results and this does not happen to all products, some work and it only brings me 1 single product, others bring more.

我尝试重新安装所有Wordpress,WooCommerce和WooCommerce产品搜索,而没有进行任何更改.我测试了其他具有相同结果的搜索插件.

I tried to reinstall all Wordpress, WooCommerce and WooCommerce product search, without any change. I tested other search plugins with the same results.

请帮忙吗?

推荐答案

当时StackOverFlow中的规则是一个问题……所以我要回答管理产品列表.

The rule in StackOverFlow is one question at the time… So I am answering for admin products list.

以下挂钩函数将允许使用自然排序顺序按数字skus对产品进行排序,从而使用ORDER BY .com/sql/func_sqlserver_abs.asp"rel =" nofollow noreferrer> ABS() :

The following hooked function will allow to sort products by numerical skus in a natural sorting order altering the ORDER BY in the related SQL query using ABS():

add_filter( 'posts_clauses', 'admin_products_list_orderby_sku_as_int', 999, 1 );
function admin_products_list_orderby_sku_as_int( $posts_clauses ){
    global $pagenow, $typenow, $wp_query;

    $query_vars = $wp_query->query_vars;

    if ( $pagenow == 'edit.php' && 'product' === $typenow && isset( $_GET['post_type'] )
    && isset( $query_vars['orderby'] ) && 'sku' === $query_vars['orderby'] ) {
        $orderby   = strtolower( $query_vars['orderby'] );
        $order     = isset( $query_vars['order'] ) ? strtoupper( $query_vars['order'] ) : 'DESC';

        if ( 'DESC' === $order ) {
            $posts_clauses['orderby'] = ' ABS( wc_product_meta_lookup.sku ) DESC, wc_product_meta_lookup.sku DESC, wc_product_meta_lookup.product_id DESC ';
        } else {
            $posts_clauses['orderby'] = ' ABS( wc_product_meta_lookup.sku ) ASC, wc_product_meta_lookup.sku ASC, wc_product_meta_lookup.product_id ASC ';
        }
    }
    return $posts_clauses;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

请参阅:以数字方式表示SQL ORDER字符

这篇关于按SKU数值(1.2.3.4.5.6 ........)对WooCommerce产品进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆