WooCommerce: Admin Filter To Find Products Without A Product Image Set

We are helping a client that has a WordPress WooCommerce site that was losing search engine visibility for years due to hundreds of code, configuration, and content issues from years of neglect, installed and uninstalled plugins, and dozens of themes.

With the new site launched, we’ve been observing the performance of the site and recently received the following Google Search Console message:

google search console merchant listings structured data issue image

We were surprised that the company had products listed in WooCommerce that didn’t have a product image set. When we crawled the newly launched site, we didn’t see any problems… this was because the new theme had a placeholder image that appeared whenever an image was not set. As a result, there were no errors for images not found.

WooCommerce Products List

Our next step was to identify the products within the site where there were no images set. That’s not an easy task when there are hundreds of products to filter through. As a result, we wrote our own filter in WooCommerce products to filter the list to products with no product image set.

Now we can easily browse the list and update the product images where necessary without effort. Here’s how we did it.

Add Filter To WooCommerce Admin Products List

Within the client’s child theme functions.php page, we added the following two sections of code. First, we build the filter field:

// Add a filter on product for set product image
add_action('restrict_manage_posts', 'filter_products_by_image_presence');
function filter_products_by_image_presence() {
    global $typenow;
    $selected = isset($_GET['product_image_presence']) ? $_GET['product_image_presence'] : '';
    if ('product' === $typenow) {
        ?>
        <select name="product_image_presence" id="product_image_presence">
            <option value="">Filter by product image</option>
            <option value="set" <?php selected('set', $selected); ?>>Image Set</option>
            <option value="notset" <?php selected('notset', $selected); ?>>Image Not Set</option>
        </select>
        <?php
    }
}

Here’s a step-by-step explanation of what each part of the code does:

This code effectively adds a dropdown filter to the products list, enabling the admin to filter the list by products that have an image set or not. This additional filter would help users manage large catalogs, ensuring products adhere to store listing requirements, including having images assigned as part of the listing quality control.

Execute Query on WooCommerce Admin Products List

Next, we have to add a query that will execute and find the products that have no image set.

add_filter('parse_query', 'filter_products_query_by_image_presence');
function filter_products_query_by_image_presence($query) {
    global $pagenow, $typenow;

    if ('edit.php' === $pagenow && 'product' === $typenow && isset($_GET['product_image_presence']) && $_GET['product_image_presence'] != '') {
        $presence = $_GET['product_image_presence'];
        $meta_query = array(
            'relation' => 'OR',
            array(
                'key' => '_thumbnail_id',
                'compare' => 'NOT EXISTS'
            ),
            array(
                'key' => '_thumbnail_id',
                'value' => '0'
            )
        );

        if ('set' === $presence) {
            $meta_query = array(
                array(
                    'key' => '_thumbnail_id',
                    'compare' => 'EXISTS'
                ),
                array(
                    'key' => '_thumbnail_id',
                    'value' => array('', '0'), // Assuming '0' or '' could be placeholders for no image.
                    'compare' => 'NOT IN'
                ),
            );
        } elseif ('notset' === $presence) {
            $meta_query = array(
                'relation' => 'OR',
                array(
                    'key' => '_thumbnail_id',
                    'compare' => 'NOT EXISTS'
                ),
                array(
                    'key' => '_thumbnail_id',
                    'value' => '0'
                )
            );
        }

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

This code snippet is for modifying the main WordPress query for product listings in the admin area to allow filtering products based on whether they have an associated image. Here’s an explanation of its components:

This customization helps a WooCommerce store admin to quickly find products lacking images, which is crucial for inventory management, marketing, and sales strategies, as products with images are more likely to sell and provide customers with the necessary visual information. By ensuring product listings are complete with images, sales and marketing efforts can be more effective.

Exit mobile version