Hide Out of Stock Products in WooCommerce with Code Snippet

How to hide out of stock products in wordpress; Wordpress hide products with stock 0; Woocommerce hide out of stock items; Hide out of stock products in woocommerce; Wordpress hide product variations with no stock; How to remove out of stock items from wordpress catalog; Wordpress hide items with stock status 0; Prevent showing out of stock products in wordpress; How to exclude out of stock products in woocommerce; Wordpress hide unavailable products;

Explanation

If you're running a WooCommerce store and want to keep your shop looking tidy by hiding products that are out of stock, this code snippet is just what you need.

Here's what it does:

  • Hides Out of Stock Products: The first function checks if you're on the main shop page, a product category, or a product tag page. It then modifies the query to exclude products that have a stock status of 'outofstock'. This means those products won't show up in your store's front end.
  • Adjusts WooCommerce Settings: The second function ensures that your WooCommerce settings are configured to hide out-of-stock items. It checks the setting and updates it if necessary, so you don't have to do it manually.

By using this code, you can automatically keep your store free of products that aren't available, providing a better shopping experience for your customers. Just make sure to add this code to your theme's functions.php file or a custom plugin to get started.

Code

// Function to hide out of stock products in WooCommerce
function wp_dudecom_hide_out_of_stock_products( $query ) {
    if ( ! is_admin() && $query->is_main_query() && ( is_shop() || is_product_category() || is_product_tag() ) ) {
        $meta_query = $query->get( 'meta_query' );

        if ( ! is_array( $meta_query ) ) {
            $meta_query = array();
        }

        $meta_query[] = array(
            'key'     => '_stock_status',
            'value'   => 'outofstock',
            'compare' => 'NOT IN'
        );

        $query->set( 'meta_query', $meta_query );
    }
}
add_action( 'pre_get_posts', 'wp_dudecom_hide_out_of_stock_products' );

// Ensure WooCommerce settings are set to hide out of stock items
function wp_dudecom_set_woocommerce_hide_out_of_stock() {
    if ( 'yes' !== get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
        update_option( 'woocommerce_hide_out_of_stock_items', 'yes' );
    }
}
add_action( 'init', 'wp_dudecom_set_woocommerce_hide_out_of_stock' );

Instructions

File Location: Add the code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.

Implementation Steps:

  1. Access Your WordPress Dashboard: Log in to your WordPress admin panel.
  2. Navigate to Theme Editor: Go to Appearance > Theme Editor. If you're using a child theme, ensure it's active.
  3. Open functions.php: In the right-hand sidebar, locate and click on Theme Functions (functions.php).
  4. Insert the Code: Scroll to the bottom of the file and paste the provided code snippet.
  5. Save Changes: Click the Update File button to save your changes.
  6. Verify WooCommerce Settings: The code automatically adjusts WooCommerce settings to hide out-of-stock items. However, you can manually check by going to WooCommerce > Settings > Products > Inventory and ensuring the Hide out of stock items from the catalog option is enabled.

By following these steps, your WooCommerce store will automatically hide products that are out of stock, enhancing the shopping experience for your customers.

If you need assistance with implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert help.