/** * Grant downloadable product access to the file identified by $download_id. * * @param string $download_id file identifier * @param int|WC_Product $product * @param WC_Order $order the order * @param int $qty purchased * @return int|bool insert id or false on failure */ 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(current_time('timestamp')); $download->set_download_count(0); $expiry = $product->get_download_expiry(); if ($expiry > 0) { $order_completed_date = date_i18n("Y-m-d", $order->get_date_completed()); $download->set_access_expires(strtotime($order_completed_date . ' + ' . $expiry . ' DAY')); } return $download->save(); }
/** * Save meta box data. * * @param int $post_id * @param WP_Post $post */ public static function save($post_id, $post) { if (isset($_POST['permission_id'])) { $permission_ids = $_POST['permission_id']; $downloads_remaining = $_POST['downloads_remaining']; $access_expires = $_POST['access_expires']; $max = max(array_keys($permission_ids)); for ($i = 0; $i <= $max; $i++) { if (!isset($permission_ids[$i])) { continue; } $download = new WC_Customer_Download($permission_ids[$i]); $download->set_downloads_remaining(wc_clean($downloads_remaining[$i])); $download->set_access_expires(array_key_exists($i, $access_expires) && '' !== $access_expires[$i] ? strtotime($access_expires[$i]) : ''); $download->save(); } } }