woocommerce在前端按库存和缺货对产品进行排序 [英] woocommerce sort products by in stock and out of stock in front-end

查看:83
本文介绍了woocommerce在前端按库存和缺货对产品进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想首先在产品类别中或如果可能的话在任何地方显示有现货的产品,然后我希望在Woocommerce中也显示缺货的产品

I want to show in stock products first in products category or if possible in any where and then I want to display out of stock products too after them in Woocommerce

实际上有很多产品没有数量编号,但是有库存,因此需要检查库存状态,但是我更喜欢先添加更多数量

Actually there are many products which have not quantity number but those are in stock, So it is need to check in stock status, But I prefer to have more quantities first

在这种情况下,如何为当前分类提供帮助? 非常感谢

How can I make a force for current sorting in such case? Thank you very much

推荐答案

使用此代码:

<?php

/**
 * Order product collections by stock status, instock products first.
 */
class iWC_Orderby_Stock_Status
{

    public function __construct()
    {
        // Check if WooCommerce is active
        if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
            add_filter('posts_clauses', array($this, 'order_by_stock_status'), 2000, 2);
        }
    }

    public function order_by_stock_status($posts_clauses, $wp_query)
    {
        global $wpdb;
        // only change query on WooCommerce loops
        if (false === is_admin() && $wp_query->is_post_type_archive('product')) {
            $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;
    }
}

new iWC_Orderby_Stock_Status;

?>

这篇关于woocommerce在前端按库存和缺货对产品进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆