Woocommerce: Get current product category - woocommerce

How do I get the current product category that the user is browsing through?
I am trying to use get_the_terms($post->ID, 'product_cat'); but this is giving me the categories for each product listed on the page. I would like to get the current category user is browsing through, the current product listing page.

Here is a one liner:
$wp_query->get_queried_object()->term_id;
or
$wp_query->get_queried_object()->name;
or
...

To get the current category ID.
you have to use
get_queried_object();
The proper way for doing this is..
$cate = get_queried_object();
$cateID = $cate->term_id;
echo $cateID;

try this :
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
$nterms = get_the_terms( $post->ID, 'product_tag' );
foreach ($terms as $term ) {
$product_cat_id = $term->term_id;
$product_cat_name = $term->name;
break;
}
echo $product_cat_name;

By the way, you can create shortcode [show_product_category_id], that will show product's category id. For example, while browsing a category of products, it will show this id. You can open functions.php of the theme and add this:
add_shortcode( 'show_product_category_id', 'show_product_category_id' );
function show_product_category_id() {
$cat = get_queried_object();
$catID = $cat->term_id;
echo $catID;
}

Related

How an i get product category name from it's id in woocommerce?

I want to create a shortcode for my wp+woocommerce site, that will show the name of the current products category. I get the category id from my url with get request - this is a specialty. It will look something like:
function this_product_category_name() {
$cat_id = (int) str_replace('-product_cat', '', $_GET['really_curr_tax']);
// here must be a string to get the $cat_name from $cat_id;
echo $cat_name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );
What can be the solution?
Use get_term_by().
function this_product_category_name() {
$cat_id = (int) str_replace('-product_cat', '', $_GET['really_curr_tax']);
$product_cat = get_term_by( 'id', $cat_id, 'product_cat' );
echo $product_cat->name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );
USEFUL LINKS
get_term_by()
Since you're on the product category page, then you could use get_queried_object, and then from that object you could get the name like so:
$cat = get_queried_object();
echo $cat->name;
//or if you want to get its id
echo $cat->term_id;
//if you want to see the object and what's in it then you could use print_r()
print_r($cat);
Let me know if that's what you're looking for.
So your code would be something like this:
function this_product_category_name()
{
$cat = get_queried_object();
echo $cat->name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );

Using Polylang to return terms in currently selected language

I'm using this basic loop to retrieve the category name:
$terms = get_the_terms( $product_id, 'product_cat' );
//var_dump ($terms);
if($items){
foreach ( $terms as $term ) {
//$cat_id = $term->id;
$cat_name = $term->name; //returns category name, but only in original language booking was made in.
}
}
}
I'm returning these results in a custom WooCommerce dashboard.php template, in order to display the client order history. The code works but it returns the category name ($cat_name) but not in the currently selected language, only in the language the booking was originally made in. The function I have tried to use is pll_get_term() but I am not sure of the best way of implementing it.
Thanks for looking.
With some help from Polylang premium support and correcting the way I was retrieving the id which i had written incorrectly it should be this:
$terms = get_the_terms( $product_id, 'product_cat' );
if($items){
foreach ( $terms as $term ) {
$cat_id = pll_get_term( $term->term_id, $current_lang );
$cat_name = get_term( $cat_id )->name;
}
}
}
I hadn't shown the attempt to use the pll_get_term function in the original post, but this function needs an id as a parameter which should be term_id and not id.
Chris

Display custom Field in loop - Woocommerce category

I have nested categories in my website.
I created a custom Field in Woocommerce category and I tried to Add it in category loop. It shows only the term value of the current category page
add_action( 'woocommerce_after_subcategory_title', 'custom_add_cat_Min_price', 12);
function custom_add_cat_Min_price ($category) {
$prix_min_catt = get_term_meta(get_queried_object_id(), 'prix_min_cat', true);
$terms = get_the_terms( $post->ID, 'prix_min_cat' );
foreach ($terms as $term){
echo '<div class="prixminofcatg">'.$prix_min_catt.'</div>';
}
}
I think the problem is the scope of your function. You've passed the $category to your function, but haven't used it. This gives you the ID of your category:
function custom_add_cat_Min_price ($category) {
$category_id = $category->term_id;
and from there, you should be able to extract the custom fields.
Thanks its working without Foreach
add_action( 'woocommerce_after_subcategory_title', 'custom_addd_cat_Min_price', 29);
function custom_addd_cat_Min_price ($category) {
$category_id = $category->term_id;
$prix_min_cag = get_term_meta($category_id, 'prix_min_cat', true);
$terms = get_term( $category_id, 'prix_min_cat' );
echo '<div class="prixminofcatg">'.$prix_min_cag.'</div>';
}

Woocommerce : Why do I get parrent category of a product and not current child category?

I want to make a discount category. But on some categories I get parent category id and not the child id.
I tried getting term_id of product cat like this :
$terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
$term_id = $terms[0]->term_id;
and this version is not a good solution for me:
$category = get_queried_object();
$term_id = $category->term_id;
So far I'm stuck at this:
function category_percentage($product)
{
$terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
$term_id = $terms[0]->term_id; // I belive here is the problem
$discount_category = get_term_meta($term_id, 'discount_category', true);
return $discount_category;
}
add_filter( 'woocommerce_product_get_price', 'custom_sale_price_for_category', 10, 2 );
function custom_sale_price_for_category( $price, $product )
{
if(category_percentage($product)<=0) return $price;
$price *= ( 1 - category_percentage($product)/100 );
return $price;
}
in category_percentage($product) function I want to get the current category of the product, but for some reason in some categories I get the parrent category, and yes, I do have the parent category checked for the product as well. But I have it like that for all the products and still get different results when I recive the current cat id.

How I can get the categories of an current product ordred hearchicaly in woocommerce?

I am in single product page of the woocommerce plugin I would create a breadcrumb, so how I can get the categories of this product ordred hierarchical order ?
Thanks in advance !
If you want to display the breadcrumb of a product, you can use the function woocommerce_breadcrumb();. I think you can save a lot of time using that function instead of creating one from zero.
add_action( 'woocommerce_before_add_to_cart_button', 'breadcrumb', 1 );
function bbloomer_custom_action() {
echo woocommerce_breadcrumb();
}
This is how it looks in my theme. https://i.stack.imgur.com/CjiIL.jpg
Finally I found this solution to make your custom breadcrumb of the current product in woocommerce single product page :
<?php
$product_id = get_queried_object_id();
$product = wc_get_product( $product_id );
$terms = get_the_terms( $product_id, 'product_cat' );
$number_of_terms = count($terms);
$breadcrumb = array();
$breadcrumb_finish = false;
$parent = 0;
while ( $breadcrumb_finish == false ) {
foreach ($terms as $term) {
if($term->parent == $parent)
{
$link_breadcrumb = "<span>".$term->name."</span>";
array_push($breadcrumb, $link_breadcrumb);
$parent = $term->term_id;
}
}
if(count($breadcrumb) == $number_of_terms)
$breadcrumb_finish = true;
}
print_r($breadcrumb);
?>

Resources