gpt4 book ai didi

php - WooCommerce 中标题由字母和数字组成时的自定义排序产品

转载 作者:行者123 更新时间:2023-11-30 21:54:45 25 4
gpt4 key购买 nike

因此,我进行了一些挖掘,但未能找到正确的方法。

我在 WooCommerce (Wordpress) 上有一些产品,但它们的标题是由字母和数字混合而成。我需要像这样打印这些产品(例如):

FNC21000-C
FNC24500-C
FNC232500-C

但是,当然,Wordpress 给了我:

FNC21000-C
FNC232500-C
FNC24500-C

有什么办法可以实现吗?

我想我可以在 WP_Query 上做一些事情,但我真的是 PHP 的新手,我知道一些可能的修复是在 PHP 上,其他一些是在 MySQL 上......事实上标题是 BOTH 字符串和整数,让我对如何写这个感到无能为力......

有人有什么建议吗?

下面是 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() .

代码:(Demo)

$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()

表和查询:(Demo)

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'";

Demo

看完后: 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';

关于php - WooCommerce 中标题由字母和数字组成时的自定义排序产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45684131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com