wc_get_customer_available_downloads

Get customer available downloads.

Description

(array) wc_get_customer_available_downloads( (int) $customer_id ); 

Returns (array)

Parameters (1)

0. $customer_id (int)
The customer id.

Usage

  1. if ( !function_exists( 'wc_get_customer_available_downloads' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-user-functions.php'; 
  3.  
  4. // The customer id. 
  5. $customer_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_get_customer_available_downloads($customer_id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-user-functions.php  
  1. function wc_get_customer_available_downloads( $customer_id ) { 
  2. $downloads = array(); 
  3. $_product = null; 
  4. $order = null; 
  5. $file_number = 0; 
  6.  
  7. // Get results from valid orders only 
  8. $results = wc_get_customer_download_permissions( $customer_id ); 
  9.  
  10. if ( $results ) { 
  11. foreach ( $results as $result ) { 
  12. if ( ! $order || $order->get_id() != $result->order_id ) { 
  13. // new order 
  14. $order = wc_get_order( $result->order_id ); 
  15. $_product = null; 
  16.  
  17. // Make sure the order exists for this download 
  18. if ( ! $order ) { 
  19. continue; 
  20.  
  21. // Downloads permitted? 
  22. if ( ! $order->is_download_permitted() ) { 
  23. continue; 
  24.  
  25. $product_id = intval( $result->product_id ); 
  26.  
  27. if ( ! $_product || $_product->get_id() != $product_id ) { 
  28. // new product 
  29. $file_number = 0; 
  30. $_product = wc_get_product( $product_id ); 
  31.  
  32. // Check product exists and has the file 
  33. if ( ! $_product || ! $_product->exists() || ! $_product->has_file( $result->download_id ) ) { 
  34. continue; 
  35.  
  36. $download_file = $_product->get_file( $result->download_id ); 
  37.  
  38. // Download name will be 'Product Name' for products with a single downloadable file, and 'Product Name - File X' for products with multiple files. 
  39. $download_name = apply_filters( 
  40. 'woocommerce_downloadable_product_name',  
  41. $_product->get_name() . ' – ' . $download_file['name'],  
  42. $_product,  
  43. $result->download_id,  
  44. $file_number 
  45. ); 
  46.  
  47. $downloads[] = array( 
  48. 'download_url' => add_query_arg( 
  49. array( 
  50. 'download_file' => $product_id,  
  51. 'order' => $result->order_key,  
  52. 'email' => urlencode( $result->user_email ),  
  53. 'key' => $result->download_id,  
  54. ),  
  55. home_url( '/' ) 
  56. ),  
  57. 'download_id' => $result->download_id,  
  58. 'product_id' => $_product->get_id(),  
  59. 'product_name' => $_product->get_name(),  
  60. 'download_name' => $download_name,  
  61. 'order_id' => $order->get_id(),  
  62. 'order_key' => $order->get_order_key(),  
  63. 'downloads_remaining' => $result->downloads_remaining,  
  64. 'access_expires' => $result->access_expires,  
  65. 'file' => array( 
  66. 'name' => $download_file->get_name(),  
  67. 'file' => $download_file->get_file(),  
  68. ),  
  69. ); 
  70.  
  71. $file_number++; 
  72.  
  73. return apply_filters( 'woocommerce_customer_available_downloads', $downloads, $customer_id );