Search code examples
woocommercehook-woocommercewoocommerce-rest-api

Woocommerce change order download files


I am trying to manipulate the download URL link for files inside an order.

Basically, I have a use case when a download product is bought I need to add files to the order to be able to download it. So I cannot add an order ID to a product because there is no order at that moment.

Point is, every downloadable product needs to have a dynamic link because it will be generated on the fly .

So how can I add data to the product download URL link ->

enter image description here


Solution

  • WooCommerce offers 2 filters that can be used to achieve what you are looking for:

    A) woocommerce_download_product_filepath used to specify what URL contains the file to be downloaded and it takes the following parameters:

    • @param string $file_path File path
    • @param string $email_address Email address
    • @param WC_Order|bool $order Order object or false
    • @param WC_Product $product Product object
    • @param WC_Customer_Download $download Download data

    And it is registered like this:

    add_filter( 'woocommerce_download_product_filepath', 'change_woocommerce_file_download_path', 10, 5 );

    More info here https://wp-kama.com/plugin/woocommerce/hook/woocommerce_download_product_filepath

    B) woocommerce_file_download_filename used to manipulate the downloaded file's name and it takes the following parameters:

    • @param string $filename Downloadable file name.
    • @param int $product_id WC product id

    And it is registered like this:

    add_filter( 'woocommerce_file_download_filename', 'change_woocommerce_file_download_filename', 10, 2 );

    More info here https://wp-kama.com/plugin/woocommerce/hook/woocommerce_file_download_filename