最终在WooCommerce的最后显示出库存产品 - 排除后订单

发布于 2025-01-22 20:01:01 字数 1061 浏览 0 评论 0原文

请注意我对其他类似问题的要求的区别。该片段有助于将库存物品放在底部:

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2025-01-29 20:01:02
/**
 * 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;
}

该片段通过stock_statusASC(登上)订单中对产品进行分类。可能的值是instock OUTOFSTOCK onbackOrder,因此它会自动在字母内显示它们:1.Instock 2.Instock 2. backbackorder 3.Outofstock

/**
 * 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. Possible values are instock outofstock onbackorder so it will automatically display them alphabetically like this: 1.instock 2.onbackorder 3.outofstock

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文