php - Woocommerce / 如何按库存状态对相关产品进行排序?
问题描述
如何在 woocommerce 中按库存状态对相关产品进行排序?
我已将此代码添加到function.php
function custom_remove_hook(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
add_action( 'woocommerce_after_single_product_summary', 'custom_function_to_sort_related', 22 );
}
add_action( 'woocommerce_after_single_product_summary', 'custom_remove_hook' );
function custom_function_to_sort_related( $args = array() ) {
global $product;
if ( ! $product ) {
return;
}
$defaults = array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => 'price', // @codingStandardsIgnoreLine.
'order' => 'desc'
);
$args = wp_parse_args( $args, $defaults );
// Get visible related products then sort them at random.
$args['related_products'] = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), $args['posts_per_page'], $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' );
// Handle orderby.
$args['related_products'] = wc_products_array_orderby( $args['related_products'], $args['orderby'], $args['order'] );
// Set global loop values.
wc_set_loop_prop( 'name', 'related' );
wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_related_products_columns', $args['columns'] ) );
wc_get_template( 'single-product/related.php', $args );
}
但我想按库存状态对单页中的相关产品进行排序(先显示库存产品,然后显示缺货产品);
我已经改变了$defaults
这样的数组:
$defaults = array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => '_stock_status',
'order' => 'array( 'meta_value' => 'ASC' )
);
它不起作用。我怎么解决这个问题?
解决方案
试试这些而不是你用的那个
$defaults = array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => array('price', 'stock')
'order' => 'desc'
);
或者您可以在从数据库中获取图像时根据您的要求对过滤后的图像进行排序。
推荐阅读
- javascript - 我的 JS/CSS 滑块在第一张幻灯片后停止工作。我该如何解决这个问题?
- python - 使用 Pymongo 更新_Many Mongo 文档
- javascript - 将输入类型文件渲染为Vue组件中的列表
- c# - 使用 .net 创建 PayPal 产品
- python - 用于掩码或无序输入数据的 Python 2d 插值
- android - KMM commonTest 从 Asset 中读取文件
- powershell - PowerShell ForEach-Object 和流水线 - 上游
- python - SQLAlchemy 迁移的问题
- sqlite - 运行 sqlite 的 esp32 上的 group by sql 语句上的磁盘 io 错误
- c# - 如何模拟基类的隐藏属性?