We’re serving to a shopper that has a WordPress WooCommerce website that was dropping search engine visibility for years because of a whole bunch of code, configuration, and content material points from years of neglect, put in and uninstalled plugins, and dozens of themes.
With the brand new website launched, we’ve been observing the efficiency of the location and just lately acquired the next Google Search Console message:
We had been stunned that the corporate had merchandise listed in WooCommerce that didn’t have a product picture set. After we crawled the newly launched website, we didn’t see any issues… this was as a result of the brand new theme had a placeholder picture that appeared every time a picture was not set. Because of this, there have been no errors for pictures not discovered.
WooCommerce Merchandise Checklist
Our subsequent step was to establish the merchandise inside the website the place there have been no pictures set. That’s not a simple process when there are a whole bunch of merchandise to filter by means of. Because of this, we wrote our personal filter in WooCommerce merchandise to filter the listing to merchandise with no product picture set.
Now we are able to simply browse the listing and replace the product pictures the place vital with out effort. Right here’s how we did it.
Add Filter To WooCommerce Admin Merchandise Checklist
Throughout the shopper’s youngster theme features.php web page, we added the next two sections of code. First, we construct the filter subject:
// Add a filter on product for set product picture
add_action('restrict_manage_posts', 'filter_products_by_image_presence');
perform filter_products_by_image_presence() {
international $typenow;
$chosen = isset($_GET['product_image_presence']) ? $_GET['product_image_presence'] : '';
if ('product' === $typenow) {
?>
<choose identify="product_image_presence" id="product_image_presence">
<possibility worth="">Filter by product picture</possibility>
<possibility worth="set" <?php chosen('set', $chosen); ?>>Picture Set</possibility>
<possibility worth="notset" <?php chosen('notset', $chosen); ?>>Picture Not Set</possibility>
</choose>
<?php
}
}
Right here’s a step-by-step clarification of what every a part of the code does:
add_action('restrict_manage_posts', 'filter_products_by_image_presence');
- This line hooks into
restrict_manage_posts
, which is an motion triggered within the WordPress admin space, permitting you so as to add further filtering choices to the posts listing. Right here, it’s used so as to add a brand new filter to the WooCommerce merchandise listing.
- This line hooks into
perform filter_products_by_image_presence() { ... }
- This block defines the perform
filter_products_by_image_presence
, which outputs HTML for a brand new dropdown choose filter on the product admin display screen.
- This block defines the perform
international $typenow;
- The worldwide variable
$typenow
is used to test the kind of the present put up listing to make sure that the customized filter is barely added to the ‘Merchandise’ put up kind screens and never others.
- The worldwide variable
$chosen = isset($_GET['product_image_presence']) ? $_GET['product_image_presence'] : '';
- This line checks if there may be an energetic filter set by searching 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 shops the present choice to retain the chosen state of the filter after the web page reloads.
if ('product' === $typenow) { ... }
- This conditional assertion checks whether or not the present put up kind is ‘product’, guaranteeing the code contained in the if-statement solely runs for WooCommerce merchandise.
- Every little thing between
?>
and<?php
is HTML output, together with the choose dropdown with choices for filtering by merchandise with ‘Picture Set’ or ‘Picture Not Set’. PHP is interspersed to deal with dynamic choice by way of thechosen()
perform, which outputs thechosen
attribute if the present$chosen
worth matches the choice worth. - The
chosen()
perform is a WordPress helper perform that compares the primary argument with the second and in the event that they match, it outputs ‘chosen=”chosen”‘, which is the HTML attribute wanted to indicate an possibility as chosen in a dropdown.
This code successfully provides a dropdown filter to the merchandise listing, enabling the admin to filter the listing by merchandise which have a picture set or not. This extra filter would assist customers handle giant catalogs, guaranteeing merchandise adhere to retailer itemizing necessities, together with having pictures assigned as a part of the itemizing high quality management.
Execute Question on WooCommerce Admin Merchandise Checklist
Subsequent, now we have so as to add a question that may execute and discover the merchandise that don’t have any picture set.
add_filter('parse_query', 'filter_products_query_by_image_presence');
perform filter_products_query_by_image_presence($question) {
international $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'), // Assuming '0' or '' might be placeholders for no 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 for modifying the principle WordPress question for product listings within the admin space to permit filtering merchandise primarily based on whether or not they have an related picture. Right here’s an evidence of its parts:
add_filter('parse_query', 'filter_products_query_by_image_presence');
- This line attaches the
filter_products_query_by_image_presence
perform to theparse_query
filter hook, which is used to regulate the principle question that WordPress makes use of to retrieve posts (or customized put up sorts like merchandise) within the admin listing desk.
- This line attaches the
perform filter_products_query_by_image_presence($question) { ... }
- This perform is outlined to change the product listing question primarily based on the presence of product pictures. The
$question
variable is an occasion of theWP_Query
class, handed by reference, which suggests any adjustments to this object will have an effect on the precise question WordPress runs.
- This perform is outlined to change the product listing question primarily based on the presence of product pictures. The
international $pagenow, $typenow;
- These international variables are WordPress surroundings variables.
$pagenow
is used to test the present admin web page, and$typenow
to test the present put up kind. - The conditional assertion checks if the present web page is ‘edit.php’ (the default web page for itemizing posts and customized put up sorts), the put up kind is ‘product’ (which suggests we’re on the WooCommerce merchandise listing), and if a filter has been set by means of a
GET
parameter named ‘product_image_presence’.
- These international variables are WordPress surroundings variables.
- A brand new meta question array is created primarily based on the worth of ‘product_image_presence’. This array is designed to create the situations for filtering merchandise with or with out pictures.
- The
relation
key set to ‘OR’ signifies that any of the situations inside might be true for the meta question to retrieve the merchandise. - If the filter is about to ‘set’, a brand new
$meta_query
is created to seek out merchandise with pictures. Merchandise which have a ‘_thumbnail_id’ (which suggests a picture is about) and never an empty string or ‘0’ are included. - If the filter is about to ‘notset’, the meta question seems for merchandise the place the ‘_thumbnail_id’ meta key both doesn’t exist or is about to ‘0’, which might imply there isn’t any picture related to the product.
- The
$query->set('meta_query', $meta_query);
- This line modifies the principle question by setting the ‘meta_query’ with the situations outlined in
$meta_query
.
- This line modifies the principle question by setting the ‘meta_query’ with the situations outlined in
This customization helps a WooCommerce retailer admin to rapidly discover merchandise missing pictures, which is essential for stock administration, advertising, and gross sales methods, as merchandise with pictures usually tend to promote and supply clients with the mandatory visible data. By guaranteeing product listings are full with pictures, gross sales and advertising efforts might be simpler.