In WooCommerce, managing product visibility based on stock status ensures that your store only displays products that are available for purchase. Hiding out-of-stock products programmatically can enhance the customer experience by avoiding frustration over unavailable products.
This guide explains how to automatically manage product visibility based on stock status without using a plugin.
Step 1: Update Product Visibility on Stock Changes
WooCommerce triggers stock updates whenever the product quantity changes. Using this hook, we can dynamically update the product visibility based on its stock status.
Code to Automatically Manage Product Visibility
Add this code to your theme’s functions.php file or your child theme:
/*
* Snippet: How to Programmatically Manage Product Visibility in WooCommerce Based on Stock Status – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1817
* Tested with WooCommerce 10.7.0
* "This function hides or shows products based on stock status"
*/
function wcsuccess_update_product_visibility_based_on_stock( $product_id ) {
$product = wc_get_product( $product_id );
if ( $product->is_in_stock() ) {
// If the product is in stock, make it visible
$product->set_catalog_visibility( 'visible' );
} else {
// If the product is out of stock, hide it from the catalogue
$product->set_catalog_visibility( 'hidden' );
}
$product->save();
}
add_action( 'woocommerce_product_set_stock_status', 'wcsuccess_update_product_visibility_based_on_stock', 10, 1 );
Explanation:
- Stock Status Check: Verifies if the product is in stock or out of stock.
- Visibility Update: Sets product visibility to
visible(in stock) orhidden(out of stock). - Automatic Update: Ensures visibility is updated whenever the stock status changes.
Step 2: Hide Out-of-Stock Products from Search Results
If a product is out of stock, you may also want to remove it from search results to streamline customer navigation.
Code to Exclude Out-of-Stock Products from Search
Add this code to your functions.php file:
/*
* Snippet: How to Programmatically Manage Product Visibility in WooCommerce Based on Stock Status – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1817
* Tested with WooCommerce 10.7.0
* "This function removes out of stock products from search results"
*/
function wcsuccess_exclude_out_of_stock_from_search( $query ) {
if ( ! is_admin() && $query->is_search && $query->is_main_query() ) {
$query->set( 'meta_query', array(
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '='
)
));
}
}
add_action( 'pre_get_posts', 'wcsuccess_exclude_out_of_stock_from_search' );
Explanation:
- Search Filtering: Modifies the search query to include only products with a stock status of
instock. - Non-Admin Context: Ensures the filtering applies only to the front end and not the admin area.
Step 3: Display an Admin Notification for Hidden Products
To keep store administrators informed, you can display a notification when products are automatically hidden due to being out of stock.
Code to Add Admin Notification
Add this code to your functions.php file:
/*
* Snippet: How to Programmatically Manage Product Visibility in WooCommerce Based on Stock Status – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1817
* Tested with WooCommerce 10.7.0
* "This function displays an admin notification for hidden products"
*/
function wcsuccess_admin_notice_for_hidden_products() {
$hidden_products = wc_get_products( array(
'status' => 'publish',
'limit' => -1,
'visibility' => 'hidden'
));
if ( ! empty( $hidden_products ) ) {
$count = count( $hidden_products );
echo '<div class="notice notice-warning is-dismissible">';
echo '<p>' . sprintf( __( 'You have %d products hidden due to being out of stock.', 'woocommerce' ), $count ) . '</p>';
echo '</div>';
}
}
add_action( 'admin_notices', 'wcsuccess_admin_notice_for_hidden_products' );
Explanation:
- Admin Alerts: Displays a notice in the WordPress admin area if there are hidden products.
- Count of Hidden Products: Dynamically counts and displays the number of hidden products.
Step 4: Customise the Visibility Criteria (Optional)
If you want to manage visibility differently for certain products (e.g., always show featured products), you can customise the criteria.
Code to Exclude Featured Products from Hiding
Modify the visibility update function:
if ( $product->is_in_stock() || $product->is_featured() ) {
$product->set_catalog_visibility( 'visible' );
} else {
$product->set_catalog_visibility( 'hidden' );
}
Explanation:
- Featured Products: Keeps featured products visible, even if they are out of stock.
Step 5: Test the Functionality
- Add a Product:
- Create a product in WooCommerce and set its stock status to In Stock or Out of Stock.
- Check the Catalogue:
- Verify that the product appears or disappears from the shop page based on its stock status.
- Search Functionality:
- Use the search bar to confirm that out-of-stock products are excluded from search results.
Example Workflow
| Stock Status | Visibility | Action |
|---|---|---|
| In Stock | Visible | Displayed on shop/search |
| Out of Stock | Hidden | Removed from shop/search |
| Featured & Out of Stock | Visible | Displayed on shop/search |
Conclusion
Managing WooCommerce product visibility programmatically based on stock status ensures a seamless shopping experience for your customers. By hiding out-of-stock products, you can reduce customer frustration and focus their attention on available inventory.
Test these changes in a staging environment before applying them to your live site. For more WooCommerce customisation options, explore our WooCommerce Visual Hooks Guide or use our wp-config generator for advanced configurations.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

