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

WooCommerce购物车及结账页显示商品分类名:代码位置与重复显示问题

WooCommerce Cart & Checkout Template Modification Questions

1. Is modifying cart/cart.php lines 75-79 appropriate for showing product categories?

Absolutely—those lines sit right in the cart item loop where individual product details are rendered in WooCommerce 3.3.3’s default template, so this is a logical spot to add category names. That said, don’t edit the core WooCommerce template directly—you’ll lose your changes the next time WooCommerce updates.

Instead, use a child theme to keep your edits safe:

  • Copy wp-content/plugins/woocommerce/templates/cart/cart.php to wp-content/themes/your-child-theme/woocommerce/cart/cart.php
  • Make your edits to the child theme’s version. This way, updates won’t overwrite your work.

As long as you’re correctly fetching the product object (using $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );) and pulling categories with something like wp_get_post_terms( $_product->get_id(), 'product_cat', array( 'fields' => 'names' ) );, this location will work great for displaying categories in the cart table.

2. Fixing duplicate category names on checkout/review-order.php

Duplicate categories here almost always stem from one of these issues—let’s break down how to fix them:

Common Causes & Fixes:

  • Your code is inside a nested loop: The review-order.php template loops through cart items, but if you added your category code inside a sub-loop (like one the Visual Products Configurator uses to render add-ons), it’ll run multiple times per item. Move your code to the main foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) loop, outside any inner loops from the plugin.

  • You’re outputting all categories without handling duplicates: If a product is in multiple categories, wp_get_post_terms returns all of them. If you’re looping through each term to display, you might see repeats if the plugin triggers the code twice. Instead, display categories in a single line or pick the first one:

    $categories = wp_get_post_terms( $_product->get_id(), 'product_cat', array( 'fields' => 'names' ) );
    if ( ! empty( $categories ) ) {
        // Show first category only
        echo '<span class="product-category">' . esc_html( $categories[0] ) . '</span>';
        // OR show all categories separated by commas
        // echo '<span class="product-category">' . esc_html( implode(', ', $categories) ) . '</span>';
    }
    
  • The configurator plugin is adding duplicate elements: Some product configurators inject extra HTML into checkout items. If your category code is placed near where the plugin outputs product data, it might get duplicated. Try moving your code to a different spot—like right after the product name instead of near the price or quantity.

  • Accidental duplicate code: Double-check lines 36-41 to make sure you didn’t paste your category code twice, or that the existing template code in that section isn’t looping twice (unlikely, but worth verifying).

Quick Debug Trick:

Wrap your category code in unique HTML comments to see if it’s rendering multiple times per product:

<!-- CATEGORY DISPLAY START -->
<?php // Your category code here ?>
<!-- CATEGORY DISPLAY END -->

Then inspect the checkout page’s HTML source. If you see multiple START/END blocks for one product, you’ll know the code is running more than once per item—adjust its placement accordingly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:08:47