隐藏“缺货"WooCommerce主页上的产品 [英] Hide "out of stock" products on home page in WooCommerce

查看:61
本文介绍了隐藏“缺货"WooCommerce主页上的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想在我的主页上显示缺货"产品.我已经尝试了一些WooCommerce挂钩和过滤器来更改产品查询,但是它不起作用.我还检查了隐藏缺货"进入woocommerce设置区域.

I don't want to display "out of stock" products on my home page. I have tried some WooCommerce hooks and filter to alter product query but its not working. I have also checked "hide out of stock" into woocommerce setting area.

,但产品仍然出现.发生时,我能得到线索吗?

but product are still appearing. Can I get the clue, wh its happening.

我尝试使用此过滤器挂钩来更改主要产品查询:

I tried this filter hook to alter main product query:

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {


$q->set( 'meta_query', array(array(
    'key'       => '_stock_status',
    'value'     => 'outofstock',
    'compare'   => 'NOT IN'
)));

}

remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

我想隐藏缺货的产品,但是没有任何效果.感谢您的帮助.

I want to hide out of stock products but nothing is working. Any help is appreciated.

推荐答案

要从首页中排除缺货"产品,可以用不同的方法来完成.

To exclude "Out of stock" products from your homepage, it can be done in different ways.

1)使用专用 woocommerce_product_query_meta_query 过滤器挂钩的元查询:

1) A Meta Query using dedicated woocommerce_product_query_meta_query filter hook:

add_filter( 'woocommerce_product_query_meta_query', 'filter_product_query_meta_query', 10, 2 );
function filter_product_query_meta_query( $meta_query, $query ) {
    // On woocommerce home page only
    if( is_front_page() ){
        // Exclude products "out of stock"
        $meta_query[] = array(
            'key'     => '_stock_status',
            'value'   => 'outofstock',
            'compare' => '!=',
        );
    }
    return $meta_query;
}

2)使用专用的 woocommerce_product_query_tax_query 过滤器挂钩进行税收查询:

2) A Tax Query using dedicated woocommerce_product_query_tax_query filter hook:

add_filter( 'woocommerce_product_query_tax_query', 'filter_product_query_tax_query', 10, 2 );
function filter_product_query_tax_query( $tax_query, $query ) {
    // On woocommerce home page only
    if( is_front_page() ){
        // Exclude products "out of stock"
        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => array('outofstock'),
            'operator' => 'NOT IN'
        );
    }
    return $tax_query;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.两者都可以.

Code goes in functions.php file of your active child theme (or active theme). Both works.

相关:仅在Woocommerce中的商店归档页面上隐藏缺货产品

这篇关于隐藏“缺货"WooCommerce主页上的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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