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>' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
Advertisements

9 thoughts on “WooCommerce – get category for product page

  1. Where to post this code???

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

  2. On the cart page, when the customer empty’s a cart, the Return to Shop button needs to direct the traffic to the Woocommerce product category page. please suggest

    1. What you mean by product page? any way if you want to change the ” Return to Shop” link then copy wooCommerce template in your theme woocommerce folder. Then go to “woocommerce” then “cart” folder and edit the “cart-empty.php” .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s