I Want to show product category name of woocommerce product in cart widget but category name should show display only one category name i.e from which category archive product is added. I added following code in functions.php but it shows all category name linked with 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' );
I want to add only queried category name in cart. I can achieve that it with following code but i dont know how can i display it in cart.
<?php
$queried_object = get_queried_object();
echo $queried_object->name;
?>