How to Programmatically Manage Product Visibility in WooCommerce Based on Stock Status – 2026

Last updated on December 27th, 2024 at 08:21 am

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) or hidden (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.
See also  How to Change the Product Tabs Order in WooCommerce - 2026

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.
See also  Reordering WooCommerce checkout fields 2026

Step 5: Test the Functionality

  1. Add a Product:
    • Create a product in WooCommerce and set its stock status to In Stock or Out of Stock.
  2. Check the Catalogue:
    • Verify that the product appears or disappears from the shop page based on its stock status.
  3. Search Functionality:
    • Use the search bar to confirm that out-of-stock products are excluded from search results.

Example Workflow

Stock StatusVisibilityAction
In StockVisibleDisplayed on shop/search
Out of StockHiddenRemoved from shop/search
Featured & Out of StockVisibleDisplayed 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.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×