To get Category of the product is very easy. We have to get all categories related to Woo-commerce products. To do this WordPress function get_terms( ) is used. Following detail of this WordPress function is given:
<?php get_terms( string|array $args = array(), array $deprecated = '' ); ?>
So in get_terms accept string or array in first argument. To get all categories related to posts we put post_tag, But we have to take all categories related to products so instead of ‘post_tags‘ we will insert ‘product_cat‘.
<?php
$terms = get_terms( 'post_tag', array(
'hide_empty' => false,
) );
?>
<?php $terms = get_terms( 'product_cat' ); ?>
By using this argument we get all categories related to product categories. We will get array of all categories.