I sell online courses in my website using WooCommerce. Each course(product) has number of downloadable files.
Unfortunately, WooCommerce does not group each product downloads together and make a long list of available downloads. If someone buy several courses, there will be a very long and confusing list of downloads which is not user-friendly.
I am looking for some codes (PHP, Java, CSS) to group each product downloads together under a drop-down menu. This way if someone buy 7 courses, then in download page there will be seven dropdown lists and by clicking on each one, downloads of that specific product just appear.
I used below PHP code. It groups downloads together but there is no drop-down menu.
/**
* Group Downloadable products by product ID
*
* @param array $downloads
* @return array
*/
function prefix_group_downloadable_products( array $downloads ) {
$unique_downloads = [];
foreach ( $downloads as $download ) {
$list = [
'download_url' => $download['download_url'],
'file_name' => $download['file']['name']
];
if ( array_key_exists( $download['product_id'], $unique_downloads ) ) {
$unique_downloads[ $download['product_id'] ]['list'][] = $list;
continue;
}
$data = $download;
$data['list'] = [ $list ];
$unique_downloads[ $download['product_id'] ] = $data;
}
return $unique_downloads;
}
add_filter( 'woocommerce_customer_get_downloadable_products',
'prefix_group_downloadable_products' );
/**
* Show number list of downloadable files for group product
*
* @param array $download
* @return void
*/
function prefix_downloads_column_download_file( array $download ) {
$lists = $download['list'];
if ( empty( $lists ) ) {
_e( 'No Download Files', 'storefront' );
return;
}
echo '<ol>';
foreach ( $lists as $list ) {
echo '<li>';
echo '<a href="' . esc_url( $list['download_url'] ) . '" class="woocommerce-MyAccount-downloads-file">';
echo esc_html( $list['file_name'] );
echo '</a></li>';
}
echo '</ol>';
}
add_action( 'woocommerce_account_downloads_column_download-file', 'prefix_downloads_column_download_file' );
Also, this code causes an error appears in "my orders" and "order report" page, preventing to display downloads there. The error is this:
Warning: Undefined array key "list" in /home/beatop/domains/sdrecords.ir/public_html/wp-content/themes/hello-theme-child-master/functions.php on line 156
No Download Files
The line 156 is related to this: $lists = $download['list'];
How to rectify this error and add drop-down menu?
2
Answers
You need to check first that
$download['list']
exist, to avoid that issue.To get a dropdown of downloads grouped by product, some changes and additions are needed (JavaScript/jQuery is required).
Note (update): On email notifications, we keep the Downloads table as it is by default, as the dropdown can’t work.
The following code will handle this dropdown everywhere, in the front end:
You will get something like (in My account > Downloads page):
In My account > View Order and in Order received (thankyou) pages:
The downloads table on email notifications stays unchanged (default WooCommerce behavior):
Addition for email notifications (optional)
Replace the downloads table, with a text linked to My Account "downloads" section:
How can I add the character (+) instead of the drop-down list next to the main product name and use the accordion menu to display the sub-set?