WooCommerce Get All The Products With No Weight (Zero Weight)

Hi Guys,

In WooCommerce, you can fetch all the products with zero weight using custom SQL query to database.
If you want to get all the products with zero weight, you can use the following code snippet:

/******Start of the code******/

<?php
include 'wp-load.php';
global $wpdb;
$srNo = 1;
$productWithNoTagsArr = array();
$productIdsResults = $wpdb->get_results("SELECT ID, post_title, post_modified, post_parent, post_type, meta_value as sku FROM ".$wpdb->prefix."posts as wp left join ".$wpdb->prefix."postmeta as wpm on wp.ID = wpm.post_id WHERE post_status='publish' AND (post_type='product' OR post_type='product_variation') AND wpm.meta_key='_sku'", ARRAY_A);

$prodWithoutWeightArr = array();

$allProductArray = array();
$prodFactoryObj = new WC_Product_Factory();
foreach ($productIdsResults as $productId) {
$prodId = $productId['ID'];
$postType = $productId['post_type'];
if($postType == 'product_variation' && $productId['post_parent'] != 0) {
$postParentProductArr[] = $productId['post_parent'];
}
$_product = $prodFactoryObj->get_product($prodId);
$weight = $_product->get_weight();
if(empty($weight)){
$prodWithoutWeightArray[] = $prodId;
}

$allProductArray[$prodId]['id'] = $prodId;
$allProductArray[$prodId]['name'] = $productId['post_title'];
$allProductArray[$prodId]['sku'] = $productId['sku'];
$allProductArray[$prodId]['last_updated'] = $productId['post_modified'];
}

$finalProducts = array_diff($prodWithoutWeightArray, $postParentProductArr);
?>
<h1 class="wp-heading-inline"><?php echo esc_html('Products With No Weight'); ?></h1>
<hr class="wp-header-end"><br>
<table id="products_with_no_tags" cellpadding="5" cellspacing="5" width="100%">
<thead>
<tr>
<th width="3%" style="text-align:center">Sr. No.</th>
<th width="9%" style="text-align:center">Product ID</th>
<th width="15%">SKU</th>
<th width="31%">Product Name</th>
<th width="8%" style="text-align:center;">Status</th>
<th style="text-align:center">Last Updated At</th>
</tr>
</thead>
<tbody>
<?php
if(count($finalProducts) > 0){
foreach ($finalProducts as $row) {
$entityID = $allProductArray[$row]['id'];
$prdName = $allProductArray[$row]['name'];
$sku = $allProductArray[$row]['sku'];
$status = "Published";
$lastActDt = $allProductArray[$row]['last_updated'];
?>
<tr>
<td style="text-align:center"><?php echo $srNo; ?></td>
<td style="text-align:center"><?php echo $entityID; ?></td>
<td style="text-align:center"><?php echo $sku; ?></td>
<td><?php echo $prdName; ?></td>
<td style="text-align:center"><?php echo $status; ?></td>
<td style="text-align:center"><?php echo date('m/d/Y H:i:s A', strtotime('-5 hours', strtotime($lastActDt))); ?> EST</td>
</tr>
<?php $srNo++;
}
} ?>
</tbody>
</table>

/******End of the code******/

That’s it! You are done.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s