Created
June 4, 2021 14:14
-
-
Save Basilakis/ca0a5644791bfb66d7953f866a4f1e64 to your computer and use it in GitHub Desktop.
Easy Digital Downloads - Modify the purchase link for downloads that are free and have only 1 download file to allow direct download bypassing checkout.
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
/** | |
* Modify the purchase link for downloads that are free and have only 1 download file to allow direct download bypassing checkout. | |
*/ | |
add_filter( 'edd_purchase_download_form', 'isa_edd_purchase_form', 20, 2 ); | |
function isa_edd_purchase_form( $purchase_form, $args ) { | |
$download_id = absint( $args['download_id'] ); | |
if ( $download_id ) { | |
$price = floatval( edd_get_lowest_price_option( $download_id ) ); | |
// If not free, show the regular purchase button | |
if ( $price >= 0.001 ) { | |
return $purchase_form; | |
} | |
$files = edd_get_download_files( $download_id ); | |
// If there's more than download file, show the regular purchase button | |
if ( count( $files ) > 1 ) { | |
return $purchase_form; | |
} | |
// get first (only) file; NB: may not be index 0, so pull from front of array | |
$file = array_shift( $files ); | |
// If there's only 1 download file, show the Download button | |
if ( ! empty( $file['file'] ) ) { | |
$refresh_files = edd_get_download_files( $download_id ); | |
// get first file only, with its array key | |
$file_keys = array_keys($refresh_files); | |
$file_key = $file_keys[0]; | |
$file_data = $refresh_files[$file_key]; | |
$download_url = $file_data['file']; | |
$download_url = apply_filters('edd_requested_file', $download_url, $refresh_files, $file_key); | |
ob_start(); | |
?> | |
<a href="<?php echo esc_url($download_url); ?>" class="button"> | |
<span>Download</span> | |
</a> | |
<?php | |
$purchase_form = ob_get_clean(); | |
} else { | |
// There's no file, show no button | |
return false; | |
} | |
} | |
return $purchase_form; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment