wc_downloadable_file_permission()WC 1.0

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

Return

Int|true|false. insert id or false on failure.

Usage

wc_downloadable_file_permission( $download_id, $product, $order, $qty, $item );
$download_id(string) (required)
File identifier.
$product(int|WC_Product) (required)
Product instance or ID.
$order(WC_Order) (required)
Order data.
$qty(int)
Quantity purchased.
Default: 1
$item(WC_Order_Item)
Item of the order.
Default: null

wc_downloadable_file_permission() code WC 9.4.2

function wc_downloadable_file_permission( $download_id, $product, $order, $qty = 1, $item = null ) {
	if ( is_numeric( $product ) ) {
		$product = wc_get_product( $product );
	}
	$download = new WC_Customer_Download();
	$download->set_download_id( $download_id );
	$download->set_product_id( $product->get_id() );
	$download->set_user_id( $order->get_customer_id() );
	$download->set_order_id( $order->get_id() );
	$download->set_user_email( $order->get_billing_email() );
	$download->set_order_key( $order->get_order_key() );
	$download->set_downloads_remaining( 0 > $product->get_download_limit() ? '' : $product->get_download_limit() * $qty );
	$download->set_access_granted( time() );
	$download->set_download_count( 0 );

	$expiry = $product->get_download_expiry();

	if ( $expiry > 0 ) {
		$from_date = $order->get_date_completed() ? $order->get_date_completed()->format( 'Y-m-d' ) : current_time( 'mysql', true );
		$download->set_access_expires( strtotime( $from_date . ' + ' . $expiry . ' DAY' ) );
	}

	$download = apply_filters( 'woocommerce_downloadable_file_permission', $download, $product, $order, $qty, $item );

	return $download->save();
}