All Resources

Most Useful Developer Hooks for Easy Digital Downloads

Besides being straightforward and lightweight, Easy Digital Downloads has, in my opinion, the most developer-friendly code of all the WordPress ecommerce plugin ecosystems. Here are the hooks I use the most when working on projects as a freelance Easy Digital Downloads developer.

Jump straight to the code examples, or look through the various action and filters below.

Purchase Actions

These actions can be used to send purchase data to 3rd party APIs, or integrate with other areas of the same site. Useful classes to look at when using these hooks are EDD_Payment, EDD_Customer, and EDD_Download.

Checkout Actions

Checkout actions can be used to output text on the checkout page (without editing template files), or can be used to control products being added to the cart as shown in the code example below.

  • edd_is_discount_valid
  • edd_purchase_form_before_submit
  • edd_before_checkout_cart
  • edd_pre_add_to_cart

Email and Receipt Content

Adding custom placeholder tags to emails and creating custom receipt content are powerful ways to extend EDD and control the checkout experience.

Defaults

Change the global Downloads name to Products, and prevent the new user email notification from being sent.

Code Examples

Use functions.php or a create a functionality plugin to utilize the code examples below.

<?php
/***
edd_add_email_tags
Add custom email tags for use in EDD email templates
***/
add_action('edd_add_email_tags', 'feti_edd_add_email_tags');
function drw_edd_add_email_tags() {
edd_add_email_tag('product_message', __('Custom product message','drw'), 'drw_product_message_email_tag');
edd_add_email_tag('product_message_2', __('Another product message','drw'), 'drw_product_message_2_email_tag');
}
//The {product_message} email tag
function drw_product_message_email_tag( $payment_id ) {
$payment_data = edd_get_payment_meta( $payment_id );
$email_tag_content = __('This is a custom message','drw');
return $email_tag_content;
}
//The {product_message_2} email tag
function drw_product_message_email_tag( $payment_id ) {
$email_tag_content = __('This is a another custom message','drw');
return $email_tag_content;
}
<?php
/***
edd_complete_purchase
$payment_id int ID of EDD payment object
Do something with payment data after purchase
https://docs.easydigitaldownloads.com/article/534-edd-complete-purchase
***/
add_action( 'edd_complete_purchase', 'drw_enroll_after_purchase' );
function drw_api_call_after_purchase( $payment_id ) {
$payment_meta = edd_get_payment_meta( $payment_id );
$cart_items = edd_get_payment_meta_cart_details( $payment_id );
$user_id = $payment_meta['user_info']['id'];
foreach ( $cart_items as $download ) {
//make API call
drw_update_email_api( $user_id, $download['id'] );
}
}
<?php
/***
edd_default_downloads_name
Change global Downloads name
***/
add_filter('edd_default_downloads_name', 'drw_edd_global_labels');
function drw_edd_global_labels($labels) {
$labels = array(
'singular' => __('Product','edd'),
'plural' => __('Products','edd')
);
return $labels;
}
//Update 'downloads' slug
define('EDD_SLUG', 'products');
<?php
/***
edd_insert_user
Remove new user notification emails in EDD
***/
remove_action( 'edd_insert_user', 'edd_new_user_notification', 10, 2 );
<?php
/***
edd_payment_receipt_before
$payment obj Current EDD_payment object
Output content in template before receipt is rendered
***/
add_action('edd_payment_receipt_before', 'drw_purchase_confirmation_text', 10, 2);
function drw_purchase_confirmation_text( $payment, $edd_receipt_args ) {
$payment = new EDD_Payment($payment->ID);
$downloads = $payment->downloads;
$gateway = $payment->gateway; //stripe //check
$gateway_text = ( 'stripe' == $gateway ) ? __('Credit Card', 'drw') : __('Check', 'drw');
printf('<div class="message">%s %s</div>', __('You have paid by'), $gateway_text);
}
<?php
/***
edd_pre_add_to_cart
$download_id int Product being added
Fires before new item is added to cart
Example: Allow a product to be added only once to cart
***/
add_action( 'edd_pre_add_to_cart', 'drw_edd_one_item_checkout', 10, 2 );
function drw_edd_one_item_checkout( $download_id, $options ) {
if ( edd_item_in_cart($download_id) ) {
$cart_contents = edd_get_cart_contents();
$cart_ids = array();
foreach( $cart_contents as $item) {
$cart_ids[] = $item['id'];
}
$cart_key = array_search ($download_id, $cart_ids);
edd_remove_from_cart( $cart_key);
set_query_var( 'edd_duplicate', 'duplicate' );
}
}
/***
edd_before_checkout_cart
Hook to output content directly before cart is rendered
Example: Set warning for multiple products if duplicate is set
***/
add_action( 'edd_before_checkout_cart', 'drw_edd_checkout_warnings' );
function drw_edd_checkout_warnings() {
if ( get_query_var('edd_duplicate') ) {
printf('<div class="alert">%s</div>', __('This item can only be purchased once', 'drw'));
}
}
<?php
/***
edd_recurring_update_subscription
$subscription_id int EDD_Subscription ID
Take action if Recurring Payments subscription status has changed
**/
add_action( 'edd_recurring_update_subscription', 'drw_subscription_update_callback', 10, 1);
function drw_subscription_update_callback( $subscription_id = null ) {
if ( null == $subscription_id ) return;
$subscription = new EDD_Subscription( $subscription_id );
$user_id = $subscription->customer->user_id;
//'pending', 'active', 'cancelled', 'expired', 'failing', 'completed'
$status = $subscription->get_status();
if ( 'cancelled' || 'expired' == $status ) {
// make API call
} elseif( 'active' == $status ) {
// make API call
}
}
<?php
/***
edd_update_payment_status
$payment_id int EDD_Payment object ID
$new_status str New payment status
$old_status str Old payment status
Take action after purchase status is changed – useful for check payments
***/
add_action( 'edd_update_payment_status', 'drw_edd_after_check_purchase', 10, 3);
function drw_edd_after_check_purchase( $payment_id, $new_status, $old_status ) {
//bail if the order isn't approved
if ( !in_array($new_status, array('publish', 'complete')) ) {
return;
}
$payment = new EDD_Payment( $payment_id );
$user_id = $payment->user_id;
//Do something with payment data
drw_update_data($user_id, $payment);
}
July 23, 2020 WordPress Development