Woocommerce: list all products without a shipping class
| jtg
Information originally found here: https://shanegowland.com/advanced-woocommerce/2018/how-to-find-woocommerce-products-without-a-shipping-class/
Just create a new PHP file in wp-content/plugins with the following code:
<?php
/**
* Plugin Name: Find Products Without Shipping Class
* Description: Adds a page that lists all products without a shipping class
* Author: Shane Gowland
* License: GPL
* License URI: https://gowland.me
*/
function register_shipping_class_checker_page() {
add_submenu_page( 'edit.php?post_type=product', 'Missing Shipping Classes', 'No Shipping Class', 'manage_options', 'missing_shipping_classes_page', 'missing_shipping_classes_page_callback' );
}
function missing_shipping_classes_page_callback() { ?>
<div class="wrap">
<h1>Missing Shipping Classes</h1>
<p>The products listed below do not have a Shipping Class assigned.</p>
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$loop = new WP_Query( $args );
global $post;
global $product;
//Count how many products were found so we can give zero-results feedback
$products_found = 0;
while ( $loop->have_posts() ) : $loop->the_post();
$_product = wc_get_product($post->ID);
$shipclass = $_product->get_shipping_class();
//Output the product
if (strlen($shipclass) == 0) {
echo '<br /> '.get_the_title(). ' — <a target="_blank" href="' . get_edit_post_link($post->ID) . '">Edit Product</a>';
$products_found++;
}
endwhile;
wp_reset_query();
//Output something if the number of results is zero
if ($products_found == 0) {
echo "No products were found.";
}
?>
</div>
<?php }
add_action('admin_menu', 'register_shipping_class_checker_page',99);