3

i would like to get the current category ID in the product page to show something in templates/single-product/add-to-cart/simple.phpin Woocommerce

If the category ID == 1 {//i'll show something}

if the category ID == 2 {//something else}

All of this in the "simple.php"

Thanks

Edit :

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

this code give the product name of the current product, if i can't get the ID, how could i get the string of the category name else ?

Jean R.
  • 476
  • 4
  • 16
  • 44
  • Please read our [ask] page to help you improve this question. Great questions tend to produce great answers from the community. At the very least, can you explain how you want to get such id? (i.e. javascript, php, etc.) – blurfus Nov 20 '15 at 18:46
  • Hi, Sorry for this it's hard to find words I'm using woocommerce plugin and i'll like to add some content depending on the category ID (it's PHP). But i need to get the product id only in the simple.php (because i need to interacte with the "add to cart" button) – Jean R. Nov 20 '15 at 18:49
  • Possible duplicate of [WooCommerce - get category for product page](http://stackoverflow.com/questions/15303031/woocommerce-get-category-for-product-page) -> See if the answer for that question is what you are looking for – blurfus Nov 20 '15 at 18:51
  • Hi, thanks for the answer but it's not the same issue.. – Jean R. Nov 20 '15 at 19:06

5 Answers5

10

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;
}
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
Firefog
  • 3,094
  • 7
  • 48
  • 86
4

Found some code online that might work as well (note: I have not tried it myself)

update link is dead, code is below just in case.

global $wp_query;
// get the query object
$cat_obj = $wp_query->get_queried_object();

print_r($cat_obj);

if($cat_obj)    {
    $category_name = $cat_obj->name;
    $category_desc = $cat_obj->description;
    $category_ID  = $cat_obj->term_id;
    echo $category_ID;

    // with the `$category_ID`, proceed to your `if/else` statement.
    if( $category_ID == 1){
       echo 'Cat ID is 1';
    }

    if( $category_ID == 2){
       echo 'Cat ID is 2';
    }
}
blurfus
  • 13,485
  • 8
  • 55
  • 61
  • Hi ! Thanks for the answer. I already use this code but the output only show product infos.. not the categorie where the product is – Jean R. Nov 20 '15 at 19:04
  • This one just shows the Product data, not a term_id as shown in your code – DelphiLynx May 01 '17 at 13:20
  • 1
    @ochi Ah, I did not know that. Unfortunately I cannot undo the vote for some reason, but I will check it again in 24 hours... – DelphiLynx May 02 '17 at 06:22
  • 2
    Good code example, specifically if you got the full Category object. What I also used to use is `get_the_terms ( $product_id, 'product_cat' );` for getting the category object on an alternating way. – DelphiLynx May 03 '17 at 12:44
2

You can use:

$product->get_category_ids()

This method has become available since WooCommerce 3, see the docs.

bramchi
  • 612
  • 11
  • 14
0

Short way to get current category ID:

$terms = get_the_terms( $post->cat_ID , 'product_cat' );
echo $terms[key($terms)]->slug;
defro
  • 56
  • 3
0

If you are in product page, first you need to get the product id like:

$product_id = $product->get_id();

then use this code:

$terms = get_the_terms( $product_id , 'product_cat' );
            
if ( !empty( $terms ) ) {
    foreach ( $terms as $term ) {
        echo $term->term_id;
    }
}
sumon cse-sust
  • 434
  • 4
  • 8