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

WooCommerce 3 全场景展示变体属性及订单分类变体问题排查

Hey there! Let's work through your WooCommerce 3 challenges one by one—you've got two solid global requirements, plus a specific cart page issue to fix first. Let's dive in:

First: Fixing the Cart Page Variation Attribute Disappearance

That code snippet you added, add_filter ('woocommerce_product_variation_title_include_attributes', '__return_false');, is actually the reason your attributes vanished. This filter controls whether variation attributes are included in the product title, and returning false tells WooCommerce to exclude them entirely.

To get attributes showing again (and display them cleanly), remove that filter and use this targeted approach instead. It keeps attributes visible while formatting them nicely in the cart:

// Ensure variation attributes are included in product titles (reverses your previous filter)
add_filter('woocommerce_product_variation_title_include_attributes', '__return_true');

// Customize how variation attributes display in the cart for better readability
add_filter('woocommerce_cart_item_name', 'display_variation_attributes_in_cart', 10, 3);
function display_variation_attributes_in_cart($name, $cart_item, $cart_item_key) {
    if ($cart_item['data'] instanceof WC_Product_Variation) {
        $attributes = $cart_item['data']->get_variation_attributes();
        if (!empty($attributes)) {
            $attribute_list = [];
            foreach ($attributes as $taxonomy => $value) {
                // Use the friendly attribute label (e.g., "Size" instead of "pa_size")
                $attribute_label = wc_attribute_label($taxonomy);
                $attribute_list[] = "$attribute_label: $value";
            }
            // Append attributes below the product name
            $name .= '<br><small>' . implode(', ', $attribute_list) . '</small>';
        }
    }
    return $name;
}

Now: Implementing Your Two Global Requirements

1. Display Variation Attributes Everywhere (Backend, Frontend, Emails)

This set of hooks ensures attributes show up consistently across your entire store—no more missing details in admin orders, customer-facing pages, or confirmation emails:

// Backend: Show attributes in the admin order edit page
add_action('woocommerce_admin_order_item_values', 'display_variation_attributes_admin_order', 10, 3);
function display_variation_attributes_admin_order($_product, $item, $item_id) {
    if ($_product instanceof WC_Product_Variation) {
        $attributes = $_product->get_variation_attributes();
        if (!empty($attributes)) {
            echo '<br><small>';
            foreach ($attributes as $taxonomy => $value) {
                echo wc_attribute_label($taxonomy) . ": $value<br>";
            }
            echo '</small>';
        }
    }
}

// Frontend: Show attributes on single variation product pages
add_action('woocommerce_single_product_summary', 'display_variation_attributes_single_product', 25);
function display_variation_attributes_single_product() {
    global $product;
    if ($product instanceof WC_Product_Variation) {
        $attributes = $product->get_variation_attributes();
        if (!empty($attributes)) {
            echo '<div class="variation-details">';
            echo '<h4>' . __('Variation Details', 'woocommerce') . '</h4>';
            echo '<ul>';
            foreach ($attributes as $taxonomy => $value) {
                echo "<li>" . wc_attribute_label($taxonomy) . ": $value</li>";
            }
            echo '</ul></div>';
        }
    }
}

// Emails: Show attributes in order confirmation emails
add_filter('woocommerce_email_order_item_quantity', 'display_variation_attributes_email', 10, 4);
function display_variation_attributes_email($quantity_display, $item) {
    $_product = $item->get_product();
    if ($_product instanceof WC_Product_Variation) {
        $attributes = $_product->get_variation_attributes();
        if (!empty($attributes)) {
            $attribute_text = [];
            foreach ($attributes as $taxonomy => $value) {
                $attribute_text[] = wc_attribute_label($taxonomy) . ": $value";
            }
            $quantity_display .= '<br><small>' . implode(', ', $attribute_text) . '</small>';
        }
    }
    return $quantity_display;
}

2. Display Product Categories & Variations Everywhere

This code adds product categories alongside variations in every key section, so both you and your customers can easily see category context:

// Helper function to get formatted product category names
function get_product_category_string($_product) {
    $category_ids = $_product->get_category_ids();
    $category_names = [];
    foreach ($category_ids as $cat_id) {
        $category = get_term($cat_id, 'product_cat');
        if ($category) $category_names[] = $category->name;
    }
    return !empty($category_names) ? implode(', ', $category_names) : __('No Category', 'woocommerce');
}

// Backend: Show categories in admin order items
add_action('woocommerce_admin_order_item_values', 'display_product_category_admin_order', 15, 3);
function display_product_category_admin_order($_product, $item, $item_id) {
    $category_string = get_product_category_string($_product);
    echo '<br><small><strong>' . __('Categories:', 'woocommerce') . '</strong> ' . $category_string . '</small>';
}

// Frontend: Show categories in cart and checkout pages
add_filter('woocommerce_cart_item_name', 'display_product_category_cart', 20, 3);
function display_product_category_cart($name, $cart_item, $cart_item_key) {
    $category_string = get_product_category_string($cart_item['data']);
    $name .= '<br><small><strong>' . __('Categories:', 'woocommerce') . '</strong> ' . $category_string . '</small>';
    return $name;
}

// Frontend: Show categories on single product pages
add_action('woocommerce_single_product_summary', 'display_product_category_single_product', 15);
function display_product_category_single_product() {
    global $product;
    $category_string = get_product_category_string($product);
    echo '<div class="product-categories">';
    echo '<strong>' . __('Categories:', 'woocommerce') . '</strong> ' . $category_string;
    echo '</div>';
}

// Emails: Show categories in order confirmation emails
add_filter('woocommerce_email_order_item_name', 'display_product_category_email', 10, 2);
function display_product_category_email($item_name, $item) {
    $_product = $item->get_product();
    $category_string = get_product_category_string($_product);
    $item_name .= '<br><small><strong>' . __('Categories:', 'woocommerce') . '</strong> ' . $category_string . '</small>';
    return $item_name;
}

Quick Tips for Implementation

  • Add all these snippets to your child theme's functions.php file, or use a plugin like Code Snippets to safely add custom code without risking theme updates.
  • Test each section (cart, admin orders, emails, single product pages) after adding the code to make sure everything displays as expected.
  • Feel free to tweak the text (like "Variation Details") or HTML classes to match your store's branding and language.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:18:08