Woocommerce - Get Category for Product Page

WooCommerce - get category for product page

A WC product may belong to none, one or more WC categories. Supposing you just want to get one WC category id.

global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}

Please look into the meta.php file in the "templates/single-product/" folder of the WooCommerce plugin.

<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>

How to Get the Current category ID in the product page in Woocommerce?

Use this code

global $wp_query;
$terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
foreach ($terms_post as $term_cat) {
$term_cat_id = $term_cat->term_id;
echo $term_cat_id;
}

Woocommerce get category id from product page

Ok a good night sleep and eventually I found the answer. If someone will need it here it is:

       if ( is_tax( 'product_cat' ) ) {
$current_cat = get_queried_object_id();
$cat_ancestors = get_ancestors( $current_cat, 'product_cat' );

} elseif ( is_singular( 'product' ) ) {
$terms = wc_get_product_terms(
get_queried_object_id(),
'product_cat',
apply_filters(
'woocommerce_product_categories_widget_product_terms_args',
array(
'orderby' => 'parent',
'order' => 'DESC',
)
)
);

if ( $terms ) {
$main_term = apply_filters( 'woocommerce_product_categories_widget_main_term', $terms[0], $terms );
$current_cat = $main_term->term_id;
$cat_ancestors = get_ancestors( $main_term->term_id, 'product_cat' );
}
}

Get the product category name and description in Woocommerce Single Product page

As you can have many product categories for a product, you will need to use a foreach loop. The $term variable is the WP_Term object…

<?php 
foreach( wp_get_post_terms( get_the_id(), 'product_cat' ) as $term ){
if( $term ){
echo $term->name . '<br>'; // product category name
if ($term->description)
echo $term->description . '<br>'; // Product category description
}
}
?>

Tested and works

Woocommerce: Get current product category

Here is a one liner:

$wp_query->get_queried_object()->term_id;

or

$wp_query->get_queried_object()->name;

or

...

Get category id by product in WooCommerce

Try

$terms = get_the_terms ( $product_id, 'product_cat' );
foreach ( $terms as $term ) {
$cat_id = $term->id;
}

Try var_dump($terms); to rightly analyse how to get category id.

Hope this helps.

Get the category name from a WooCommerce product using the product id or the category id

To get the product category name(s) from a product Id, use instead wp_get_post_terms() function:

$product_id = 1202; // product Id
$term_names_array = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'names') ); // Array of product category term names
$term_names_string = count($term_names_array) > 0 ? implode(', ', $term_names_array) : ''; // Convert to a coma separated string
echo $term_names_string; // Display the string

To get the product category name from a product category Id, use get_term-by() function as follows:

$term_id   = 125; // Product category term Id
$term = get_term-by( 'term_id', $category_term_id, 'product_cat' ); // The WP_Term Object
$term_name = $term->name; // Get the term name from the WP_Term Object
echo $term_name; // Display

Tested and works.



Related Topics



Leave a reply



Submit