我想在购物车小部件中显示woocommerce产品的产品类别名称,但类别名称应显示仅显示一个类别名称,即从哪个类别归档产品添加 . 我在functions.php中添加了以下代码,但它显示了与product关联的所有类别名称 .

function modfuel_woocommerce_before_order_add_cat($name, $item){

   $product_id = $item['product_id'];

   $_product = wc_get_product( $product_id );
   $htmlStr = "";
   $cats = "";
   $terms = get_the_terms( $product_id, 'product_cat' );

   $count = 0;
   foreach ( $terms as $term) {
    $count++;

    if($count > 1){
      $cats .= $term->name;
    }
    else{
      $cats .= $term->name . ',';
    }

   }

   $cats = rtrim($cats,',');

   $htmlStr .= sprintf( '<a href="%s">%s </a>', esc_url( $prod_permalink ), $_product->get_name() );

   $htmlStr .= "
   <dl class='variation'><dt>Product Type:</dt><dd>" . $cats . "</dd></dl>";

   return $htmlStr;
}

add_filter('woocommerce_cart_item_name','modfuel_woocommerce_before_order_add_cat', 10, 2);

add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );

我想在购物车中只添加查询的类别名称 . 我可以通过以下代码实现它,但我不知道如何在购物车中显示它 .

<?php 
    $queried_object = get_queried_object(); 
    echo $queried_object->name;
    ?>