Skip to content

Instantly share code, notes, and snippets.

@SeanChDavis
Created November 8, 2015 21:16
Show Gist options
  • Save SeanChDavis/bbedcaf76a93c75de010 to your computer and use it in GitHub Desktop.
Save SeanChDavis/bbedcaf76a93c75de010 to your computer and use it in GitHub Desktop.
EDD Already Purchased Item(s)
<?php // DO NOT COPY THIS LINE
/**
* Prevents logged-in customers from purchasing an item twice
*/
function pw_edd_prevent_duplicate_purchase( $valid_data, $posted ) {
$cart_contents = edd_get_cart_contents();
foreach( $cart_contents as $item ) {
if( edd_has_user_purchased( get_current_user_id(), $item['id'] ) ) {
edd_set_error( 'duplicate_item', 'You have one or more items in the cart that have already been purchased.' );
}
}
}
add_action( 'edd_checkout_error_checks', 'pw_edd_prevent_duplicate_purchase', 10, 2 );
/**
* Add notice next to each item in cart that has been purchased
*/
function sd_already_purchased_notice( $item ) {
if ( edd_has_user_purchased( get_current_user_id(), $item['id'] ) ) {
echo ' <small>(already purchased)</small>';
}
}
add_action( 'edd_checkout_cart_item_title_after', 'sd_already_purchased_notice' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment