Force download, fall-back to redirect, issue and (maybe) solution
-
Hi there,
I’ve tried to build a store with two different digital products:- pages with content secured using the “Page Restrict for WooCommerce” plugin
- media files uploaded to woocommerce_uploads folder.
WooCommerce products are downloadable & virtual, with links to pages and media files.
I use Apache2 without mods, so to protect media files I have to use “forced download” method and .htaccess.
But this method doesn’t work well with “Page Restrict”: if I “sell” a link to a page in the same site, the link doesn’t work.There is WC_Download_Handler::download_file_force function (includes\class-wc-download-handler.php) with the following logic implemented:
- convert link to a media file to physical path
- check if file exists and is readable to serve it
- if not, check wheter link leads to remote server AND “redirect fallback allowed” option is set and make redirection, if so.
“Fallback to redirection” setting only applies to remote links. What about linking to a page on the same site? “File not found” because it is not “remote_file”. But according to the guide (https://woocommerce.com/document/digital-downloadable-product-handling/): “if the “Force Download” (…) does not work, the system will use “Redirect” method as last resort”. It should do it also for links to pages, but it does not = seems to be a bug.
For my purposes I’ve changed one line:
if ( $parsed_file_path[‘remote_file’] && ‘yes’ === get_option( ‘woocommerce_downloads_redirect_fallback_allowed’ ) ) {
to
if ( $parsed_file_path[‘remote_file’] || ‘yes’ === get_option( ‘woocommerce_downloads_redirect_fallback_allowed’ ) ) {After this modification, the logic is: check if the user is trying to download a local file, redirect if the file is not physically available AND (the link directs to a remote server OR the “Fallback to redirect” option is set).
I found several threads regarding the handling of the “Fallback to redirect” and “Download method” options introduced in 5.x. with similar problems described. After many years of programming my experience is, use of AND instead of OR is quite common mistake, I’ve made it plenty of times …
Please consider including my modification in your code as a fix for a bug that appears in really rarely configuration.Thanks,
Maciek
- The topic ‘Force download, fall-back to redirect, issue and (maybe) solution’ is closed to new replies.