Snippets

Visser Labs Modifying the download filepath of downloadable files

Created by Michael Visser

File functions.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+
+// Force WooCommerce to believe that the downloadable file exists
+add_filter( 'woocommerce_downloadable_file_exists', '__return_true' );
+
+// Intercept the downloadable file as it is being returned to the customer and return something else
+function custom_override_product_file_download_path( $file_path, $object, $download_id ) {
+
+
+	// Fetch the downloadable files associated with this download
+	$files = $object->get_files();
+
+	// Bail out if the download ID does not match the files we requested
+	if( !isset( $files[ $download_id ] ) )
+		return $file_path;
+
+	// The electronic download files are saved outside of WordPress’s reach
+	$downloads_directory = '/opt/bitnami/apps/wordpress/downloads/'
+
+	// Check if the directory we are looking for exists
+	if( !is_dir( $downloads_directory . $file_path ) )
+		return $file_path;
+
+	// Check if the file that we are looking for exists
+	if( file_exists( $downloads_directory . $file_path . '.zip' ) ) {
+
+	// Set an updated file path to return to the customer
+	$file_path = $downloads_directory . $file_path . '.zip';
+	
+	} else {
+	
+    	// Build a new ZIP archive in this location
+
+		// Set an updated file path to return to the customer
+		$file_path = $downloads_directory . $file_path . '.zip';
+
+    }
+
+	return $file_path;
+
+}
+add_filter( 'woocommerce_product_file_download_path', 'custom_override_product_file_download_path', 10, 3 );
+?>
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.