Skip to content

Instantly share code, notes, and snippets.

@mwhiteley16
Created July 23, 2018 18:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwhiteley16/460d3978d8c60177bdbded943bbfcc26 to your computer and use it in GitHub Desktop.
Save mwhiteley16/460d3978d8c60177bdbded943bbfcc26 to your computer and use it in GitHub Desktop.
EDD One Item Per Customer
// Prevent checkout if offer already purchased
add_filter( 'edd_can_checkout', 'wd_limit_purchase' );
function wd_limit_purchase(){
$user_id = get_current_user_id(); // Get the current customer's ID
$downloads = edd_get_users_purchased_products( $user_id );// Get all of the products the customer has purchased in the past
$can_checkout = true; // Allow checkout for now
if ( ! empty( $downloads ) ){ // If the customer hasn't purchased anything before we don't need to go any further.
foreach ( $downloads as $download ){ // Loop through each product the customer has purchased before.
$download_id = $download->ID;
$in_cart = edd_item_in_cart( $download_id );
if( $in_cart == true ) {
$can_checkout = false;
}
}
}
return $can_checkout;
}
@joelacevedor
Copy link

Hi @MWhiteley,

I got here from scottdeluzio.com post about this.

I was wondering if you know how to make this code to check all the downloads on a specific category, so every time I add a download to that category I don't have to edit the code with new ID's?

Thanks
All the best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment