如何在WooCommerce后台订单详情页展示产品自定义分类法
WooCommerce后台订单详情页展示产品自定义分类「季节」实现方案
前提确认
- 你创建的「季节」自定义分类法的slug记为
season,如果你的实际slug不同,后续代码中对应替换即可 - 以下代码无需修改WooCommerce核心文件,直接添加到主题功能文件即可,不影响后续版本更新
实现代码
将以下代码添加到当前活跃主题的functions.php文件,或使用自定义代码片段插件导入:
add_action( 'woocommerce_before_order_itemmeta', 'display_product_season_taxonomy_in_admin_order', 10, 3 ); function display_product_season_taxonomy_in_admin_order( $item_id, $item, $product ) { // 仅后台管理员访问时生效 if ( ! is_admin() || ! current_user_can( 'manage_woocommerce' ) ) return; // 定义你要展示的自定义分类法slug,这里对应季节分类的slug $taxonomy_slug = 'season'; // 获取产品关联的分类术语 $terms = get_the_terms( $product->get_id(), $taxonomy_slug ); if ( $terms && ! is_wp_error( $terms ) ) { $term_names = wp_list_pluck( $terms, 'name' ); // 输出分类信息 echo '<div class="product-season-tax"><strong>所属季节:</strong>' . implode( '、', $term_names ) . '</div>'; } }
效果验证
添加代码后进入任意后台订单详情页,订单内每个产品的名称下方会自动展示关联的季节分类信息,若产品绑定了多个季节分类,会自动用顿号分隔展示。
如果你的产品是变体类型,代码会自动读取父级产品的季节分类信息,无需额外调整
内容的提问来源于stack exchange,提问作者user3796291
相关产品推荐
相关产品推荐

