最终在WooCommerce的最后显示出库存产品 - 排除后订单
请注意我对其他类似问题的要求的区别。该片段有助于将库存物品放在底部:
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;
}
但是,我想排除backorder
状态,然后只将产品视为sidkock,因此它们保持在同一位置,就像它们一样有存货。我认为区别将在加入
中,但我不清楚所需的SQL。
Please note the difference in my request to the other similar questions. This snippet works to keep Out of Stock items at the bottom:
Show Out of stock products at the end in Woocommerce
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;
}
However, I want to exclude backorder
status and simply consider products on backorder as instock, so they remain in the same position as if they were in stock. I think the difference will be in the JOIN
but I am unclear on the SQL required.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该片段通过
stock_status
在ASC
(登上)订单中对产品进行分类。可能的值是instock
OUTOFSTOCK
onbackOrder
,因此它会自动在字母内显示它们:1.Instock 2.Instock 2. backbackorder 3.OutofstockThis snippet sorts products by
stock_status
inASC
(ascending) order. Possible values areinstock
outofstock
onbackorder
so it will automatically display them alphabetically like this: 1.instock 2.onbackorder 3.outofstock