Advertisement
vapvarun

Save downloadable files

Aug 26th, 2024
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | Software | 0 0
  1. /**
  2.      * Save downloadable files.
  3.      *
  4.      * @param WC_Product $product    Product instance.
  5.      * @param array      $downloads  Downloads data.
  6.      *
  7.      * @return WC_Product
  8.      */
  9.     protected function save_downloadable_files( $product, $downloads ) {
  10.         $files = [];
  11.         foreach ( $downloads as $key => $file ) {
  12.             if ( empty( $file['file'] ) ) {
  13.                 continue;
  14.             }
  15.  
  16.             $download = new WC_Product_Download();
  17.             $download->set_id( $key );
  18.             $download->set_name( $file['name'] ? $file['name'] : wc_get_filename_from_url( $file['file'] ) );
  19.             $download->set_file( apply_filters( 'woocommerce_file_download_path', $file['file'], $product, $key ) );
  20.             $files[] = $download;
  21.         }
  22.         $product->set_downloads( $files );
  23.  
  24.         return $product;
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement