WC_Customer_Download::track_download()
Track a download on this permission.
Method of the class: WC_Customer_Download{}
No Hooks.
Return
null
. Nothing (null).
Usage
$WC_Customer_Download = new WC_Customer_Download(); $WC_Customer_Download->track_download( $user_id, $user_ip_address );
- $user_id(int)
- Id of the user performing the download.
Default: null - $user_ip_address(string)
- IP Address of the user performing the download.
Default: null
Changelog
Since 3.3.0 | Introduced. |
WC_Customer_Download::track_download() WC Customer Download::track download code WC 9.3.1
public function track_download( $user_id = null, $user_ip_address = null ) { global $wpdb; // Must have a permission_id to track download log. if ( ! ( $this->get_id() > 0 ) ) { throw new Exception( __( 'Invalid permission ID.', 'woocommerce' ) ); } // Increment download count, and decrement downloads remaining. // Use SQL to avoid possible issues with downloads in quick succession. // If downloads_remaining is blank, leave it blank (unlimited). // Also, ensure downloads_remaining doesn't drop below zero. $query = $wpdb->prepare( " UPDATE {$wpdb->prefix}woocommerce_downloadable_product_permissions SET download_count = download_count + 1, downloads_remaining = IF( downloads_remaining = '', '', GREATEST( 0, downloads_remaining - 1 ) ) WHERE permission_id = %d", $this->get_id() ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared $wpdb->query( $query ); // Re-read this download from the data store to pull updated counts. $this->data_store->read( $this ); // Track download in download log. $download_log = new WC_Customer_Download_Log(); $download_log->set_timestamp( time() ); $download_log->set_permission_id( $this->get_id() ); if ( ! is_null( $user_id ) ) { $download_log->set_user_id( $user_id ); } if ( ! is_null( $user_ip_address ) ) { $download_log->set_user_ip_address( $user_ip_address ); } $download_log->save(); }