How to Update WooCommerce Product Download Links

How to Update WooCommerce Product Download Links

If you have launched your website with WooCommerce and you want to sell downloadable products with that, you will have a significant problem with downloadable products. The problem is that if you update the WooCommerce product downloadable file or add a new file to the files list, your user will not see the download link of the new file and will still see the download links they received when they purchased the product. In this article, I want to explain how to update WooCommerce product download links for the previous orders.

Change MihanWP infrastructure to WooCommerce

If you know, we are changing the sales system of the MihanWP website from Easy Digital Downloads to WooCommerce. After the transfer is completed, we will explain the reason for doing so in an Instagram live and the transfer process. But the first problem we encountered with using WooCommerce was that the download links were not provided to the user after the update. This was the main problem that one of the dear users of the website also asked a question in the support center about how to solve it. So we decided to share with you how to solve this problem.

To solve this problem, we have to force WooCommerce to check and provide download links instantly. When we place an order in WooCommerce, the order meta, product list, and list of downloadable files are included. There is no way to update the downloaded file, and the user can only access the previous files.

But we never have any restrictions on using WordPress and WooCommerce. Because using PHP and WordPress hooks, we can altogether remove the limits.

To solve this problem, add the following code to the functions.php file of your website template:

class WooCommerce_Legacy_Grant_Download_Permissions {
	protected static $instance = null;
	private function __construct() {
		if ( ! class_exists( 'WC_Admin_Post_Types', false ) ) {
			return;
		}
		remove_action( 'woocommerce_process_product_file_download_paths', array( 'WC_Admin_Post_Types', 'process_product_file_download_paths' ), 10, 3 );
		add_action( 'woocommerce_process_product_file_download_paths', array( $this, 'grant_download_permissions' ), 10, 3 );
	}
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self;
		}
		return self::$instance;
	}
	public function grant_download_permissions( $product_id, $variation_id, $downloadable_files ) {
		global $wpdb;

		if ( $variation_id ) {
			$product_id = $variation_id;
		}

		if ( ! $product = wc_get_product( $product_id ) ) {
			return;
		}

		$existing_download_ids = array_keys( (array) $product->get_downloads() );
		$updated_download_ids  = array_keys( (array) $downloadable_files );
		$new_download_ids      = array_filter( array_diff( $updated_download_ids, $existing_download_ids ) );
		$removed_download_ids  = array_filter( array_diff( $existing_download_ids, $updated_download_ids ) );

		if ( ! empty( $new_download_ids ) || ! empty( $removed_download_ids ) ) {
			$existing_orders = $wpdb->get_col( $wpdb->prepare( "SELECT order_id from {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE product_id = %d GROUP BY order_id", $product_id ) );

			foreach ( $existing_orders as $existing_order_id ) {
				$order = wc_get_order( $existing_order_id );

				if ( $order ) {
					if ( ! empty( $removed_download_ids ) ) {
						foreach ( $removed_download_ids as $download_id ) {
							if ( apply_filters( 'woocommerce_process_product_file_download_paths_remove_access_to_old_file', true, $download_id, $product_id, $order ) ) {
								$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %s", $order->get_id(), $product_id, $download_id ) );
							}
						}
					}
					if ( ! empty( $new_download_ids ) ) {
						foreach ( $new_download_ids as $download_id ) {
							if ( apply_filters( 'woocommerce_process_product_file_download_paths_grant_access_to_new_file', true, $download_id, $product_id, $order ) ) {
								if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT 1=1 FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %s", $order->get_id(), $product_id, $download_id ) ) ) {
									wc_downloadable_file_permission( $download_id, $product_id, $order );
								}
							}
						}
					}
				}
			}
		}
	}
}

add_action( 'admin_init', array( 'WooCommerce_Legacy_Grant_Download_Permissions', 'get_instance' ) );

Just as easily! Now safely update your digital goods. This code will free access to all files in the purchased product for previous users.

Good luck. 🙂

Is there any particular point that would complete this article?