Wordpress - Get the category from an ID of a product?

I answered my own question, this work for me :

<?php 
$term_list = wp_get_post_terms($id_product,'product_cat',array('fields'=>'ids'));
$cat_id = (int)$term_list[0];
echo get_term_link ($cat_id, 'product_cat');

?>

Thanks Mridul Aggarwal for your help


Option #1

Get all product_cat's using this function

global $product;

$terms = get_the_terms( $product->get_id(), 'product_cat' );

Offical docs


Option #2 If you only need their ids, you can get all product_category_ids associated with a specific product, using this function:

global $product;

$product_cats_ids = wc_get_product_term_ids( $product->get_id(), 'product_cat' );

Official docs


Extra

If you would like to print out - for instance - the categories names, you need the category term-object. This can be retrieved using get_term_by().

An example:

foreach( $product_cats_ids as $cat_id ) {
    $term = get_term_by( 'id', $cat_id, 'product_cat' );

    echo $term->name;
}

Since the question is tagged woocommerce, i'm assuming that it's a product CPT created by woocommerce wordpress plugin. This answer doesn't apply if that's not the case.

The products categories is not normal categories, they are a custom taxonomy created specifically for products which is just labeled as "Categories".

You should go through the woocommerce documentation to find some function that would do this for you, if you don't find anything you can try an alternative solution. For that, first you should know the name of the taxonomy. You can copy it from inside the url in your browser when you visit categories edit screen in the backend. Then you can use wp_get_post_terms to get the terms.