Last active
September 9, 2015 19:18
-
-
Save SeanChDavis/0ec87e37e5260dabe5b0 to your computer and use it in GitHub Desktop.
EDD Replace Purchase Form w/ Download Link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // DO NOT COPY THIS LINE | |
function ck_edd_user_download_button( $purchase_form, $args ) { | |
if ( !is_user_logged_in() ) | |
return $purchase_form; | |
$download_id = (string)$args['download_id']; | |
$current_user_id = get_current_user_id(); | |
// If the user has purchased this item, itterate through their purchases to get the specific | |
// purchase data and pull out the key and email associated with it. This is necessary for the | |
// generation of the download link | |
if ( edd_has_user_purchased( $current_user_id, $download_id, $variable_price_id = null ) ) { | |
$user_purchases = edd_get_users_purchases( $current_user_id, -1, false, 'complete' ); | |
foreach ( $user_purchases as $purchase ) { | |
$cart_items = edd_get_payment_meta_cart_details( $purchase->ID ); | |
$item_ids = wp_list_pluck( $cart_items, 'id' ); | |
if ( in_array( $download_id, $item_ids ) ) { | |
$email = edd_get_payment_user_email( $purchase->ID ); | |
$payment_key = edd_get_payment_key( $purchase->ID ); | |
} | |
} | |
// Attempt to get the file data associated with this download | |
$download_data = edd_get_download_files( $download_id, null ); | |
if ( $download_data ) { | |
// Setup the style and colors associated with the settings | |
global $edd_options; | |
$style = isset( $edd_options['button_style'] ) ? $edd_options['button_style'] : 'button'; | |
$color = isset( $edd_options['checkout_color'] ) ? $edd_options['checkout_color'] : 'blue'; | |
$new_purchase_form = ''; | |
foreach ( $download_data as $filekey => $file ) { | |
// Generate the file URL and then make a link to it | |
$file_url = edd_get_download_file_url( $payment_key, $email, $filekey, $download_id, null ); | |
$new_purchase_form .= '<a href="' . $file_url . '" class="' . $style . ' ' . $color . ' edd-submit"><span class="edd-add-to-cart-label">Download ' . $file['name'] . '</span></a> '; | |
} | |
} | |
// As long as we ended up with links to show, use them. | |
if ( !empty( $new_purchase_form ) ) | |
$purchase_form = '<h4>' . __( 'You already own this product. Download it now:', 'edd' ) . '</h4>' . $new_purchase_form; | |
} | |
return $purchase_form; | |
} | |
add_filter( 'edd_purchase_download_form', 'ck_edd_user_download_button', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello and good day.
Thank you very much for this script! It works great for individual sold products. My question is can we make it work in case a customer purchased a subscription bundle?
For example on my store I sell the products individually or they can get them all with a yearly membership. If they buy a membership it would be nice to have this script recognize them so they can download their products as well just like individual sales.
Please let me know what you think and have an amazing day! :)