banner

Sabuj Kundu 13th Oct 2019

Woocommerce allows to upload digital files if any product is downloadable. Both Simple and Variable Product can have downloadable digital files. Sometimes free or commercial product we may need to list the digital files which customer want to purchase. In this blog post I will show two shortcodes how to display files using product id for simple product type and variation id for variable product. Actually I am sharing this code from a work in progress plugin files so all things may not show complete.

Here are some example shortcodes I tested, the product id and variation id as per my local test site

Simple Product Downloadable File Display Shortcode
here shortcode param id is product id
[cbxwoodigitalfiles_simple_product id="540"]

Variable Product Downloadable File Display Shortcode
Here shortcode param id is variation id
One variation
[cbxwoodigitalfiles_variation_product id="4644"]

Another variation
[cbxwoodigitalfiles_variation_product id="4645"]

Check the codebase online
https://gist.github.com/manchumahara/821e860511451157381f4b8010281178
or check below

<code class="language-php">
/**
 * on init call shortcode function
 */
add_action('init', 'init_shortcodes');

/**
 * Init shortcodes
 */
function init_shortcodes(){
	add_shortcode('cbxwoodigitalfiles_simple_product', 'cbxwoodigitalfiles_simple_product_display');
	add_shortcode('cbxwoodigitalfiles_variation_product', 'cbxwoodigitalfiles_variation_product_display');
}//end method init_shortcodes

/**
 * Display digital file for simple product
 *
 * @param $atts
 */
function cbxwoodigitalfiles_simple_product_display($atts){
	// normalize attribute keys, lowercase
	$atts = array_change_key_case((array)$atts, CASE_LOWER);

	$atts                      = shortcode_atts(
		array(
			'id' => 0, //this is product id				
		), $atts, 'cbxwoodigitalfiles_simple_product' );

	$id = isset($atts['id'])? intval($atts['id']) : 0;
	if($id > 0 && class_exists('WC_Product')){
		$product = new WC_Product( $id );

		if($product->is_type( 'simple' ) && $product->is_downloadable( 'yes' )){
			if($product->has_file()){
				$item_downloads = $product->get_downloads();

				$output = '<ul class="cbxwfpquickcheckout_files cbxwfpquickcheckout_files_simple">';

				foreach ($item_downloads as $item_download){
					$file_id = $item_download->get_id();
					$file_name = $item_download->get_name();
					$file_file = $item_download->get_file();

					$output .= '<li><a href="#">'.esc_attr(wp_unslash($file_name)).'</a></li>';
				}

				$output .= '</ul>';

				return $output;

			}

		}
	}


}//end method cbxwoodigitalfiles_simple_product_display

/**
 * Display files name for variation product
 *
 * @param $atts
 */
function cbxwoodigitalfiles_variation_product_display($atts){
	$atts                      = shortcode_atts(
		array(
			'id' => 0, //variation id, this is not product id			
		), $atts, 'cbxwoodigitalfiles_variation_product' );

	$id = isset($atts['id'])? intval($atts['id']) : 0;
	if($id > 0 && class_exists('WC_Product_Variation')){
		$product = new WC_Product_Variation( $id );
		if($product->is_downloadable( 'yes' ) && $product->has_file()){
			$item_downloads = $product->get_downloads();

			$output = '<ul class="cbxwfpquickcheckout_files cbxwfpquickcheckout_files_variation">';

			foreach ($item_downloads as $item_download){
				$file_id = $item_download->get_id();
				$file_name = $item_download->get_name();
				$file_file = $item_download->get_file();

				$output .= '<li><a href="#">'.esc_attr(wp_unslash($file_name)).'</a></li>';
			}

			$output .= '</ul>';

			return $output;

		}
	}

}//end method cbxwoodigitalfiles_variation_product_display
</code>

I think if we get the files list it will not be hard to implement the file download features also, I am leaving this for next day or for your homework.