显示 WooCommerce 下载文件列表
目录
有时,我们可能需要列出客户想要购买的数字文件的名称
/** 原文:https://codeboxr.com/display-woocommerce-digital-download-files/ 简码 [cbxwoodigitalfiles_simple_product id="540"] [cbxwoodigitalfiles_variation_product id="4644"] /** * 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