- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,我进行了一些挖掘,但未能找到正确的方法。
我在 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中,只要格式是...
...您可以调用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'";
看完后: 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/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!