Skip to content

Report: Galleries with no orders

This code will add a new custom menu item under "Sunshine" in your WordPress admin and show you all the galleries which have not yet had a purchase made.

Code generously created and provided by Jesse Anthony of Swesk

add_action('admin_menu', 'add_no_orders_report_with_email');
function add_no_orders_report_with_email() {

// Ensure both admin and sunshine manager roles see the menu
$capability = 'manage_options'; // Default for admin
if (current_user_can('sunshine_manager')) {
$capability = 'sunshine_manager';
}

add_submenu_page(
'edit.php?post_type=sunshine-gallery',
__('Galleries Without Orders', 'sunshine-photo-cart'),
__('No Orders Report', 'sunshine-photo-cart'),
$capability,
'sunshine-galleries-no-orders',
'render_no_orders_report_with_email'
);
}

function render_no_orders_report_with_email() {
global $wpdb;

// Fetch galleries without orders
$query = "
SELECT g.ID, g.post_title
FROM {$wpdb->posts} g
LEFT JOIN {$wpdb->prefix}sunshine_order_items oi
ON g.ID = oi.gallery_id
WHERE g.post_type = 'sunshine-gallery'
AND g.post_status = 'publish'
AND oi.gallery_id IS NULL
";
$galleries = $wpdb->get_results($query);

// Count total galleries without orders
$total_galleries = count($galleries);

?>
<div class="wrap">
<h1><?php _e('Galleries Without Orders', 'sunshine-photo-cart'); ?></h1>
<p><strong><?php _e('Total Galleries Without Orders:', 'sunshine-photo-cart'); ?></strong> <?php echo esc_html($total_galleries); ?></p>
<table class="widefat fixed striped">
<thead>
<tr>
<th><?php _e('Gallery Name', 'sunshine-photo-cart'); ?></th>
<th><?php _e('Customer Email(s)', 'sunshine-photo-cart'); ?></th>
<th><?php _e('Edit Link', 'sunshine-photo-cart'); ?></th>
</tr>
</thead>
<tbody>
<?php if (!empty($galleries)) : ?>
<?php foreach ($galleries as $gallery) : ?>
<tr>
<td><?php echo esc_html($gallery->post_title); ?></td>
<td>
<?php
// Fetch customer IDs associated with the gallery
$user_ids = get_post_meta($gallery->ID, 'private_users', true);
if (!empty($user_ids) && is_array($user_ids)) {
foreach ($user_ids as $user_id) {
$user = get_user_by('id', $user_id);
if ($user) {
echo esc_html($user->user_email) . '<br>';
}
}
} else {
_e('No emails associated', 'sunshine-photo-cart');
}
?>
</td>
<td>
<a href="<?php echo admin_url('post.php?action=edit&post=' . $gallery->ID); ?>">
<?php _e('Edit Gallery', 'sunshine-photo-cart'); ?>
</a>
</td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="3"><?php _e('No galleries without orders found.', 'sunshine-photo-cart'); ?></td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<?php
}

Learn how to add this custom code to your WordPress website

Still need help?

If you have not yet found your answer in the documentation articles, please contact support

Sunshine Photo Cart for WordPress