How to get WooCommerce Product Category Link by ID?

Another update (Sept. 2015):

I can use get_term_link after all. The problem was that the string needed to be converted to an integer. Used a Stack Overflow tip for the fastest way to convert it using the (int)$value in PHP.

So it would look like this if you don’t want to use the slug in the foreach loop:

$woo_cat_id_int = (int)$woo_cat_id; //convert 

That converted value is used instead of the slug in get_term_link. Hope it helps someone. 🙂


Looks like I figured it out.

I used get_term_link. And I was getting an error because I was using it this way:

get_term_link( $woo_cat_id, 'product_cat' );

Which gave me this error:

Object of class WP_Error could not be converted to string

So I went this route instead with the slug and it worked:

$prod_cat_args = array(
  'taxonomy'     => 'product_cat', //woocommerce
  'orderby'      => 'name',
  'empty'        => 0
);

$woo_categories = get_categories( $prod_cat_args );

foreach ( $woo_categories as $woo_cat ) {
    $woo_cat_id = $woo_cat->term_id; //category ID
    $woo_cat_name = $woo_cat->name; //category name
    $woo_cat_slug = $woo_cat->slug; //category slug


    $return .= '<a href="' . get_term_link( $woo_cat_slug, 'product_cat' ) . '">' . $woo_cat_name . '</a>';
}//end of $woo_categories foreach  

Leave a Comment