所以,我做了一些挖掘,但找不到正确的方法来做到这一点。
我在WooCommerce(Wordpress(上有一些产品,但它们的标题由字母和数字混合组成。 我需要像这样打印这些产品(例如(:
FNC21000-C FNC24500-C FNC232500-C
但是,当然,Wordpress给了我:
FNC21000-C FNC232500-C FNC24500-C
有没有办法做到这一点?
我想我可能会在WP_Query上做一些事情,但我真的是 PHP 的新手,我知道一些可能的修复是在 PHP 上,其他一些是在 MySQL 上......标题既是字符串又是整数的事实,让我对如何写这个感到一无所知......
有人有什么建议吗?
下面是class-wc-query.php,我相信这是我需要编辑的文件:
/**
* Returns an array of arguments for ordering products based on the selected values.
*
* @access public
* @return array
*/
public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
// Get ordering from query string unless defined
if ( ! $orderby ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
// Get order + orderby args from string
$orderby_value = explode( '-', $orderby_value );
$orderby = esc_attr( $orderby_value[0] );
$order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
}
$orderby = strtolower( $orderby );
$order = strtoupper( $order );
$args = array();
// default - menu_order
$args['orderby'] = "strlen('title') => 'ASC', 'title' => 'ASC'";
// $args['orderby'] = 'title';
$args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
$args['meta_key'] = '';
switch ( $orderby ) {
case 'rand' :
$args['orderby'] = 'rand';
break;
case 'date' :
$args['orderby'] = 'date ID';
$args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
break;
case 'price' :
$args['orderby'] = "meta_value_num ID";
$args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
$args['meta_key'] = '_price';
break;
case 'popularity' :
$args['meta_key'] = 'total_sales';
// Sorting handled later though a hook
add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
break;
case 'rating' :
// Sorting handled later though a hook
add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
break;
case 'title' :
$args['orderby'] = "strlen('title') => 'ASC', 'title' => 'ASC'";
//$args['orderby'] = 'meta_value meta_value_num title';
$args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
break;
}
return apply_filters( 'woocommerce_get_catalog_ordering_args', $args );
}
在 PHP 中,只需调用 natsort((。
代码:(演示(
$array=['FND100000-C','FNC24500-C','FNC232500-C','FMC30000-C','FNC21000-C'];
natsort($array);
var_export($array);
输出:
array (
3 => 'FMC30000-C',
4 => 'FNC21000-C',
1 => 'FNC24500-C',
2 => 'FNC232500-C',
0 => 'FND100000-C',
)
在 MYSQL 中,只要格式是...
- 然后3个字母
- 然后是一系列数字
- 然后连字符
- 一个或多个字符
。您可以通过调用 SUBSTRING(( 和 SUBSTRING_INDEX(( 进行排序
表和查询:(演示(
CREATE TABLE `test` (
`PrdID` varchar(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `test` (`PrdID`) VALUES
('FMC30000-C'),
('FNC21000-B'),
('FNC21000-C'),
('FNC232500-C'),
('FNC24500-C'),
('FND100000-C');
ALTER TABLE `test`
ADD PRIMARY KEY (`PrdID`);
SELECT PrdID,
SUBSTRING(PrdID,1,3),
SUBSTRING(SUBSTRING_INDEX(PrdID,'-',1),4),
SUBSTRING_INDEX(PrdID,'-',-1)
FROM `test`
ORDER BY
SUBSTRING(PrdID,1,3),
CAST(SUBSTRING(SUBSTRING_INDEX(PrdID,'-',1),4) AS UNSIGNED),
SUBSTRING_INDEX(PrdID,'-',-1)
输出:
PrdID | SUBSTRING(PrdID,1,3) | SUBSTRING(SUBSTRING_INDEX(PrdID,'-',1),4) | SUBSTRING_INDEX(PrdID,'-',-1)
----------------------------------------------------------------------------------------------------------------
FMC30000-C | FMC | 30000 | C
FNC21000-B | FNC | 21000 | B
FNC21000-C | FNC | 21000 | C
FNC24500-C | FNC | 24500 | C
FNC232500-C | FNC | 232500 | C
FND100000-C | FND | 100000 | C
这是一个非WordPress开发人员在黑暗中的镜头......
$args['orderby'] = "substr('title',0,3( => 'ASC', substr(strstr('title','-',true(,3( => 'ASC', substr('title',strpos('title','-'(+1( => 'ASC'";
演示
看完之后: https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-query.php 看起来 orderby 组件只是空格分隔的,而不是逗号分隔的。 试试这个:
$args['orderby'] = "substr('title',0,3) substr(strstr('title','-',true),3) substr('title',strpos('title','-')+1)";
$args['order'] = $order==='DESC'?'DESC':'ASC';