Stop Customers Buying the Same Download Twice
Replaces the purchase button with a "You already own this" link for logged-in customers who bought a download before, and blocks repeat purchases at checkout.
Opens WPIDE on
Code
add_filter( 'edd_purchase_download_form', function ( $form, $args ) {
$download_id = (int) ( $args['download_id'] ?? 0 );
if ( ! $download_id || ! is_user_logged_in() || ! edd_has_user_purchased( get_current_user_id(), $download_id ) ) {
return $form;
}
$history = edd_get_option( 'purchase_history_page' );
return sprintf(
'<p class="wpide-edd-owned">%1$s%2$s</p>',
esc_html( 'You already own this.' ),
$history ? ' <a href="' . esc_url( get_permalink( $history ) ) . '">' . esc_html( 'Go to your downloads' ) . '</a>' : ''
);
}, 10, 2 );
add_action( 'edd_checkout_error_checks', function () {
if ( ! is_user_logged_in() ) {
return;
}
foreach ( (array) edd_get_cart_contents() as $item ) {
if ( edd_has_user_purchased( get_current_user_id(), $item['id'] ) ) {
edd_set_error( 'wpide_already_purchased', sprintf( 'You already own %s. Please remove it from your cart.', get_the_title( $item['id'] ) ) );
return;
}
}
} );
Notes
Works for logged-in customers, and downloads with variable prices are checked as a whole.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.