How can I set an "mobile deep link" to downloadable file from woocommerce?
Asked Answered
I

3

5

I have woocommerce setup up on a wordpress site for downloading PDF files. After a user goes through the checkout process and is ready to download the file, I'd like to check to see if they are on a mobile device or not. If the user is not on a mobile device, the download link would just be normal.

If the user is on a mobile device, I'd like to set the download link to be an "app deep link" using a custom URI scheme (from an existing vendor). For example, instead of the download link referencing http:// or https://, I'd like the download link to be appname://domain.com/folder/filename.pdf. This way, the PDF would automatically open in my app when they clicked on the link. The app vendor already support the custom URI schema, so I am just looking for "how" to set the link dynamically in WordPress/Woocommerce.

How would I go about:

  1. detecting if the user is on a mobile device, and
  2. changing the URI scheme if they are a mobile user?

The concept I am trying to accomplish is described here in the "Native App" example: http://www.mobiloitte.com/blog/deep-linking-101

and here: https://en.wikipedia.org/wiki/Mobile_deep_linking

Itagaki answered 27/3, 2018 at 4:31 Comment(2)
What about mobile users who don't have your app? I would suggest to adapt the app, so it claims your https://example.com/folder download links, instead of using a custom URI scheme. This should be possible on both iOS and Android.Abrego
That's a very good point, but in my particular case, the QR code provides a convenient way for uses to upload the PDF into a mapping application.Itagaki
M
3

The following code runs before any type of download method, checking if it is mobile and replacing http or https in the download URL with appname, and then finally redirecting to the new URL. In this case, the final URL should be like:

appname://domain.com/folder/filename.pdf

Copy the following code into functions.php:

add_action( 'woocommerce_download_file_redirect', 'change_download_link_on_mobile' , 1, 2 );
add_action( 'woocommerce_download_file_xsendfile', 'change_download_link_on_mobile', 1, 2 );
add_action( 'woocommerce_download_file_force', 'change_download_link_on_mobile' , 1, 2 );

function change_download_link_on_mobile( $file_path, $filename = '' ){
    if( wp_is_mobile () ){
        $file_path  = preg_replace( '/http|https/','appname', $file_path ); // Replace http|https with appname 
        header( 'Location: ' . $file_path );
        exit();
    }
}
Millen answered 3/5, 2018 at 15:51 Comment(4)
Can you give some explanation as well?Epode
this code run before any type of download method and check if is mobile then replace 'http' or 'https' in download url with 'appname' and finally redirect user to new url . in your case final url shoud be like that : appname://domain.com/folder/filename.pdfMillen
@MahdiAkrami, this code appears is so close, but isn't quite working for downloading on the mobile device. It works fine for either downloading from a laptop, or when the mobile browser is in "desktop mode". I tried setting the appname to http for testing the mobile link, but that did not work, either.Itagaki
After further research, the regex syntax wasn't working correctly. I've updated the code snippet with working syntax for replacing multiple strings. Thank you!Itagaki
B
4

For your first question, trying using javascript window.navigator.userAgent or with PHP try using $_SERVER['HTTP_USER_AGENT']. This should work independent of Wordpress or other site details. See related questions here and here.

For your second question, deep linking is used for individual mobile apps, right? The answer probably depends on which app you're targeting. If it's an existing app, you might get the best traction contacting the app developer. If you're writing a new app, there is information for android here and for iOS here. If you want a permalink URL that refers to specific content on your Wordpress page (like a specific transaction event and user name) then you can find information about permalinks and query strings with WordPress, for example here.

Bethune answered 27/4, 2018 at 5:38 Comment(1)
I am using an existing app from a vendor that already supports a deep link schema, so I will give your suggestions for identifying mobile users and setting the link dynamically. Thank you.Itagaki
H
4

Firstly, to see if you're on a mobile platform, WP has a function for that: wp_is_mobile (see Codex page).

Then, if you look at the source of the class-wc-download-handler.php file (see here), at line 177 is a download( $file_path, $product_id ) function. This, at line 188 generates the download path using apply_filters( 'woocommerce_file_download_filename', $filename, $product_id ) so, in theory, you should be able to hook into that and use a regex to replace http with your deep link uri.

Hope that helps

Hellish answered 30/4, 2018 at 13:44 Comment(1)
Thank you, I'll definitely read more about these two options.Itagaki
M
3

The following code runs before any type of download method, checking if it is mobile and replacing http or https in the download URL with appname, and then finally redirecting to the new URL. In this case, the final URL should be like:

appname://domain.com/folder/filename.pdf

Copy the following code into functions.php:

add_action( 'woocommerce_download_file_redirect', 'change_download_link_on_mobile' , 1, 2 );
add_action( 'woocommerce_download_file_xsendfile', 'change_download_link_on_mobile', 1, 2 );
add_action( 'woocommerce_download_file_force', 'change_download_link_on_mobile' , 1, 2 );

function change_download_link_on_mobile( $file_path, $filename = '' ){
    if( wp_is_mobile () ){
        $file_path  = preg_replace( '/http|https/','appname', $file_path ); // Replace http|https with appname 
        header( 'Location: ' . $file_path );
        exit();
    }
}
Millen answered 3/5, 2018 at 15:51 Comment(4)
Can you give some explanation as well?Epode
this code run before any type of download method and check if is mobile then replace 'http' or 'https' in download url with 'appname' and finally redirect user to new url . in your case final url shoud be like that : appname://domain.com/folder/filename.pdfMillen
@MahdiAkrami, this code appears is so close, but isn't quite working for downloading on the mobile device. It works fine for either downloading from a laptop, or when the mobile browser is in "desktop mode". I tried setting the appname to http for testing the mobile link, but that did not work, either.Itagaki
After further research, the regex syntax wasn't working correctly. I've updated the code snippet with working syntax for replacing multiple strings. Thank you!Itagaki

© 2022 - 2024 — McMap. All rights reserved.