WordPress at Your Fingertips
function is not described


DownloadPermissionsAdjuster::adjust_download_permissions() public WC 1.0

Create additional download permissions for variations if necessary.

When a simple downloadable product is converted to a variable product, existing download permissions are still present in the database but they don't apply anymore. This method creates additional download permissions for the variations based on the old existing ones for the main product.

The procedure is as follows. For each existing download permission for the parent product, check if there's any variation offering the same file for download (the file URL, not name, is checked). If that is found, check if an equivalent permission exists (equivalent means for the same file and with the same order id and customer id). If no equivalent permission exists, create it.

{} It's a method of the class: DownloadPermissionsAdjuster{}

No Hooks.


null. Nothing.


$DownloadPermissionsAdjuster = new DownloadPermissionsAdjuster();
$DownloadPermissionsAdjuster->adjust_download_permissions( $product_id );
$product_id(int) (required)
The id of the product to check permissions for.

Code of DownloadPermissionsAdjuster::adjust_download_permissions() WC 5.9.0

public function adjust_download_permissions( int $product_id ) {
	$product = wc_get_product( $product_id );
	if ( ! $product ) {

	$children_ids = $product->get_children();
	if ( ! $children_ids ) {

	$parent_downloads = $this->get_download_files_and_permissions( $product );
	if ( ! $parent_downloads ) {

	$children_with_downloads = array();
	foreach ( $children_ids as $child_id ) {
		$child                                = wc_get_product( $child_id );
		$children_with_downloads[ $child_id ] = $this->get_download_files_and_permissions( $child );

	foreach ( $parent_downloads['permission_data_by_file_order_user'] as $parent_file_order_and_user => $parent_download_data ) {
		foreach ( $children_with_downloads as $child_id => $child_download_data ) {
			$file_url = $parent_download_data['file'];

			$must_create_permission =
				// The variation offers the same file as the parent for download...
				in_array( $file_url, array_keys( $child_download_data['download_ids_by_file_url'] ), true ) &&
				// ...but no equivalent download permission (same file URL, order id and user id) exists.
				! array_key_exists( $parent_file_order_and_user, $child_download_data['permission_data_by_file_order_user'] );

			if ( $must_create_permission ) {
				// The new child download permission is a copy of the parent's,
				// but with the product and download ids changed to match those of the variation.
				$new_download_data                = $parent_download_data['data'];
				$new_download_data['product_id']  = $child_id;
				$new_download_data['download_id'] = $child_download_data['download_ids_by_file_url'][ $file_url ];
				$this->downloads_data_store->create_from_data( $new_download_data );