Skip to content

Advanced info on free downloads in a gallery

This custom code tracks users who have downloaded a free image. It will show you a list of their email address and which images they downloaded. This relies on having the Digital Downloads and Analytics add-on enabled on your site.

add_action( 'sunshine_download_free_image', 'sunshine_customer_track_download_free_image_emails' );
function sunshine_customer_track_download_free_image_emails( $image ) {
    global $current_user;

    if ( is_user_logged_in() ) {
        $email = $current_user->user_email;
    } else {
        $email = SPC()->session->get( 'email' );
    }
    if ( $email ) {
        $events = $image->gallery->get_meta_value( 'free_download_events' );
        if ( empty( $entries ) ) {
            $events = array();
        }
        $events[] = array(
            'email'    => $email,
            'image_id' => $image->get_id(),
        );
        $image->gallery->update_meta_value( 'free_download_events', $events );
    }

}

add_action( 'sunshine_meta_gallery_analytics_display', 'sunshine_customer_track_download_free_image_emails_display', 99 );
function sunshine_customer_track_download_free_image_emails_display() {
    global $post;

    $gallery = sunshine_get_gallery( $post->ID );
    $events  = $gallery->get_meta_value( 'free_download_events' );
    if ( ! empty( $events ) ) {
        echo '<p><strong>' . __( 'Free Downloads', 'sunshine-photo-cart' ) . '</strong></p>';
        echo '<ul>';
        $downloads = array();
        foreach ( $events as $event ) {
            $image = sunshine_get_image( $event['image_id'] );
            if ( empty( $downloads[ $event['email'] ] ) ) {
                $downloads[ $event['email'] ] = array();
            }
            $downloads[ $event['email'] ][] = $image->get_name();
        }
        foreach ( $downloads as $email => $images ) {
            echo '<li>' . $email . ': ' . join( ', ', $images ) . '</li>';
        }
        echo '</ul>';
    }
}

Learn how to add this custom code to your WordPress website

When editing a gallery, you can view the Analytics tab and find the additional data:

Still need help?

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

Sunshine Photo Cart for WordPress