awesomemotive / edd-library

EDD Code Snippet Library
http://library.easydigitaldownloads.com/
141 stars 42 forks source link

Update prevent duplicate purchases #168

Open mihaijoldis opened 2 years ago

mihaijoldis commented 2 years ago

Based on an .org ticket https://wordpress.org/support/topic/plugin-code-from-edd-library-does-not-work/ we should update our snippet to allow Renewals and Upgrades if SL is being used. https://library.easydigitaldownloads.com/checkout/prevent-duplicate-purchases.html

Working code (updated) from @robincornett

function pw_edd_prevent_duplicate_purchase( $valid_data, $posted ) {

    $cart_contents = edd_get_cart_contents();
    foreach ( $cart_contents as $item ) {
        if ( ! empty( $item['options']['is_upgrade'] ) ) {
            continue;
        }
        if ( ! empty( $item['options']['is_renewal'] ) ) {
            continue;
        }
        if ( edd_has_user_purchased( get_current_user_id(), $item['id'] ) ) {
            edd_set_error( 'duplicate_item', 'You have already purchased this item so may not purchase it again. If you want to upgrade, click on My Licenses and select the next upgrade level!' );
        }
    }
}
add_action( 'edd_checkout_error_checks', 'pw_edd_prevent_duplicate_purchase', 10, 2 );