在 Woocommerce 的最后显示缺货产品 [英] Show Out of stock products at the end in Woocommerce
问题描述
是否可以在 wordpress 中的类别或页面末尾显示缺货产品?
Is it possible to show out of stock products at the end of a category or page in wordpress?
所以客户首先看到有货的产品,然后看到缺货的产品.
So the customer first see the products that are available and after that the products that are out of stock.
推荐答案
这和 Viktor & 一样Bogdan 的回答,但没有额外的班级代码.
This is the same as Viktor & Bogdan's answer, but without the extra Class code.
它使用 post_clause
过滤器来修改产品查询.我们将 wp_postmeta 表JOIN
到查询中,并在现有查询前添加一个 orderby _stock_status
子句.这样,任何其他 orderby
子句也会保留在查询中.
It uses the post_clause
filter to modify the product query. We JOIN
the wp_postmeta table to the query and prepend an orderby _stock_status
clause to the existing query. This way any other orderby
clauses remain in the query as well.
add_filter('posts_clauses', 'order_by_stock_status');
function order_by_stock_status($posts_clauses) {
global $wpdb;
// only change query on WooCommerce loops
if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {
$posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
$posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
$posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
}
return $posts_clauses;
}
如果您出于某种原因首先想要缺货商品,您可以将 istockstatus.meta_value ASC
更改为 istockstatus.meta_value DESC
.
You could change istockstatus.meta_value ASC
to istockstatus.meta_value DESC
if you for some reason wanted the Out Of Stock items first.
在 WP 上测试:4.8;厕所 3.0.8
Tested on WP: 4.8; WC 3.0.8
这篇关于在 Woocommerce 的最后显示缺货产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!