简体   繁体   English

在 WooCommerce 中最后显示缺货产品 - 不包括延期交货

[英]Show Out of stock products at the end in WooCommerce - excluding backorder

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 在 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.但是,我想排除backorder状态,并简单地将延期交货的产品视为库存,因此它们保持与库存相同的位置。 I think the difference will be in the JOIN but I am unclear on the SQL required.我认为不同之处在于JOIN但我不清楚所需的 SQL。

/**
 * Sort Products By Stock Status - WooCommerce Shop
 */
 
add_filter( 'woocommerce_get_catalog_ordering_args', 'shop_sort_by_stock_amount', 9999 );
 
function shop_sort_by_stock_amount( $args ) {
   $args['orderby'] = 'meta_value';
   $args['meta_key'] = '_stock_status';
   return $args;
}

This snippet sorts products by stock_status in ASC (ascending) order.此代码段按ASC (升序)顺序按stock_status对产品进行排序。 Possible values are instock outofstock onbackorder so it will automatically display them alphabetically like this: 1.instock 2.onbackorder 3.outofstock可能的值是instock outofstock onbackorder所以它会自动按字母顺序显示它们,如下所示: 1.instock 2.onbackorder 3.outofstock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM