
本文详解如何在wcfm marketplace插件中,于供应商店铺页面按产品分类(如蔬菜、水果)分区块动态展示其对应商品,并提供可直接部署的短代码方案与关键优化要点。
本文详解如何在wcfm marketplace插件中,于供应商店铺页面按产品分类(如蔬菜、水果)分区块动态展示其对应商品,并提供可直接部署的短代码方案与关键优化要点。
在 WooCommerce 搭配 WCFM Marketplace(WCFMmp)构建的多供应商市场中,默认的供应商店铺页仅以时间或销量排序展示全部商品,缺乏按 WooCommerce 原生产品分类(Product Category)组织内容的能力。而实际业务场景中,供应商常需按「蔬菜」「水果」「粮油」等逻辑分类清晰陈列商品,提升买家浏览效率与转化率。本文提供一套轻量、可靠且符合 WordPress 最佳实践的解决方案——通过自定义短代码,实现「遍历供应商所售全部分类 → 分组拉取对应商品 → 按标题+网格布局渲染」的完整流程。
✅ 核心实现思路
该方案不依赖额外插件或复杂钩子,而是基于 WCFM 提供的上下文识别能力(wcfm_is_store_page()、is_product() 等),精准获取当前店铺 ID;再结合 WooCommerce 原生 [products] 短代码的 store 和 category 参数,动态生成分类区块。关键在于:分类列表必须来自该供应商实际销售的商品所属分类,而非全站分类——这通过 get_terms() 配合 woocommerce_get_shop_term_ids() 或自定义查询实现(原始答案中硬编码 category="t-shirt" 仅为示意,生产环境需动态获取)。
? 完整可部署代码(推荐放入子主题的 functions.php)
// 注册短代码 [wcfm_store_category_products]
add_shortcode('wcfm_store_category_products', 'wcfm_store_category_products_shortcode');
function wcfm_store_category_products_shortcode($attr) {
// 安全过滤属性
$atts = shortcode_atts([
'per_page' => 8,
'columns' => 4,
'show_title' => 'yes'
], $attr);
// 1. 获取当前店铺 ID(兼容店铺页、商品页、其他上下文)
$store_id = 0;
if (wcfm_is_store_page()) {
$wcfm_store_url = get_option('wcfm_store_url', 'store');
$store_name = apply_filters('wcfmmp_store_query_var', get_query_var($wcfm_store_url));
if (!empty($store_name)) {
$store_user = get_user_by('slug', $store_name);
$store_id = $store_user ? $store_user->ID : 0;
}
} elseif (is_product() && isset($GLOBALS['post']->post_author)) {
$store_id = $GLOBALS['post']->post_author;
} elseif (is_single() && isset($GLOBALS['post']) && wcfm_is_vendor($GLOBALS['post']->post_author)) {
$store_id = $GLOBALS['post']->post_author;
}
if (!$store_id || !wcfm_is_vendor($store_id)) {
return '<p class="wcfm-error">无法识别供应商,请检查页面上下文。</p>';
}
// 2. 获取该供应商销售的所有产品分类(去重、排除未售出分类)
$vendor_categories = array();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'author' => $store_id,
'posts_per_page' => -1,
'fields' => 'ids',
);
$product_ids = get_posts($args);
if (!empty($product_ids)) {
foreach ($product_ids as $pid) {
$terms = wp_get_post_terms($pid, 'product_cat', ['fields' => 'ids']);
$vendor_categories = array_merge($vendor_categories, $terms);
}
$vendor_categories = array_unique(array_filter($vendor_categories));
}
if (empty($vendor_categories)) {
return '<p class="wcfm-info">该供应商暂未上架任何分类商品。</p>';
}
// 3. 按分类循环输出商品区块
ob_start();
foreach ($vendor_categories as $cat_id) {
$category = get_term($cat_id, 'product_cat');
if (!$category || is_wp_error($category) || !$category->count) continue;
echo '<section class="wcfm-store-category-section" data-category-id="' . esc_attr($cat_id) . '">';
if ('yes' === $atts['show_title']) {
echo '<h3 class="wcfm-store-category-title">' . esc_html($category->name) . '</h3>';
}
// 使用原生 [products] 短代码,支持 store + category 组合(需 WCFMmp ≥ 3.5.0)
echo do_shortcode(sprintf(
'[products category="%s" store="%d" limit="%d" columns="%d" paginate="no"]',
$category->slug,
$store_id,
intval($atts['per_page']),
intval($atts['columns'])
));
echo '</section>';
}
return ob_get_clean();
}? 使用方式
在供应商店铺页编辑器(Gutenberg 或经典编辑器)中,插入以下短代码:
[wcfm_store_category_products per_page="6" columns="3" show_title="yes"]
- per_page:每分类显示商品数(默认 8)
- columns:网格列数(默认 4)
- show_title:是否显示分类标题(yes/no)
✅ 优势说明:本方案使用 do_shortcode('[products ...]') 而非手动 WP_Query,确保继承 WooCommerce 商品卡片样式、AJAX 加载、库存状态、价格格式等全部功能,同时兼容 WCFMmp 的供应商权限与库存隔离逻辑。
⚠ 注意事项与优化建议
- 性能提示:对商品量大的供应商,建议配合对象缓存(如 Redis)或添加 wp_cache_set/get 缓存分类列表(示例中已省略以保简洁);
- 分类顺序:当前按 ID 升序排列,如需按名称或自定义顺序,可在 get_terms() 中传入 'orderby' => 'name' 等参数;
- 空分类处理:代码已内置跳过无商品分类的逻辑,避免空白区块;
- 样式定制:为 .wcfm-store-category-section 和 .wcfm-store-category-title 添加 CSS 即可全局控制视觉风格;
- 兼容性:要求 WCFM Marketplace Pro ≥ 3.5.0(旧版需确认 store 参数是否支持,否则需改用 author + tax_query 手动查询)。
通过以上实现,您即可在任意供应商店铺页灵活启用「分类驱动的商品陈列」,既满足运营需求,又保持代码轻量、可维护性强,是 WooCommerce 多商户场景下的标准实践方案。










