Skip to main content

Search here to get answers to you questions.

How to Restrict WooCommerce Files Download Only for Logged-in Users?

Restricting file downloads in WooCommerce to logged-in users can be useful for various reasons, such as protecting digital content or offering exclusive downloads to registered customers.

In this documentation, we will walk you through the process of setting up this WooCommerce file download restriction using a simple code snippet.

Note: Some technical knowledge or assistance may be required to implement the solution effectively.

Step 1: Enable Account Creation in WooCommerce

  • Go to your WordPress dashboard.
  • Navigate to WooCommerce > Settings > Accounts & Privacy.
  • Locate the “Account Creation” section.
  • Check the boxes for “Allow customers to place orders without an account” and “Allow customers to create an account during checkout.”
  • Save the changes.

 

 

Step 2: Restrict Downloads with Custom Code

Open your preferred code editor or access your WordPress theme’s functions.php file.

 

Add the following code snippet to the .php file:

 

function restrict_woocommerce_downloads() {
if (!is_user_logged_in() && is_downloadable()) {
wc_add_notice( __(‘Please log in to download files’, ‘woocommerce’), ‘error’ );
wp_redirect(get_permalink(wc_get_page_id(‘myaccount’)));
exit;
}
}
add_action(‘woocommerce_before_download’, ‘restrict_woocommerce_downloads’);

  • Save the changes to the file.

 

Explanation:

The provided code snippet adds a check before a file is downloaded. Here’s how it works:

The restrict_woocommerce_downloads function is responsible for checking if the user is logged in and if the file is downloadable.
If the user is not logged in and the file is downloadable, an error notice is displayed, informing the user to log in.
The user is then redirected to the “My Account” page, where they can either log in or register.
The add_action hook ensures that the restrict_woocommerce_downloads function is executed before a download occurs.

 

Important Notes:

Customization: The example provided is a basic implementation and may require adjustments to fit your specific WooCommerce setup. It’s recommended to consult a developer if you encounter any issues or need further customization.

Backing up: Before making any modifications to your site or code, always create a backup to avoid any potential data loss or unintended consequences.

How to Restrict without Custom Code?

If you prefer not to use custom code, there are plugins available that can help you restrict file downloads in WooCommerce to logged-in users. We will introduce two popular plugins that can assist you in achieving this functionality: “WooCommerce for Logged-in Users” and “WooCommerce Memberships” and ways to use them rather than using custom code snippets.

 

Option 1: WooCommerce for Logged-in Users Plugin

 

 

The WooCommerce for Logged-in Users plugin is a handy tool that allows you to restrict various aspects of your WooCommerce store to logged-in users.

With this plugin, you can easily control access to file downloads, products, pages, and more. It provides user-friendly configuration options within the WooCommerce settings, making it convenient to set up restrictions without the need for custom code.

Here is the step-by-step method to implement the plugin to restrict Logged-in users in WooCommerce:

  • Go to your WordPress dashboard.
  • Navigate to Plugins > Add New.
  • Search for “WooCommerce for Logged-in Users.”
  • Install and activate the plugin.
  • Once activated, the plugin should provide configuration options within the WooCommerce settings or through a dedicated menu item.
  • Follow the plugin’s documentation or instructions to set up the restriction for file downloads specifically.

 

Option 2: WooCommerce Memberships Plugin

 

 

The WooCommerce Memberships plugin is yet another powerful solution for creating membership plans and restricting content in your WooCommerce store.

With this plugin, you can define different membership levels, assign access to specific products or files, and control content visibility based on user roles. By utilizing the WooCommerce Memberships plugin, you can effectively limit file downloads exclusively to logged-in users without implementing any custom code hassle.

 

Here is the step-by-step method to implement the plugin restricting Logged-in users in WooCommerce:

 

  • Go to your WordPress dashboard.
  • Navigate to Plugins > Add New.
  • Search for “WooCommerce Memberships.”
  • Install and activate the plugin.
  • Once activated, the plugin will guide you through the setup process, including creating membership plans and granting access to specific products or files.
  • Configure the membership settings to restrict file downloads only to logged-in users.
  • You can follow the plugin’s documentation or instructions to set up the restriction for file downloads specifically.