WC_Customer_Download::track_download()publicWC 3.3.0

Track a download on this permission.

Метод класса: WC_Customer_Download{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$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.
По умолчанию: null
$user_ip_address(строка)
IP Address of the user performing the download.
По умолчанию: null

Список изменений

С версии 3.3.0 Введена.

Код WC_Customer_Download::track_download() WC 8.7.0

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();
}