Retrieving a download's payment IDs using EDD's Logging Class
The logging class in Easy Digital Downloads was first introduced in v1.3.1 as a means for logging events and errors. In this tutorial, we're going to use Easy Digital Downloads' Logging Class to retrieve each payment ID associated with a download's purchase. Once you have the payment ID, you have access to a wealth of information about the purchase, which you can use for practical applications.
Once such example is EDD Purchase Gravatars, which displays the gravatars of customers who have purchased your product. This can be used for showing social proof/trust, where customers are more inclined to purchase your product when they see other real customers who have already purchased.
The plugin uses the customer's email address and first name which are required by Easy Digital Downloads to complete the purchase. Using WordPress' get_avatar() function, the plugin retrieves the gravatar associated with the given email address, and uses the customer's first name for the image's alt tag.
The sales log
In order to retrieve the customer's email address and first name for the gravatars, we ultimately need the payment ID for each purchase. This can be found by first retrieving each purchase's log ID. We can see a list of all a download's purchases by clicking "View Sales Log" on the "download stats" metabox on the download's edit/publish screen.
From the image below, we can see one purchase has a Log ID of 55. In the same row, we can see it has a payment ID of 54. Each time a purchase is made, the Log ID and payment ID are stored at the same time, sequentially.
Digging into wp_postmeta
To understand how we can get the payment ID from the log ID, we need to dig into WordPress' wp_postmeta table. If we filter the table by a post_id of 55 (log ID), we find the _edd_log_payment_id meta_key with a value of 54 (our payment ID). This also matches the sales log, illustrated in the image above.
Filtering by post_id with our newly discovered payment ID (54), we find all the useful information stored against our purchase. In EDD Purchase Gravatars we use the email stored in the _edd_payment_user_email meta_key, and the first name which stored inside the _edd_payment_meta array (there's also an email address inside here also). In order to make use of all the information stored in the _edd_payment_meta array, you will first need to unserialize it, using the maybe_unserialize function in WordPress.
Retrieving the Log IDs
Now that we know we need to first retrieve all the Log ID's of a download, we'll use the helpful logging class in Easy Digital Downloads to do this for us. This function requires a download ID to be passed to it, which in the case of EDD Purchase Gravatars, allow the gravatars to be shown anywhere on the website, for any download.
Retrieving the Payment IDs
Now that we have an array of all the log IDs, we can use them in a new function to retrieve an array of all the payment IDs.
Now that you have an array of all the payment IDs, you can loop and output the information you need. What else can you build using payment IDs and EDD's Logging Class?