如何通过WooCommerce Store API修改「全部评论」块按作者过滤
WooCommerce Store API扩展:作者页面过滤产品评论
需求说明
- 在WordPress作者页面,让Gutenberg「全部评论」块仅显示当前作者所创建产品的HPOS WooCommerce评论
- 需要扩展WooCommerce Store API,对
GET /wc/store/v1/products/reviews请求添加作者过滤逻辑
当前API默认请求参数
GET /wc/store/v1/products/reviews { "context":"view", "page":1, "per_page":10, "order":"desc", "orderby":"date_gmt", "offset":0, "_locale":"user" }
已尝试的代码
前端JS代码(修改API请求参数)
document.addEventListener('DOMContentLoaded', function () { if (document.body.classList.contains('author')) { const authorId = window.wc_author_data.author_id; if (!authorId) { console.error('Author ID is not set.'); return; } const originalFetch = wp.apiFetch; wp.apiFetch = function (options) { if (options.path && options.path.startsWith('/wc/store/v1/products/reviews')) { const url = new URL(options.path, window.location.origin); url.searchParams.set('author', authorId); options.path = url.pathname + url.search; } return originalFetch(options); }; // 测试请求 wp.apiFetch({ path: `/wc/store/v1/products/reviews?order=desc&orderby=date_gmt&per_page=10&offset=0&_locale=user&author=${authorId}` }) .then(response => console.log('Fetch response:', response)) .catch(error => console.error('Fetch error:', error)); } });
后端PHP代码(过滤评论查询)
function filter_product_reviews_by_author($query) { if (defined('REST_REQUEST') && REST_REQUEST) { if (strpos($_SERVER['REQUEST_URI'], '/wc/store/v1/products/reviews') !== false) { $params = $_GET; if (isset($params['author']) && !empty($params['author'])) { $query->query_vars['user_id'] = intval($params['author']); error_log('Query modified to filter by author: ' . $params['author']); } } } } add_action('pre_get_comments', 'filter_product_reviews_by_author');
问题分析与修正方案
你的思路方向是对的,但存在几个关键问题需要调整:
- 后端过滤逻辑错误:
user_id是过滤评论提交者的ID,不是产品作者ID。得先找到该作者的所有产品,再过滤关联这些产品的评论。 - 前端拦截方式不规范:直接覆盖
wp.apiFetch容易和其他插件冲突,建议用官方提供的拦截器方法。 - HPOS兼容性:要确保逻辑适配WooCommerce的高性能订单存储模式,避免依赖旧的评论元数据逻辑。
修正后的后端代码
function filter_product_reviews_by_product_author($query) { if (defined('REST_REQUEST') && REST_REQUEST && strpos($_SERVER['REQUEST_URI'], '/wc/store/v1/products/reviews') !== false) { $author_id = isset($_GET['author']) ? intval($_GET['author']) : 0; if ($author_id > 0) { // 获取当前作者的所有产品ID $product_ids = get_posts([ 'post_type' => 'product', 'author' => $author_id, 'fields' => 'ids', 'posts_per_page' => -1, ]); if (!empty($product_ids)) { // 过滤评论所属的产品ID $query->query_vars['post__in'] = $product_ids; error_log('Filtering reviews for products by author ID: ' . $author_id); } } } } add_action('pre_get_comments', 'filter_product_reviews_by_product_author');
修正后的前端代码
document.addEventListener('DOMContentLoaded', function () { if (!document.body.classList.contains('author')) return; const authorId = window.wc_author_data?.author_id; if (!authorId) { console.error('Author ID未设置'); return; } // 使用官方API拦截器,避免覆盖原有逻辑 wp.apiFetch.use(function (options, next) { if (options.path?.startsWith('/wc/store/v1/products/reviews')) { const url = new URL(options.path, window.location.origin); url.searchParams.set('author', authorId); options.path = url.pathname + url.search; } return next(options); }); });
额外注意事项
- 要确保
window.wc_author_data在作者页面能正确获取,可通过后端代码传递:
function enqueue_author_data_script() { if (is_author()) { $author_id = get_queried_object_id(); wp_localize_script('wp-api', 'wc_author_data', [ 'author_id' => $author_id ]); } } add_action('wp_enqueue_scripts', 'enqueue_author_data_script');
- 测试时确认HPOS模式已启用,验证评论是否正确关联到对应产品的作者。
内容的提问来源于stack exchange,提问作者DFakdkmnadDlakdkd
相关产品推荐
相关产品推荐

