Skip to content

Instantly share code, notes, and snippets.

@manchumahara
Last active October 13, 2019 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manchumahara/821e860511451157381f4b8010281178 to your computer and use it in GitHub Desktop.
Save manchumahara/821e860511451157381f4b8010281178 to your computer and use it in GitHub Desktop.
Display WooCommerce Digital Download Files for WordPress https://codeboxr.com/display-woocommerce-digital-download-files/
/**
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment