wc_downloadable_file_permission( string $download_id, int|WC_Product $product, WC_Order $order, int $qty = 1, WC_Order_Item $item = null )
Grant downloadable product access to the file identified by $download_id.
Description Description
Parameters Parameters
- $download_id
-
(Required) File identifier.
- $product
-
(Required) Product instance or ID.
- $order
-
(Required) Order data.
- $qty
-
(Optional) Quantity purchased.
Default value: 1
- $item
-
(Optional) Item of the order.
Default value: null
Return Return
(int|bool) insert id or false on failure.
Source Source
File: includes/wc-order-functions.php
function wc_downloadable_file_permission( $download_id, $product, $order, $qty = 1 ) {
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 );
return $download->save();
}