Woocommerce: Allow users to extend downloadable products expiry after login

There are a few ways that we can protect downloadable product links in woocommerce.

Option A: Limit the number of download per user

Option B: Enforce Logged-in before download.

Option C: Set an expiry date for the download link.

Personally, I dislike option A and B because they are a hassle. Option C sounds more viable but the complain might come in after the expiry date. I think the best solution is to allow users to download the product immediately upon purchase (or in the email) and subsequently, they have to be logged in order to download the product. However, woocommerce doesn’t allow “log in to download” per product (we have to do it globally). So another option is to extend the expiry date everytime the user login. We can do this using the woocommerce_after_my_account hook.

# extend download expiry date by 1 day everytime user logins to dashboard
add_action('woocommerce_after_my_account', 'update_downloadable_expiry_now');
function update_downloadable_expiry_now() {
  global $wpdb;
  $table = $wpdb->prefix.'woocommerce_downloadable_product_permissions';
  $query = $wpdb->prepare("update $table set access_expires=CURDATE() + INTERVAL 1 DAY where user_id='".wp_get_current_user()->ID."' and access_expires is not NULL");
  if ($wpdb->query($query) === FALSE) {
    _e("could not update downloadable product access expiry date.", 'woocommerce');
  }
}

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Sitecritic.net Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.

1 thought on “Woocommerce: Allow users to extend downloadable products expiry after login”

  1. Respected Sir,
    This is a really imp script to extend the expiry of downloadable product. I am trying to use it in wordpress (3.91) and woocommerce(2.1.11), but its not working. Sir, can you please guide me how to apply this code ?
    yours faithfully
    Murulimadhav

Comments are closed.