Immediate Download Instead of Add To Cart For Free Downloads in Easy Digital Downloads

This snippet is for Easy Digital Downloads. It modifies the purchase (or Add to Cart) button for downloads that are free, to allow direct download, bypassing the checkout process. So, instead of an “Add to Cart” or “Purchase” button, free downloads will have a “Download” button that will directly download the item.

This will only affect downloads that are free and have only 1 download file attached to them. If you want this to affect all free downloads regardless of the number of files attached, then remove lines 21 — 24, below.

Also, the download button will not work if you upload the free download file through the Edit Download page. For this to work, the file for the free download has to be uploaded via your Media Library, or it has to be somewhere accessible (i.e. GitHub). Enter the File URL in the Download Files meta box.

/**
 * 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;
}

See more:

We've 5 Responses

  1. September 24th, 2021 at 7:54 am

    Hi Isabel,
    thank you for the snippets. Does exist a method to show the free downloaded file into order details account page?

    If I download a free resource with this snippets, the dowload does not show on account page.

    Francesco

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]