You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce商品按子分类分组显示表格格式问题求助

Hey there! Let's work through this WooCommerce issue together—you've got two key goals here: getting that table layout to actually show up on your shop page, and grouping products neatly by their child categories. Let's break this down step by step, with code examples you can drop into your theme.

1. Fixing the Table Layout on the Shop Page

First, the reason your table layout isn't kicking in on the shop page is likely because WooCommerce uses specific template files for archive/shop pages, and you might have only modified a partial template. Here's how to replace the default list-based loop with a table:

Step 1: Override the Product Loop Container

Add this code to your child theme's functions.php file (always use a child theme to avoid losing changes when your theme updates):

// Replace default product loop <ul> with a table wrapper
add_action('woocommerce_product_loop_start', 'replace_loop_with_table_start', 9);
function replace_loop_with_table_start() {
    remove_action('woocommerce_product_loop_start', 'woocommerce_output_product_loop_wrapper', 10);
    echo '<table class="woocommerce-shop-table"><thead><tr><th>Product</th><th>Price</th><th>Add to Cart</th></tr></thead><tbody>';
}

// Close the table instead of the default </ul>
add_action('woocommerce_product_loop_end', 'replace_loop_with_table_end', 9);
function replace_loop_with_table_end() {
    remove_action('woocommerce_product_loop_end', 'woocommerce_output_product_loop_wrapper_end', 10);
    echo '</tbody></table>';
}

Step 2: Modify the Product Content Template

Copy WooCommerce's content-product.php file from /wp-content/plugins/woocommerce/templates/ to your child theme's woocommerce/ folder (create the folder if it doesn't exist). Then replace its contents with this to output table rows instead of list items:

<?php
defined( 'ABSPATH' ) || exit;

global $product;

if ( empty( $product ) || ! $product->is_visible() ) {
    return;
}
?>
<tr>
    <td class="product-cell">
        <?php
        do_action( 'woocommerce_before_shop_loop_item' );
        do_action( 'woocommerce_before_shop_loop_item_title' );
        do_action( 'woocommerce_shop_loop_item_title' );
        do_action( 'woocommerce_after_shop_loop_item_title' );
        do_action( 'woocommerce_after_shop_loop_item' );
        ?>
    </td>
    <td class="price-cell"><?php woocommerce_template_loop_price(); ?></td>
    <td class="cart-cell"><?php woocommerce_template_loop_add_to_cart(); ?></td>
</tr>

Step 3: Add Basic CSS for Styling

Drop this into your child theme's style.css to make the table look clean:

.woocommerce-shop-table {
    width: 100%;
    border-collapse: collapse;
    margin: 2rem 0;
}
.woocommerce-shop-table th, .woocommerce-shop-table td {
    border: 1px solid #e0e0e0;
    padding: 1.2rem;
    text-align: left;
}
.woocommerce-shop-table th {
    background-color: #f5f5f5;
    font-weight: 600;
}
.product-cell img {
    max-width: 80px;
    margin-right: 1rem;
    float: left;
}

2. Grouping Products by Child Categories

Now, to organize products under their respective child categories, we'll modify the main shop archive template to loop through categories first, then products within each category.

Step 1: Override the Archive Product Template

Copy /wp-content/plugins/woocommerce/templates/archive-product.php to your child theme's woocommerce/ folder. Then replace the product loop section with this code:

<?php
// Get child categories (set $parent_cat_id to a specific ID if you want to target one parent category)
$parent_cat_id = 0; // 0 = all top-level categories
$child_categories = get_terms( array(
    'taxonomy' => 'product_cat',
    'parent' => $parent_cat_id,
    'hide_empty' => true, // Only show categories with products
) );

if ( ! empty( $child_categories ) ) {
    foreach ( $child_categories as $category ) {
        // Output category heading
        echo '<h2 class="category-heading">' . esc_html( $category->name ) . '</h2>';
        
        // Start table for this category
        echo '<table class="woocommerce-shop-table category-table"><thead><tr><th>Product</th><th>Price</th><th>Add to Cart</th></tr></thead><tbody>';
        
        // Query products in this category
        $category_products = new WP_Query( array(
            'post_type' => 'product',
            'posts_per_page' => -1, // Show all products, adjust as needed
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'term_id',
                    'terms' => $category->term_id,
                ),
            ),
        ) );
        
        if ( $category_products->have_posts() ) {
            while ( $category_products->have_posts() ) {
                $category_products->the_post();
                wc_get_template_part( 'content', 'product' );
            }
        }
        
        // Close category table
        echo '</tbody></table>';
        wp_reset_postdata();
    }
} else {
    // Fallback: Show all products if no child categories exist
    if ( woocommerce_product_loop() ) {
        do_action( 'woocommerce_before_shop_loop' );
        woocommerce_product_loop_start();
        
        if ( wc_get_loop_prop( 'total' ) ) {
            while ( have_posts() ) {
                the_post();
                do_action( 'woocommerce_shop_loop' );
                wc_get_template_part( 'content', 'product' );
            }
        }
        
        woocommerce_product_loop_end();
        do_action( 'woocommerce_after_shop_loop' );
    } else {
        do_action( 'woocommerce_no_products_found' );
    }
}

Notes for Adjustments

  • If you only want to group products under a specific parent category, replace $parent_cat_id = 0 with the ID of your parent category.
  • Adjust the posts_per_page value if you don't want to show all products in a category at once (you'll need to add pagination if you do this, but that's an extra step).
  • Tweak the CSS to match your theme's design—adjust colors, padding, and image sizes as needed.

Just a quick reminder: Always use a child theme for these modifications so your changes don't get overwritten when WooCommerce or your theme updates. Test each change step by step to make sure everything works as expected!

内容的提问来源于stack exchange,提问作者user7706309

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:24:30