wc_downloadable_file_permission

Grant downloadable product access to the file identified by $download_id.

Description

(int|bool) wc_downloadable_file_permission( (string) $download_id, (int|WC_Product) $product, (WC_Order) $order, (int) $qty = 1 ); 

Returns (int|bool)

insert id or false on failure

Parameters (4)

0. $download_id (string)
The download id.
1. $product (int|WC_Product)
The product.
2. $order (WC_Order)
The order.
3. $qty — Optional. (int) => 1
Purchased

Usage

  1. if ( !function_exists( 'wc_downloadable_file_permission' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-order-functions.php'; 
  3.  
  4. // The download id. 
  5. $download_id = ''; 
  6.  
  7. // The product. 
  8. $product = null; 
  9.  
  10. // The order. 
  11. $order = null; 
  12.  
  13. // purchased 
  14. $qty = 1; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wc_downloadable_file_permission($download_id, $product, $order, $qty); 
  18.  

Defined (1)

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

/includes/wc-order-functions.php  
  1. function wc_downloadable_file_permission( $download_id, $product, $order, $qty = 1 ) { 
  2. if ( is_numeric( $product ) ) { 
  3. $product = wc_get_product( $product ); 
  4. $download = new WC_Customer_Download(); 
  5. $download->set_download_id( $download_id ); 
  6. $download->set_product_id( $product->get_id() ); 
  7. $download->set_user_id( $order->get_customer_id() ); 
  8. $download->set_order_id( $order->get_id() ); 
  9. $download->set_user_email( $order->get_billing_email() ); 
  10. $download->set_order_key( $order->get_order_key() ); 
  11. $download->set_downloads_remaining( 0 > $product->get_download_limit() ? '' : $product->get_download_limit() * $qty ); 
  12. $download->set_access_granted( current_time( 'timestamp', true ) ); 
  13. $download->set_download_count( 0 ); 
  14.  
  15. $expiry = $product->get_download_expiry(); 
  16.  
  17. if ( $expiry > 0 ) { 
  18. $from_date = $order->get_date_completed() ? $order->get_date_completed()->format( 'Y-m-d' ) : current_time( 'mysql', true ); 
  19. $download->set_access_expires( strtotime( $from_date . ' + ' . $expiry . ' DAY' ) ); 
  20.  
  21. return $download->save();