Filter products by catalog visibility in WooCommerce Admin

Last edited:
December 27, 2023
Reading time:
8 mins

/

Blog

/

Development, WooCommerce

/

Filter products by catalo...

đź’ˇWe may earn a commision if you subscribe to a service from a link on this page.

WooCommerce is a powerful e-commerce plugin for WordPress, but there are instances where certain features that seem essential are missing. One such feature is the ability to filter products by their catalog visibility in the “All Products” section of the WooCommerce admin. Catalog visibility allows you to determine whether a product is visible to everyone, only to logged-in users, or to specific user roles. By default, WooCommerce doesn’t provide an option to filter products based on their catalog visibility settings in the admin area, even though it can be incredibly useful for managing your online store efficiently.

Our goal is to create this:

The Importance of Catalog Visibility

Catalog visibility is a critical aspect of managing your WooCommerce store effectively. It allows you to control who can see and purchase specific products, which can be essential for various scenarios. For example, you may want to offer exclusive products only to logged-in customers or provide special pricing to wholesale users. Without catalog visibility settings, managing these aspects becomes challenging.

You can change visibility settings of any product in it’s edit page:

Why Filtering by Catalog Visibility Matters

While WooCommerce provides the ability to set catalog visibility on individual product pages, it falls short when it comes to efficiently managing a large inventory. When you have a substantial number of products, being able to filter them based on their catalog visibility settings becomes a necessity. This feature simplifies tasks such as:

  1. Bulk Updates: If you want to change the visibility of multiple products at once, filtering by catalog visibility allows you to select and modify them efficiently.
  2. Inventory Management: When you need to keep track of products with specific visibility settings, filtering helps you identify and manage them without sifting through your entire product list.
  3. Offer Customization: By being able to quickly filter products by catalog visibility, you can tailor offers and promotions to specific user groups, enhancing the customer experience and potentially increasing sales.
  4. Streamlined User Experience: For store administrators, having the ability to filter products by catalog visibility reduces the time and effort required to manage and maintain the online catalog.

Enabling the Feature

To enable the ability to filter products by catalog visibility in the “All Products” section of WooCommerce, you can add custom code to your WordPress site. This code will introduce the necessary functionality, making your product management more efficient and user-friendly.

In the following code block, we’ll provide you with the code snippet to add this feature to your WooCommerce site. Once implemented, you’ll have the capability to filter products by their catalog visibility settings, helping you streamline your store management tasks.

// Add the custom catalog visibility filter to the admin products list page using the 'restrict_manage_posts' filter.
function add_catalog_visibility_filter() {
    global $post_type;

    if ($post_type === 'product') {
        // Define an array of visibility options.
        $visibility_options = array(
            ''                      => esc_html__('All Catalog Visibilities', 'your-text-domain'),
			'visible-in-both'       => esc_html__('Visible in Both', 'your-text-domain'),
            'exclude-from-catalog'  => esc_html__('Exclude from Catalog', 'your-text-domain'),
            'exclude-from-search'   => esc_html__('Exclude from Search', 'your-text-domain'),
            'both-excluded'         => esc_html__('Both Excluded', 'your-text-domain'),
            'featured'              => esc_html__('Featured', 'your-text-domain'),
        );

        // Get the current filter value (if any).
        $current_visibility = isset($_GET['catalog_visibility']) ? sanitize_text_field($_GET['catalog_visibility']) : '';

        // Output the filter dropdown.
        echo '<select name="catalog_visibility">';
        foreach ($visibility_options as $key => $label) {
            $selected = $key === $current_visibility ? 'selected="selected"' : '';
            echo '<option value="' . esc_attr($key) . '" ' . $selected . '>' . esc_html($label) . '</option>';
        }
        echo '</select>';
    }
}

// Hook into the 'restrict_manage_posts' filter to add the filter dropdown.
add_filter('restrict_manage_posts', 'add_catalog_visibility_filter');

// Filter products based on the selected catalog visibility using the 'parse_tax_query' action.
function filter_products_by_catalog_visibility($query) {
    global $pagenow;

    if (is_admin() && $pagenow == 'edit.php' && isset($_GET['catalog_visibility']) && $_GET['catalog_visibility'] != '') {
        $visibility = sanitize_text_field($_GET['catalog_visibility']);

        if ($visibility != 'all') {
            $tax_query = array(
                'relation' => 'OR',
                array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'slug',
                    'terms'    => $visibility,
                ),
            );

            // For 'both-excluded', include products that are both 'exclude-from-catalog' and 'exclude-from-search'.
            if ($visibility == 'both-excluded') {
                $tax_query[] = array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'slug',
                    'terms'    => array('exclude-from-catalog', 'exclude-from-search'),
                    'operator' => 'AND',
                );
            }

            // For 'visible-in-both', exclude products that are either 'exclude-from-catalog' or 'exclude-from-search'.
            if ($visibility == 'visible-in-both') {
                $tax_query[] = array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'slug',
                    'terms'    => array('exclude-from-catalog', 'exclude-from-search'),
                    'operator' => 'NOT IN',
                );
            }

            $query->set('tax_query', $tax_query);
        }
    }
}

// Hook into the 'parse_tax_query' action to filter products by catalog visibility.
add_action('parse_tax_query', 'filter_products_by_catalog_visibility');

Examples of Changing Default Visibility:

Now, let’s explore some examples of when and why you might want to change the default visibility settings of products in your WooCommerce store.

  1. Hide from Catalog:
    • Scenario: You have a product that is part of a limited-time promotion or is exclusively available to a specific group of customers.
    • Example: A membership-based website offers special discounts to its premium members. They want to hide these discounted products from regular catalog view but still make them accessible to logged-in members.
  2. Hide from Search Results:
    • Scenario: You sell products with certain sensitive information or specific use cases that you don’t want to appear in general search results.
    • Example: A medical supply store sells specialized medical equipment for specific conditions. They choose to hide these products from search results to prevent unrelated users from finding them through general searches.
  3. Completely Hide:
    • Scenario: You have products that are no longer in stock, and you don’t want customers to see them on your site until they are back in stock.
    • Example: An online electronics store temporarily runs out of stock for a particular smartphone model. They decide to completely hide the product from the catalog and search results until they restock it.

Filter your products by visibility in WC Admin!

Catalog visibility settings play a crucial role in managing an efficient and customer-focused WooCommerce store. Being able to filter products by their visibility settings in the “All Products” section of your WooCommerce admin can simplify product management and improve the customer experience. By implementing the custom code provided in this guide, you’ll gain the capability to filter products based on their catalog visibility, ensuring that your online store meets the unique needs of your business and customers.