/**
* Save downloadable files for a WooCommerce product.
*
* @param WC_Product $product Product instance.
* @param array $downloads Array of download data.
*
* @return WC_Product
*/
protected function save_downloadable_files( $product, $downloads ) {
$download_files = [];
foreach ( $downloads as $download_id => $download_data ) {
// Skip if the file URL is empty
if ( empty( $download_data['file'] ) ) {
continue;
}
// Initialize a new download object
$download = new WC_Product_Download();
$download->set_id( $download_id );
$download->set_name( ! empty( $download_data['name'] ) ? $download_data['name'] : wc_get_filename_from_url( $download_data['file'] ) );
// Apply the filter to the file path
$filtered_file_path = apply_filters( 'woocommerce_file_download_path', $download_data['file'], $product, $download_id );
$download->set_file( $filtered_file_path );
$download_files[] = $download;
}
// Apply a filter to the array of downloads before setting it on the product
$filtered_download_files = apply_filters( 'woocommerce_product_download_files', $download_files, $product );
// Set the downloads on the product
$product->set_downloads( $filtered_download_files );
return $product;
}
Comments