Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.