Add this code in your functions.php and change the meta key (‘custom_meta_key’) and row title (‘Meta’) to your liking:
if( ! function_exists('wpcl_post_class_meta_box') ) {
function wpcl_post_class_meta_box( $object, $box ) {
global $post, $wpdb;
$post_id = $post->ID;
$wpcl_orders = '';
$columns = array(
__('Order', 'wc-product-customer-list'),
__('First name', 'wc-product-customer-list'),
__('Last name','wc-product-customer-list'),
__('E-mail', 'wc-product-customer-list'),
__('Phone', 'wc-product-customer-list'),
__('Qty', 'wc-product-customer-list'),
'Meta'
);
$customerquery = "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_itemmeta woim
LEFT JOIN {$wpdb->prefix}woocommerce_order_items oi
ON woim.order_item_id = oi.order_item_id
WHERE meta_key = '_product_id' AND meta_value = %d
GROUP BY order_id;";
$order_ids = $wpdb->get_col( $wpdb->prepare( $customerquery, $post_id ) );
if( $order_ids ) {
$args = array(
'post_type' =>'shop_order',
'post__in' => $order_ids,
'posts_per_page' => 999,
'order' => 'ASC',
'post_status' => array( 'wc-processing', 'wc-completed' )
);
$wpcl_orders = new WP_Query( $args );
}
?>
<div id="postcustomstuff" class="wpcl">
<?php if($wpcl_orders) { ?>
<table id="list-table">
<thead>
<tr>
<?php foreach($columns as $column) { ?>
<th>
<strong><?php echo $column; ?></strong>
</th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$emaillist = '';
$productcount = array();
$items = array();
$ordercount = $wpcl_orders->found_posts;
foreach($wpcl_orders->posts as $wpcl_order) {
$order = new WC_Order($wpcl_order->ID);
?>
<tr>
<td>
<p>
<?php if ($order->id) { echo '<a href="' . esc_url( home_url('/' ) ) . 'wp-admin/post.php?post=' . $order->id . '&action=edit" target="_blank">' . $order->id . '</a>'; $items['order-id'] = $order->id; } ?>
</p>
</td>
<td>
<p>
<?php if ($order->billing_first_name) { echo $order->billing_first_name; $items['first-name'] = $order->billing_first_name; } ?>
</p>
</td>
<td>
<p>
<?php if ($order->billing_last_name) { echo $order->billing_last_name; $items['last-name'] = $order->billing_last_name; } ?>
</p>
</td>
<td>
<p>
<?php if ($order->billing_email) { echo '<a href="mailto:' . $order->billing_email . '">' . $order->billing_email . '</a>'; $items['billing-email'] = $order->billing_email; } ?>
</p>
</td>
<td>
<p>
<?php if ($order->billing_phone) { echo '<a href="tel:' . $order->billing_phone . '">' . $order->billing_phone . '</a>'; $items['billing-phone'] = $order->billing_phone; } ?>
</p>
</td>
<td>
<p>
<?php if (sizeof($order->get_items())>0) { $singlecount = ''; foreach($order->get_items() as $item) { if( $item['product_id'] == $post_id ) { $productcount[] = $item['qty']; $singlecount+= $item['qty']; } } $items['order-qty'] = $singlecount; echo $singlecount; } ?>
</p>
</td>
<td>
<p><?php $custom_meta = get_post_meta($order->id, 'custom_meta_key', true); if($custom_meta) { echo $custom_meta; } ?></p>
</td>
</tr>
<?php
$emaillist .= $order->billing_email;
if($i < $ordercount) {
$emaillist .= ',';
}
$i++;
$export[] = $items;
}
wp_reset_query();
?>
</tbody>
</table>
<p class="total">
<?php echo '<strong>' . __('Total', 'wc-product-customer-list') . ' : </strong>' . array_sum($productcount); ?>
</p>
<a href="mailto:?bcc=<?php echo $emaillist; ?>" class="button"><?php _e('Email all customers', 'wc-product-customer-list'); ?></a>
<?php
// Store data in sessions to output csv/html
$_SESSION["export"] = $export;
$_SESSION["columns"] = $columns;
$_SESSION["post-title"] = get_the_title($post_id);
$_SESSION["slug"] = get_post($post_id)->post_name;
// Export to CSV
echo '<a href="' . plugin_dir_url( __FILE__ ) . 'output/export-csv.php" target="_blank" class="button button-second">' . __('Export to CSV', 'wc-product-customer-list') . '</a>';
// Print dialog
echo '<a href="' . plugin_dir_url( __FILE__ ) . 'output/print.php" target="_blank" class="button">' . __('Print', 'wc-product-customer-list') . '</a>';
} else {
_e('This product currently has no customers', 'wc-product-customer-list');
}
?>
</div>
<?php
}
}