While writing my WooCommerce Premium Plugin System (Woops) I was having issues finding simple documentation on the hooks and filters I needed to build my plugin. This is where I’ve stored my “working papers”. The cheat sheet and notes about the stuff I need to remember or want to track while building the plugin. It may help some other devs build their own WooCommerce add-ons as well.
My account has a different mechanism to list downloads than viewing order details.
When a product has a download attached to it a unique download ID is assigned. This download ID is attached to the order via a unique download_id that is tracked in the woocommerce_downloadable_product_permissions table.
Q1) If the product download file is changed to a different file, will the user be able to download the new file or just the original version of the file?
Simple Answer) It will allow downloads to the new file.
A1) If you use a static URL like they will always be able to get the latest version of the download whenever the source download.zip is updated. The download_id and all records in the woocommerce_downloadable_product_permissions table remain static.
A2) When changing the download file by editing a product and choosing “upload file” the content of the file paths will change. When this changes WooCommerce copies all records in the woocommerce_downloadable_product_permissions table that have the old download ID to add a new record with a new download ID, in essence doubling all the download records for download orders for that product ID. This happens in the writepanels-init.php via the woocommerce_process_product_file_download_paths() method.
I) NOT changing the upload file by editing the product and choosing a new download file is far more efficient. For the reason noted in A2.
II) woocommerce_process_product_file_download_paths() uses the woocommerce_file_download_paths filter to modify the EXISTING file path(s) for a product. That is the content of the file path(s) field on the product BEFORE editing/saving the product. Thus you can only make it appear as there was no file prior to adding a new one, however the mechanism that duplicates the records in A2) above does an array diff to determine what records to change.
III) To disable the duplicate download copies the action woocommerce_process_product_file_download_paths() needs to be overridden.
Updating Product Download Files Methodology
When a product is updated via the admin panel and the file paths (downloads) entry is changed, WooCommerce runs through the writepanel-* procedural code. Buried deep in that nest of code is the woocommerce_process_product_file_download_paths interface.
I) If you prevent the automatic update of the downloadable_product_permissions table via a remove_action() call then the users will lose access to their downloads when a new download file comes online. Using this method to remove access to newer downloads means a new method must be implemented to allow access to older versions of the files.
In version 2.X of WooCommerce some of the hooks and filters to manipulate the process includes:
woocommerce_process_product_file_download_path() – called from the writepanel-product-type-variable and writepanel-product_data procedural code. Also called from the product->has_file() call, which is used to build the list of available downloads.
woocommerce_process_product_file_download_paths() – called from the writepanel-product-type-variable and writepanel-product_data procedural code. Also called from the product->has_file() call, which is used to build the list of available downloads. Called FIRST in the has_file() method which sets a variable that is the first parameter in the download_path() (singular) filter noted above.
My Account Download List Methodology
My account uses the WC_Customer.get_downloadable_products() method. This method loops over the woocommerce_downloadable_products_permissions table and iterates once for each order_id, product_id, download_id combination.
In version 2.X of WooCommerce this only has 3 potential hooks or filters to manipulate the results:
woocommerce_order_is_download_permitted – called once for each order being processed in the get_downloadable_products method
woocommerce_downloadable_product_name – called multiple times while building the downloads stack
woocommerce_customer_get_downloadable_products – called at exit of get_downloadable_products method
Hooks and Filters
My unofficial documentation on WooCommerce version 2.x hooks and filters that I need to manipulate the product downloads and related information.
A simple filter that determines if a current order is allowed to download stuff.
parameters:  - boolean based on current download permission  - the current WC_Order object
Gets the “stack” of downloads available to the user, shown on the “my account” page. Downloads is a named array:
Properties: download_url download_id product_id download_name - filtered per woocommerce_downloadable_product_name order_id order_key downloads_remaining
Parameters:  - fully qualified URL of the download file  - the product ID  - the download id, the MD5 style key in the woocommerce_downloadable_product_permissions table
This is called whenever a file is requested directly from a URL, as in when a user clicks a download link on their order. The file path can be modified by using this filter.
Parameters:  - stack of file paths, the key is an MD5 key to downloadable_product_permissions table, value is the file path  - the product ID  - the order ID (null for the product->has_file method)  - the item (null for the product->has_file method)
Called at the START of the product->has_file method, which then gets tested before setting file_path for the woocommerce_file_download_path (singular) filter noted above.
Parameters:  - post_id  - variation id (or 0)  - file paths stack
This action manages the updates to the downloadable product permissions table. It is called by the writepanel-product-type-variable and writepanel-product_data processors via the do_action WordPress function. Both procedural scripts fire the writepanels-init function of the same name.
Parameters:  - Title + file Number  - Product object  - Download ID  - File Number
Used to set the download name that renders when a user views “my account”.