WooCommerce: Admin filter to seek out merchandise with out product picture set

We’re serving to a shopper who has a WordPress WooCommerce web site that has misplaced search engine visibility resulting from years of neglect, put in and uninstalled plugins and dozens of themes resulting from a whole lot of code, configuration and content material points.

As we launched the brand new web site, we monitored the positioning’s efficiency and just lately obtained the next Google Search Console message:

Image with structured data about dealer listings in Google Search Console

We have been stunned that the corporate had merchandise listed in WooCommerce that did not have a product picture set. After we crawled the newly launched web site, we didn’t discover any points. This was as a result of the brand new theme had a placeholder picture that was displayed each time no picture was set. Because of this, there have been no photographs not discovered errors.

WooCommerce product record

Our subsequent step was to establish the merchandise on the positioning that did not have photographs posted. That is no straightforward process when a whole lot of merchandise have to be filtered. That is why we wrote our personal filter in WooCommerce Merchandise to filter the record for merchandise that do not have a product picture set.

Product image filter not set

Now we are able to simply browse the record and replace the product photographs when wanted with none problem. That is how we did it.

Add filter to WooCommerce admin product record

On the features.php web page of the client’s baby theme, we added the next two sections of code. First we construct the filter subject:

// Add a filter on the product to set the product picture add_action(‘restrict_manage_posts’, ‘filter_products_by_image_presence’); operate filter_products_by_image_presence() { world $typenow; $chosen = isset($_GET[‘product_image_presence’])? $_GET[‘product_image_presence’] : ”; if (‘product’ === $typow) { ?> Here’s a step-by-step clarification of every a part of the code:

add_action(‘restrict_manage_posts’, ‘filter_products_by_image_presence’); This line connects to “restrict_manage_posts”, an motion triggered within the WordPress admin space that means that you can add further filtering choices to the posts record. Right here it’s used so as to add a brand new filter to the WooCommerce product record. operate filter_products_by_image_presence() { … } This block defines the filter_products_by_image_presence operate, which outputs HTML for a brand new drop-down choice filter on the product administration display screen. world $typow; The $typenow world variable is used to verify the kind of the present put up record to make sure that the customized filter is barely added to the Merchandise put up sort screens and never others. $chosen = isset($_GET[‘product_image_presence’])? $_GET[‘product_image_presence’] : ”; This line checks whether or not an energetic filter set exists by on the lookout for the product_image_presence parameter within the URL, which is handed as a GET request when you choose a filter possibility and submit the filter. It saves the present choice to keep up the chosen state of the filter after web page reload. if (‘product’ === $typenow) { … } This conditional assertion checks whether or not the present put up sort is “Product” and ensures that the code within the if assertion solely runs for WooCommerce merchandise. The whole lot between ?> and

This code successfully provides a drop-down filter to the product record, permitting the admin to filter the record by merchandise that do or wouldn’t have a picture set. This extra filter would assist customers handle giant catalogs and be certain that merchandise meet the necessities of the shop itemizing, together with attributing photographs as a part of the itemizing’s high quality management.

Run a question on the WooCommerce admin product record

Subsequent we have to add a question that runs and finds the merchandise for which there is no such thing as a picture set.

add_filter(‘parse_query’, ‘filter_products_query_by_image_presence’); operate filter_products_query_by_image_presence($question) { world $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’, ‘evaluate’ => ‘NOT EXISTS’ ), array( ‘key’ => ‘_thumbnail_id’, ‘worth’ => ‘0’ ) ); if (‘set’ === $presence) { $meta_query = array( array( ‘key’ => ‘_thumbnail_id’, ‘evaluate’ => ‘EXISTS’ ), array( ‘key’ => ‘_thumbnail_id’, ‘ worth’ => array(”, ‘0’), // Suppose ‘0’ or ” couldn’t be placeholders for any picture. ‘evaluate’ => ‘NOT IN’ ), ); } elseif (‘notset’ === $presence) { $meta_query = array( ‘relation’ => ‘OR’, array( ‘key’ => ‘_thumbnail_id’, ‘evaluate’ => ‘NOT EXISTS’ ), array ( ‘key’ => ‘_thumbnail_id’, ‘worth’ => ‘0’ ) ); } $query->set(‘meta_query’, $meta_query); } }

This code snippet is designed to change the principle WordPress product itemizing question within the admin panel to permit filtering of merchandise based mostly on whether or not they have an related picture. Right here is a proof of its elements:

add_filter(‘parse_query’, ‘filter_products_query_by_image_presence’); This line appends the filter_products_query_by_image_presence operate to the parse_query filter hook, which is used to customise the principle question that WordPress makes use of to retrieve posts (or customized put up varieties like merchandise) within the admin record desk. operate filter_products_query_by_image_presence($question) { … } This operate is outlined to vary the product record question based mostly on the presence of product photographs. The $question variable is an occasion of the WP_Query class handed by reference. Which means any adjustments to this object will have an effect on the precise question executed by WordPress. world $pagenow, $typow; These world variables are WordPress setting variables. $pagenow is used to verify the present admin web page and $typow is used to verify the present put up sort. The conditional assertion checks if the present web page is “edit.php” (the default web page for itemizing posts and customized put up varieties), the put up sort is “Product” (which means we’re on the WooCommerce product itemizing), and if a The filter was set by way of a GET parameter referred to as “product_image_presence”. A brand new meta question array is created based mostly on the worth of product_image_presence. This array is meant to create the circumstances for filtering merchandise with or with out photographs. The connection key set to OR signifies that any of the circumstances it accommodates may be true for the meta question to retrieve the merchandise. When the filter is about to set, a brand new $meta_query is created to seek out merchandise with photographs. Merchandise which have a “_thumbnail_id” (which means a picture is about) and never an empty string or “0” are included. If the filter is about to “notset”, the meta question will seek for merchandise the place the meta key “_thumbnail_id” both doesn’t exist or is about to “0”, which might imply that there is no such thing as a picture related to the product. $query->set(‘meta_query’, $meta_query); This line modifies the principle question by setting meta_query with the circumstances outlined in $meta_query.

This customization helps a WooCommerce retailer admin rapidly discover merchandise with out photographs, which is essential for stock administration, advertising and gross sales methods as a result of merchandise with photographs usually tend to promote and supply prospects with the required visible info. By guaranteeing product listings are absolutely featured with photographs, gross sales and advertising efforts may be simpler.

Similar Items

Leave a Comment