如何在WooCommerce网站中为指定订单用户添加专属可下载文件?
解决方案
核心思路
- 后台添加专属管理界面,让管理员通过用户ID或订单号关联评估文件,同时自动验证目标用户是否购买过'Assessments'产品;
- 将关联的文件ID存储到用户元数据中;
- 过滤WooCommerce的用户下载列表,仅给已关联文件的用户显示专属评估文件。
代码实现(添加到主题的functions.php)
1. 管理员后台管理界面
这段代码会在WooCommerce菜单下添加一个「评估文件管理」子页面,支持通过用户ID或订单号关联文件,同时自动验证用户是否购买过目标产品:
// 添加评估文件管理后台菜单 add_action('admin_menu', 'add_assessment_file_manager_menu'); function add_assessment_file_manager_menu() { add_submenu_page( 'woocommerce', '评估文件管理', '评估文件管理', 'manage_woocommerce', 'assessment-file-manager', 'render_assessment_file_manager_page' ); } // 渲染管理页面表单 function render_assessment_file_manager_page() { // 处理表单提交 if ($_POST['submit_assessment_file'] && current_user_can('manage_woocommerce')) { $user_id = isset($_POST['user_id']) ? absint($_POST['user_id']) : 0; $order_id = isset($_POST['order_id']) ? absint($_POST['order_id']) : 0; $file_id = isset($_POST['assessment_file']) ? absint($_POST['assessment_file']) : 0; // 优先处理订单号:通过订单获取用户ID并验证产品 if ($order_id > 0) { $order = wc_get_order($order_id); if ($order && $order->get_user_id() > 0) { $user_id = $order->get_user_id(); $has_assessment = false; foreach ($order->get_items() as $item) { if ($item->get_name() === 'Assessments') { $has_assessment = true; break; } } if (!$has_assessment) { echo '<div class="error notice"><p>该订单未购买Assessments产品,无法关联文件</p></div>'; return; } // 保存用户关联的文件(支持多个) $existing_files = get_user_meta($user_id, '_user_assessment_files', true); $existing_files = is_array($existing_files) ? $existing_files : []; if (!in_array($file_id, $existing_files)) { $existing_files[] = $file_id; update_user_meta($user_id, '_user_assessment_files', $existing_files); echo '<div class="updated notice"><p>文件已成功关联到订单对应的用户</p></div>'; } else { echo '<div class="error notice"><p>该文件已关联给此用户</p></div>'; } } else { echo '<div class="error notice"><p>无效的订单号或订单无关联用户</p></div>'; } } elseif ($user_id > 0) { // 直接通过用户ID验证购买记录 $assessment_product = get_page_by_title('Assessments', OBJECT, 'product'); if (!$assessment_product) { echo '<div class="error notice"><p>未找到名为Assessments的产品,请检查产品名称</p></div>'; return; } $has_purchased = wc_customer_bought_product('', $user_id, $assessment_product->ID); if (!$has_purchased) { echo '<div class="error notice"><p>该用户未购买Assessments产品,无法关联文件</p></div>'; return; } // 保存用户关联的文件 $existing_files = get_user_meta($user_id, '_user_assessment_files', true); $existing_files = is_array($existing_files) ? $existing_files : []; if (!in_array($file_id, $existing_files)) { $existing_files[] = $file_id; update_user_meta($user_id, '_user_assessment_files', $existing_files); echo '<div class="updated notice"><p>文件已成功关联给该用户</p></div>'; } else { echo '<div class="error notice"><p>该文件已关联给此用户</p></div>'; } } else { echo '<div class="error notice"><p>请输入有效的用户ID或订单号</p></div>'; } } // 输出管理表单 ?> <div class="wrap"> <h1>评估文件管理</h1> <form method="post"> <table class="form-table"> <tr> <th><label for="user_id">用户ID</label></th> <td><input type="number" id="user_id" name="user_id" placeholder="输入用户ID" /></td> </tr> <tr> <th><label for="order_id">订单号</label></th> <td><input type="number" id="order_id" name="order_id" placeholder="输入订单号(优先使用)" /></td> </tr> <tr> <th><label for="assessment_file">选择评估文件</label></th> <td> <?php wp_enqueue_media(); ?> <input type="hidden" id="assessment_file" name="assessment_file" value="" /> <button type="button" class="button" id="select-assessment-file">选择文件</button> <div id="selected-file-preview"></div> </td> </tr> </table> <?php submit_button('关联文件', 'primary', 'submit_assessment_file'); ?> </form> <script> jQuery(document).ready(function($) { $('#select-assessment-file').click(function(e) { e.preventDefault(); var frame = wp.media({ title: '选择评估文件', button: { text: '选择文件' }, multiple: false }); frame.on('select', function() { var attachment = frame.state().get('selection').first().toJSON(); $('#assessment_file').val(attachment.id); $('#selected-file-preview').html('<p>已选择:<a href="' + attachment.url + '" target="_blank">' + attachment.filename + '</a></p>'); }); frame.open(); }); }); </script> </div> <?php }
2. 为用户添加专属下载项
这段代码会过滤WooCommerce的用户下载列表,仅给已关联评估文件的用户显示对应的下载项:
// 为符合条件的用户添加专属评估文件到下载栏 add_filter('woocommerce_customer_get_downloadable_products', 'add_user_assessment_downloads', 10, 1); function add_user_assessment_downloads($downloads) { $current_user = wp_get_current_user(); if (!$current_user->ID) return $downloads; // 获取用户关联的评估文件ID $assessment_file_ids = get_user_meta($current_user->ID, '_user_assessment_files', true); if (!is_array($assessment_file_ids) || empty($assessment_file_ids)) return $downloads; // 生成下载项并添加到列表 foreach ($assessment_file_ids as $file_id) { $attachment = get_post($file_id); if (!$attachment || $attachment->post_type !== 'attachment') continue; $download_url = wp_get_attachment_url($file_id); $download_name = $attachment->post_title ?: $attachment->filename; // 匹配WooCommerce下载项格式 $downloads[] = array( 'download_url' => $download_url, 'download_id' => 'assessment-file-' . $file_id, 'product_id' => 0, 'order_id' => 0, 'product_name' => 'Assessments 评估文件', 'download_name' => $download_name, 'access_expires' => '', // 永不过期,可修改为时间戳设置过期时间 'downloads_remaining' => '', // 不限下载次数,可设置数字限制 ); } return $downloads; }
使用说明
- 将上述代码复制到你的WordPress主题(或子主题)的
functions.php文件中; - 登录后台,进入「WooCommerce」→「评估文件管理」;
- 输入用户ID或订单号,点击「选择文件」从媒体库中挑选评估文件,点击「关联文件」即可;
- 用户登录后,在个人中心的「下载」标签栏就能看到专属的评估文件。
内容的提问来源于stack exchange,提问作者RichReach Education
相关产品推荐
相关产品推荐

