Skip to content

Instantly share code, notes, and snippets.

@cklosowski
Last active August 29, 2015 14:14
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 cklosowski/00ac09551933912137af to your computer and use it in GitHub Desktop.
Save cklosowski/00ac09551933912137af to your computer and use it in GitHub Desktop.
See if a commenter has purchased a given download ID
<?php
/*
* Plugin Name: Easy Digital Downloads - Commenter has purchased
* Description: Adds a 'Purchased' tag after a commenter who's purchased a download
* Author: Chris Klosowski
* Version: 1.0
*/
/**
* NOTE: This is currently not very performant. If an email address has a lot
* of purchases, this will take some time to crunch. It should be used sparingly.
*/
/*
* If the commenter has purchased the product being referenced, return true
*/
function ck_edd_commenter_has_purcahsed( $commenter_email, $download_id ) {
$customer = EDD()->customers->get_by( 'email', $commenter_email );
$payment_ids = explode( ',', $customer->payment_ids );
// Setup an array to capture all the downloads this email address has purchased
$purchased_download_ids = array();
foreach ( $payment_ids as $payment_id ) {
$payment_downloads = edd_get_payment_meta_downloads( $payment_id );
if ( ! is_array( $payment_downloads ) ) {
continue; // May not be an array due to some very old payments, move along
}
foreach ( $payment_downloads as $download ) {
$purchased_download_ids[] = $download['id'];
}
}
$unique_downloads = array_unique( $purchased_download_ids );
return in_array( $download_id, $unique_downloads );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment