Design a plugin for wordpress woocommerce to display a tab to show attachment download
You can add the attachement tab in the product edit page.
Demo Link: https://www.shopym.com/product/amd-fd6350frhkbox-fx-6350-fx-series-6-core-black-edition/
add a custom_field , spare it with link|description and commas, the default description is the file name.
save below code to a .php file, and upload it to the /wp-content/plugin
<?php /* Plugin Name: Your WooCommerce Product Attachment Plugin Description: This plugin allows you to upload and display attachments on product pages in WooCommerce. Version: 1.0 Author: Shopaii Author URI: https://www.shopaii.com License: GPL2 */ // 插件其他功能代码... // 在产品编辑页面添加自定义字段 add_action('woocommerce_product_options_general_product_data', 'custom_attachment_field'); function custom_attachment_field() { woocommerce_wp_text_input( array( 'id' => '_custom_attachment_field', 'label' => __('Attachment URLs', 'text_domain'), 'desc_tip' => 'true', 'description' => __('Enter attachment URLs|Descriptions separated by commas ', 'text_domain') ) ); } // 保存自定义字段的值 add_action('woocommerce_process_product_meta', 'save_custom_attachment_field'); // 保存自定义字段的值为二维数组 function save_custom_attachment_field($product_id) { $custom_attachment = sanitize_text_field($_POST['_custom_attachment_field']); $attachment_array = array_map('trim', explode(',', $custom_attachment)); $attachment_data = array(); foreach ($attachment_array as $attachment) { $attachment_parts = explode('|', $attachment); // 按照 | 分割链接和说明 $url = trim($attachment_parts[0]); // 链接 $description = isset($attachment_parts[1]) ? trim($attachment_parts[1]) : basename($url); // 说明,默认为文件名 $attachment_data[] = array( 'url' => $url, 'description' => $description ); } update_post_meta($product_id, '_custom_attachment_field', $attachment_data); } // JavaScript 上传文件并更新自定义字段 add_action('admin_footer', 'custom_attachment_js_code'); function custom_attachment_js_code() { ?> <script> jQuery(document).ready(function($) { $(document).on('change', 'input[type="file"]', function() { var attachmentData = new FormData(); attachmentData.append('file', $(this)[0].files[0]); attachmentData.append('action', 'upload_attachment'); $.ajax({ type: 'POST', url: ajaxurl, data: attachmentData, contentType: false, processData: false, success: function(response) { // 更新自定义字段的值 $('input#_custom_attachment_field').val(response.url); } }); }); }); </script> <?php } // 在产品详情页添加自定义标签 add_filter('woocommerce_product_tabs', 'add_attachment_tab'); function add_attachment_tab($tabs) { global $product; // 获取附件信息 $attachment_urls = get_post_meta($product->get_id(), '_custom_attachment_field', true); // 如果自定义附件字段不为空,则添加自定义标签 if ($attachment_urls !== '') { $tabs['attachment_tab'] = array( 'title' => __('Attachment', 'text_domain'), 'priority' => 50, 'callback' => 'attachment_tab_content' ); } return $tabs; } // 显示附件信息 function attachment_tab_content() { global $product; $attachment_data = get_post_meta($product->get_id(), '_custom_attachment_field', true); if (!empty($attachment_data)) { echo '<ul>'; foreach ($attachment_data as $attachment) { echo '<li><a href="' . esc_url($attachment['url']) . '">' . $attachment['description'] . '</a></li>'; } echo '</ul>'; } } ?>