3

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Rango
  • 313
  • 1
  • 4
  • 16

3 Answers3

3

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

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) 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;
}

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


Related: Hide out of stock products only on shop archive pages in Woocommerce

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
1

You are using code for old WooCommerce version. Try this approach.

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    if ( ! $q->is_main_query() || is_admin() || ! $q->is_front_page() ) {
        return;
    }

    if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) {

        $tax_query = (array) $q->get('tax_query');

        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field' => 'term_taxonomy_id',
            'terms' => array( $outofstock_term->term_taxonomy_id ),
            'operator' => 'NOT IN'
        );

        $q->set( 'tax_query', $tax_query );

    }
}
Artem
  • 755
  • 5
  • 19
0

UPDATE

Check font-page.php file in your current theme folder, maybe your developers have added a custom code into your template files without calling any hook actions.

OLD ANSWER

Why don't use WooCommerce Settings?

  1. Go to Woocommerce → Settings and click the Products tab
  2. Click the Inventory link at the top
  3. Check the Hide out of stock items from the catalog option to hide out of stock items
Jamy
  • 53
  • 6
  • 1
    Because it's only for home page (as title says) and if you read the question, the OP as already tried what you are suggesting. – LoicTheAztec May 10 '19 at 03:08