1

I know that there is this option on admin to hide products from the entire catalog if there is no stock, but how do I hide these products only from home page and not from the entire catalog?

I want to be able to search the products and see it even if it has no stock.

  • It depends. How are you displaying the products on the homepage? – Jacob Peattie Mar 8 at 14:59
  • i'm using the store front theme with default configuration. – RFL Mar 8 at 15:51
1

Assuming you are using storefront default homepage template. You can filter products before displaying on hompepage

function rfl_show_backorders( $is_visible, $id ) {
    $product = new wC_Product( $id );

    if ( ! $product->is_in_stock() && ! $product->backorders_allowed() && is_front_page() ) {
        $is_visible = false;
    }

    return $is_visible;
}
add_filter( 'woocommerce_product_is_visible', 'rfl_show_backorders', 10, 2 );
0

You can check current page is HOME page like this:

You can check product is in stock or not using $product->is_in_stock() in your loop.

if ( is_front_page() || is_home() ) {
    // Out of products stuff here
}

Hope it will help you !

0

To display only in stock products you can add _stock_status key with the appropriate value to meta query.

add_filter('woocommerce_product_query_meta_query', 'wpse_331086_instock_homepage', 20);

function wpse_331086_instock_homepage( $meta_query )
{
    //
    // is_shop()        - apply on shop homepage (product archive page)
    // is_front_page()  - apply on site homepage
    //
    // stock status: 'instock', 'outofstock', 'onbackorder'
    if ( is_shop() ) {
        $meta_query[] = array(
            'key'           => '_stock_status',
            'value'         => 'instock',
            'compare'       => '='
        );
    }
    return $meta_query;
}

Code goes in functions.php file of your theme .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.