Skip to content

Instantly share code, notes, and snippets.

@dryan1144
Last active November 27, 2020 21:49
Show Gist options
  • Save dryan1144/2c50949dbf227ed6cdd6bf98b3b80ae6 to your computer and use it in GitHub Desktop.
Save dryan1144/2c50949dbf227ed6cdd6bf98b3b80ae6 to your computer and use it in GitHub Desktop.
Easy Digital Downloads Developer Hooks
<?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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment