WC_Download_Handler::download PHP Méthode

download() public static méthode

Download a file - hook into init function.
public static download ( string $file_path, integer $product_id )
$file_path string URL to file
$product_id integer of the product being downloaded
    public static function download($file_path, $product_id)
    {
        if (!$file_path) {
            self::download_error(__('No file defined', 'woocommerce'));
        }
        $filename = basename($file_path);
        if (strstr($filename, '?')) {
            $filename = current(explode('?', $filename));
        }
        $filename = apply_filters('woocommerce_file_download_filename', $filename, $product_id);
        $file_download_method = apply_filters('woocommerce_file_download_method', get_option('woocommerce_file_download_method', 'force'), $product_id);
        // Add action to prevent issues in IE
        add_action('nocache_headers', array(__CLASS__, 'ie_nocache_headers_fix'));
        // Trigger download via one of the methods
        do_action('woocommerce_download_file_' . $file_download_method, $file_path, $filename);
    }

Usage Example

 /**
  * Admin voucher download, which will work regardless of the download
  * permissions/current user, and will not count towards the download
  * count
  *
  * @since 2.1
  */
 public function download_voucher()
 {
     if (isset($_GET['post']) && isset($_GET['product_id']) && isset($_GET['item_id']) && isset($_GET['action']) && 'download' == $_GET['action'] && $_GET['voucher_id']) {
         $order = wc_get_order($_GET['post']);
         $items = $order->get_items();
         $voucher = new WC_Voucher($_GET['voucher_id'], $_GET['post'], $items[$_GET['item_id']], $_GET['item_id']);
         $download_handler = new WC_Download_Handler();
         $file_path = $voucher->get_voucher_full_filename(WC_PDF_Product_Vouchers::get_uploads_path());
         if ('redirect' === get_option('woocommerce_file_download_method')) {
             $file_path = $voucher->convert_path_to_url($file_path);
         }
         $download_handler->download($file_path, $_GET['product_id']);
         exit;
     }
 }