WC_Product_Data_Store_CPT::read_downloadsprotectedWC 3.0.0

Read downloads from post meta.

Method of the class: WC_Product_Data_Store_CPT{}

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->read_downloads( $product );
$product(WC_Product) (required) (passed by reference — &)
Product object.

Changelog

Since 3.0.0 Introduced.

WC_Product_Data_Store_CPT::read_downloads() code WC 10.7.0

protected function read_downloads( &$product ) {
	$meta_values = array_filter( (array) get_post_meta( $product->get_id(), '_downloadable_files', true ) );

	if ( $meta_values ) {
		$downloads = array();
		foreach ( $meta_values as $key => $meta_value ) {
			if ( ! isset( $meta_value['name'], $meta_value['file'] ) ) {
				continue;
			}
			$download = new WC_Product_Download();
			$download->set_id( $key );
			$download->set_name( $meta_value['name'] ? $meta_value['name'] : wc_get_filename_from_url( $meta_value['file'] ) );

			/**
			 * Filter for the path of the downloadable file.
			 *
			 * @since 2.1.0
			 *
			 * @param string     $file    The file path.
			 * @param WC_Product $product The product object.
			 * @param string     $key     The download key.
			 */
			$download->set_file( apply_filters( 'woocommerce_file_download_path', $meta_value['file'], $product, $key ) );

			/**
			 * Filter product download after initialization.
			 *
			 * @since 10.6.0
			 *
			 * @param WC_Product_Download $download   The attribute object.
			 * @param array               $meta_value The meta value.
			 * @param WC_Product          $product    The product object.
			 */
			$downloads[] = apply_filters( 'woocommerce_product_read_download', $download, $meta_value, $product );
		}
		$product->set_downloads( $downloads );
	}
}